Class: Railsboot::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/railsboot/component.rb

Overview

Parent class for all RailsbootUI related components.

Constant Summary collapse

COLORS =

Colors.

%w[primary secondary success danger warning info light dark].freeze
DEFAULT_COLOR =

Default color.

"primary".freeze

Instance Method Summary collapse

Instance Method Details

#fetch_or_fallback(argument, constant, fallback) ⇒ String, Symbol (protected)

Validates that an argument is present and included in the given constant,
falling back to a default otherwise.

Parameters:

  • argument (String, Symbol)

    the argument value to validate

  • constant (Array<String>)

    the list of allowed option strings

  • fallback (String, Symbol)

    the value returned when the argument is invalid

Returns:

  • (String, Symbol)

    the validated argument or the fallback



30
31
32
33
34
35
36
# File 'app/components/railsboot/component.rb', line 30

def fetch_or_fallback(argument, constant, fallback)
  if argument.present? && constant.include?(argument.to_s)
    argument
  else
    fallback
  end
end

#fetch_or_raise(argument, constant) ⇒ String, Symbol (protected)

Validates that an argument is present and included in the given constant,
raising an error listing the allowed options otherwise.

Parameters:

  • argument (String, Symbol)

    the argument value to validate

  • constant (Array<String>)

    the list of allowed option strings

Returns:

  • (String, Symbol)

    the validated argument

Raises:

  • (ArgumentError)

    when the argument is blank or not in +constant+



18
19
20
21
22
# File 'app/components/railsboot/component.rb', line 18

def fetch_or_raise(argument, constant)
  raise ArgumentError, "The `#{argument}: #{argument}` argument is not valid, allowed options are #{constant.join(', ')}." unless argument.present? && constant.include?(argument.to_s)

  argument
end