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.



413
414
415
416
417
418
419
420
421
# File 'lib/sandals/view.rb', line 413

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.



411
412
413
# File 'lib/sandals/view.rb', line 411

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



411
412
413
# File 'lib/sandals/view.rb', line 411

def label
  @label
end

#optionsObject (readonly)

Returns the value of attribute options.



411
412
413
# File 'lib/sandals/view.rb', line 411

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



411
412
413
# File 'lib/sandals/view.rb', line 411

def value
  @value
end

Instance Method Details

#change(value) ⇒ Object

Raises:

  • (ArgumentError)


423
424
425
426
427
428
# File 'lib/sandals/view.rb', line 423

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)


430
431
432
# File 'lib/sandals/view.rb', line 430

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