Class: Sandals::Select

Inherits:
Object
  • Object
show all
Includes:
Actionable, FieldValidation
Defined in:
lib/sandals/view.rb

Direct Known Subclasses

Radio

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Attributes included from FieldValidation

#error

Instance Method Summary collapse

Methods included from FieldValidation

#error_id, #invalid?

Constructor Details

#initialize(id, label, options:, value:, action:, error: nil) ⇒ Select

Returns a new instance of Select.



435
436
437
438
439
440
441
442
443
# File 'lib/sandals/view.rb', line 435

def initialize(id, label, options:, value:, action:, error: nil)
  @id = id
  @label = label.to_s
  @options = normalize(options)
  @value = value
  initialize_error(error)
  @action = action
  validate_unique_values
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



433
434
435
# File 'lib/sandals/view.rb', line 433

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



433
434
435
# File 'lib/sandals/view.rb', line 433

def label
  @label
end

#optionsObject (readonly)

Returns the value of attribute options.



433
434
435
# File 'lib/sandals/view.rb', line 433

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



433
434
435
# File 'lib/sandals/view.rb', line 433

def value
  @value
end

Instance Method Details

#change(value) ⇒ Object

Raises:

  • (ArgumentError)


445
446
447
448
449
450
# File 'lib/sandals/view.rb', line 445

def change(value)
  option = @options.find { |candidate| candidate.value.to_s == value }
  raise ArgumentError, "unknown select value: #{value}" unless option

  execute_action(option.value)
end

#selected?(option) ⇒ Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/sandals/view.rb', line 452

def selected?(option)
  option.value == value
end