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.


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

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


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

def values
  @values
end

Class Method Details

.build(list) ⇒ Object


11
12
13
14
15
16
# 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


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

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


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

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

#to_label(value) ⇒ Object


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

def to_label(value)
  value.to_s.strip
end

#valid_labelsObject


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

def valid_labels
  values.map(&:label)
end

#valid_value?(value) ⇒ Boolean

Returns:


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

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