Class: EacRubyUtils::Console::Speaker::List

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/console/speaker/list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ List

Returns a new instance of List.



30
31
32
33
34
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 30

def initialize(values)
  @values = values.map do |v|
    ::OpenStruct.new(key: to_key(v.key), label: to_label(v.label), value: v.value)
  end
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



28
29
30
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 28

def values
  @values
end

Class Method Details

.build(list) ⇒ Object



11
12
13
14
15
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 11

def build(list)
  return List.new(hash_to_values(list)) if list.is_a?(::Hash)
  return List.new(array_to_values(list)) if list.is_a?(::Array)
  raise "Invalid list: #{list} (#{list.class})"
end

Instance Method Details

#build_value(value) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 52

def build_value(value)
  key = to_key(value)
  values.each do |v|
    return v.value if v.key == key
  end
  raise "Value not found: \"#{value}\" (#{values})"
end

#to_key(value) ⇒ Object



44
45
46
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 44

def to_key(value)
  to_label(value).downcase
end

#to_label(value) ⇒ Object



48
49
50
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 48

def to_label(value)
  value.to_s.strip
end

#valid_labelsObject



36
37
38
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 36

def valid_labels
  values.map(&:label)
end

#valid_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/eac_ruby_utils/console/speaker/list.rb', line 40

def valid_value?(value)
  values.any? { |v| v.key == to_key(value) }
end