Class: EacRubyUtils::Listable::List
- Inherits:
-
Object
- Object
- EacRubyUtils::Listable::List
show all
- Defined in:
- lib/eac_ruby_utils/listable/list.rb
Constant Summary
collapse
- BLANK_VALUE =
nil
- BLANK_KEY =
:__blank
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(lists, item, labels) ⇒ List
Returns a new instance of List.
13
14
15
16
17
18
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 13
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
54
55
56
57
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 54
def method_missing(name, *args, &block)
list = find_list_by_method(name)
list || super
end
|
Instance Attribute Details
Returns the value of attribute item.
11
12
13
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 11
def item
@item
end
|
Instance Method Details
#each_value(&block) ⇒ Object
24
25
26
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 24
def each_value(&block)
values.each(&block)
end
|
#hash_keys_validate!(hash, error_class = ::StandardError) ⇒ Object
63
64
65
66
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 63
def hash_keys_validate!(hash, error_class = ::StandardError)
hash.each_key { |key| value_validate!(key, error_class) }
hash
end
|
68
69
70
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 68
def i18n_key
"eac_ruby_utils.listable.#{class_i18n_key}.#{item}"
end
|
73
74
75
76
77
78
79
80
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 73
def instance_value(instance)
v = instance.send(item)
return blank_value if v.blank?
return @values[v] if @values.key?(v)
raise "List value unkown: \"#{v}\" (Source: #{@lists.source}, Item: #{item}, Instance: " \
"#{instance.to_debug}, Values: #{@values.keys})"
end
|
29
30
31
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 29
def item_by_value(value)
@values.values.find { |item| item.value == value }
end
|
34
35
36
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 34
def item_by_value!(value)
item_by_value(value) || raise(::KeyError, "Value not found: #{value}")
end
|
46
47
48
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 46
def options
@values.values.map(&:to_option)
end
|
#options_with_blank ⇒ Object
50
51
52
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 50
def options_with_blank
[blank_value.to_option] + options
end
|
#respond_to_missing?(name, include_all = false) ⇒ Boolean
59
60
61
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 59
def respond_to_missing?(name, include_all = false)
find_list_by_method(name) || super
end
|
#value_valid?(value) ⇒ Boolean
82
83
84
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 82
def value_valid?(value)
values.include?(value)
end
|
#value_validate!(value, error_class = ::StandardError) ⇒ Object
86
87
88
89
90
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 86
def value_validate!(value, error_class = ::StandardError)
return value if value_valid?(value)
raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
end
|
38
39
40
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 38
def values
@values.values.map(&:value)
end
|
#values_to_s ⇒ Object
92
93
94
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 92
def values_to_s
values.map { |v| "\"#{v}\"" }.join(', ')
end
|
#values_with_blank ⇒ Object
42
43
44
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 42
def values_with_blank
[blank_value.value] + values
end
|