Class: EacRubyUtils::Listable::List

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

Direct Known Subclasses

IntegerList, StringList, SymbolList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lists, item, labels) ⇒ List

Returns a new instance of List.



8
9
10
11
12
13
# File 'lib/eac_ruby_utils/listable/list.rb', line 8

def initialize(lists, item, labels)
  @lists = lists
  @item = item
  @values = build_values(labels)
  apply_constants
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



27
28
29
30
# File 'lib/eac_ruby_utils/listable/list.rb', line 27

def method_missing(name, *args, &block)
  list = find_list_by_method(name)
  list || super
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



6
7
8
# File 'lib/eac_ruby_utils/listable/list.rb', line 6

def item
  @item
end

Instance Method Details

#each_value(&block) ⇒ Object



15
16
17
# File 'lib/eac_ruby_utils/listable/list.rb', line 15

def each_value(&block)
  values.each(&block)
end

#hash_keys_validate!(hash, error_class = ::StandardError) ⇒ Object



36
37
38
39
# File 'lib/eac_ruby_utils/listable/list.rb', line 36

def hash_keys_validate!(hash, error_class = ::StandardError)
  hash.keys.each { |key| value_validate!(key, error_class) }
  hash
end

#i18n_keyObject



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

def i18n_key
  "eac_ruby_utils.listable.#{class_i18n_key}.#{item}"
end

#instance_value(instance) ⇒ Object



45
46
47
48
49
50
# File 'lib/eac_ruby_utils/listable/list.rb', line 45

def instance_value(instance)
  v = instance.send(item)
  return @values[v] if @values.key?(v)

  raise "List value unkown: #{v} (Source: #{@lists.source}, Item: #{item})"
end

#optionsObject



23
24
25
# File 'lib/eac_ruby_utils/listable/list.rb', line 23

def options
  @values.values.map { |v| [v.label, v.value] }
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:



32
33
34
# File 'lib/eac_ruby_utils/listable/list.rb', line 32

def respond_to_missing?(name, include_all = false)
  find_list_by_method(name) || super
end

#value_valid?(value) ⇒ Boolean

Returns:



52
53
54
# File 'lib/eac_ruby_utils/listable/list.rb', line 52

def value_valid?(value)
  values.include?(value)
end

#value_validate!(value, error_class = ::StandardError) ⇒ Object



56
57
58
59
# File 'lib/eac_ruby_utils/listable/list.rb', line 56

def value_validate!(value, error_class = ::StandardError)
  value_valid?(value) ||
    raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
end

#valuesObject



19
20
21
# File 'lib/eac_ruby_utils/listable/list.rb', line 19

def values
  @values.values.map(&:value)
end

#values_to_sObject



61
62
63
# File 'lib/eac_ruby_utils/listable/list.rb', line 61

def values_to_s
  values.map { |v| "\"#{v}\"" }.join(', ')
end