Class: EacCli::Speaker::List

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

Defined Under Namespace

Classes: VALUE_STRUCT

Constant Summary collapse

DEFAULT_IGNORE_CASE =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Object

Parameters:



44
45
46
47
48
49
# File 'lib/eac_cli/speaker/list.rb', line 44

common_constructor :values, :options, default: [{}] do
  self.options = self.class.lists.option.hash_keys_validate!(options)
  self.values = values.map do |v|
    VALUE_STRUCT.new(to_key(v.key), to_label(v.label), v.value)
  end
end

Instance Attribute Details

#valuesArray<VALUE_STRUCT>, Hash (readonly)

# @!attribute [r] options

Returns:



44
45
46
47
48
49
# File 'lib/eac_cli/speaker/list.rb', line 44

common_constructor :values, :options, default: [{}] do
  self.options = self.class.lists.option.hash_keys_validate!(options)
  self.values = values.map do |v|
    VALUE_STRUCT.new(to_key(v.key), to_label(v.label), v.value)
  end
end

Class Method Details

.build(list, options = {}) ⇒ Object

Parameters:

  • list (Array, Hash)
  • options (Hash) (defaults to: {})


14
15
16
17
18
19
# File 'lib/eac_cli/speaker/list.rb', line 14

def build(list, options = {})
  return List.new(hash_to_values(list), options) if list.is_a?(::Hash)
  return List.new(array_to_values(list), options) if list.is_a?(::Array)

  raise "Invalid list: #{list} (#{list.class})"
end

Instance Method Details

#build_value(value) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/eac_cli/speaker/list.rb', line 75

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

#ignore_case?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/eac_cli/speaker/list.rb', line 52

def ignore_case?
  options.if_key(OPTION_IGNORE_CASE, DEFAULT_IGNORE_CASE, &:to_bool)
end

#to_key(value) ⇒ String

Parameters:

Returns:

  • (String)


66
67
68
69
# File 'lib/eac_cli/speaker/list.rb', line 66

def to_key(value)
  r = to_label(value)
  ignore_case? ? r.downcase : r
end

#to_label(value) ⇒ Object



71
72
73
# File 'lib/eac_cli/speaker/list.rb', line 71

def to_label(value)
  value.to_s.strip
end

#valid_labelsObject



56
57
58
# File 'lib/eac_cli/speaker/list.rb', line 56

def valid_labels
  values.map(&:label)
end

#valid_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/eac_cli/speaker/list.rb', line 60

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