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
36
37
38
39
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 36
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
#blank_value ⇒ Object
20
21
22
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 20
def blank_value
@blank_value ||= ::EacRubyUtils::Listable::Value.new(self, BLANK_VALUE, BLANK_KEY, false)
end
|
#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
45
46
47
48
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 45
def hash_keys_validate!(hash, error_class = ::StandardError)
hash.each_key { |key| value_validate!(key, error_class) }
hash
end
|
50
51
52
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 50
def i18n_key
"eac_ruby_utils.listable.#{class_i18n_key}.#{item}"
end
|
55
56
57
58
59
60
61
62
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 55
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
|
32
33
34
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 32
def options
@values.values.map { |v| [v.label, v.value] }
end
|
#respond_to_missing?(name, include_all = false) ⇒ Boolean
41
42
43
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 41
def respond_to_missing?(name, include_all = false)
find_list_by_method(name) || super
end
|
#value_valid?(value) ⇒ Boolean
64
65
66
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 64
def value_valid?(value)
values.include?(value)
end
|
#value_validate!(value, error_class = ::StandardError) ⇒ Object
68
69
70
71
72
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 68
def value_validate!(value, error_class = ::StandardError)
return value if value_valid?(value)
raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
end
|
28
29
30
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 28
def values
@values.values.map(&:value)
end
|
#values_to_s ⇒ Object
74
75
76
|
# File 'lib/eac_ruby_utils/listable/list.rb', line 74
def values_to_s
values.map { |v| "\"#{v}\"" }.join(', ')
end
|