Class: Compendium::ParamWithChoices

Inherits:
Param
  • Object
show all
Defined in:
lib/compendium/param_types.rb

Direct Known Subclasses

DropdownParam, RadioParam

Instance Method Summary collapse

Methods inherited from Param

#==, #boolean?, #date?, #dropdown?, #nil?, #radio?, #scalar?

Constructor Details

#initialize(obj, choices) ⇒ ParamWithChoices

Returns a new instance of ParamWithChoices.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/compendium/param_types.rb', line 25

def initialize(obj, choices)
  @choices = choices

  if @choices.respond_to?(:call)
    # If given a proc, defer determining values until later.
    index = obj
  else
    index = obj.numeric? ? obj.to_i : @choices.index(obj)
    raise IndexError if (!obj.nil? and index.nil?) or index.to_i.abs > @choices.length - 1
  end

  super(index)
end

Instance Method Details

#valueObject



39
40
41
# File 'lib/compendium/param_types.rb', line 39

def value
  @choices[self]
end