Class: Enumerize::Attribute
- Inherits:
-
Object
- Object
- Enumerize::Attribute
show all
- Defined in:
- lib/enumerize/attribute.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/enumerize/attribute.rb', line 5
def initialize(klass, name, options={})
raise ArgumentError, ':in option is required' unless options[:in]
raise ArgumentError, ':scope option does not work with option :multiple' if options[:multiple] && options[:scope]
extend Multiple if options[:multiple]
@klass = klass
@name = name.to_sym
value_class = options.fetch(:value_class, Value)
@values = Array(options[:in]).map { |v| value_class.new(self, *v) }
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
if options[:i18n_scope]
raise ArgumentError, ':i18n_scope option accepts only String or Array of strings' unless Array(options[:i18n_scope]).all? { |s| s.is_a?(String) }
@i18n_scope = options[:i18n_scope]
end
if options[:default]
@default_value = find_default_value(options[:default])
raise ArgumentError, 'invalid default value' unless @default_value
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/enumerize/attribute.rb', line 127
def method_missing(method)
if @value_hash.include?(method.to_s)
find_value(method)
else
super
end
end
|
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
3
4
5
|
# File 'lib/enumerize/attribute.rb', line 3
def default_value
@default_value
end
|
#i18n_scope ⇒ Object
Returns the value of attribute i18n_scope.
3
4
5
|
# File 'lib/enumerize/attribute.rb', line 3
def i18n_scope
@i18n_scope
end
|
#name ⇒ Object
Returns the value of attribute name.
3
4
5
|
# File 'lib/enumerize/attribute.rb', line 3
def name
@name
end
|
#values ⇒ Object
Returns the value of attribute values.
3
4
5
|
# File 'lib/enumerize/attribute.rb', line 3
def values
@values
end
|
Instance Method Details
#define_methods!(mod) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/enumerize/attribute.rb', line 82
def define_methods!(mod)
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}
if defined?(super)
self.class.enumerized_attributes[:#{name}].find_value(super)
elsif respond_to?(:read_attribute)
self.class.enumerized_attributes[:#{name}].find_value(read_attribute(:#{name}))
else
if defined?(@#{name})
self.class.enumerized_attributes[:#{name}].find_value(@#{name})
else
@#{name} = nil
end
end
end
def #{name}=(new_value)
allowed_value_or_nil = self.class.enumerized_attributes[:#{name}].find_value(new_value)
allowed_value_or_nil = allowed_value_or_nil.value unless allowed_value_or_nil.nil?
if defined?(super)
super allowed_value_or_nil
elsif respond_to?(:write_attribute, true)
write_attribute '#{name}', allowed_value_or_nil
else
@#{name} = allowed_value_or_nil
end
_enumerized_values_for_validation['#{name}'] = new_value.nil? ? nil : new_value.to_s
allowed_value_or_nil
end
def #{name}_text
self.#{name} && self.#{name}.text
end
def #{name}_value
self.#{name} && self.#{name}.value
end
RUBY
end
|
#find_default_value(value) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/enumerize/attribute.rb', line 31
def find_default_value(value)
if value.respond_to?(:call)
value
else
find_value(value)
end
end
|
#find_value(value) ⇒ Object
39
40
41
|
# File 'lib/enumerize/attribute.rb', line 39
def find_value(value)
@value_hash[value.to_s] unless value.nil?
end
|
#find_values(*values) ⇒ Object
43
44
45
|
# File 'lib/enumerize/attribute.rb', line 43
def find_values(*values)
values.map { |value| find_value(value) }.compact
end
|
#i18n_scopes ⇒ Object
47
48
49
50
51
52
53
54
55
|
# File 'lib/enumerize/attribute.rb', line 47
def i18n_scopes
@i18n_scopes ||= if i18n_scope
scopes = Array(i18n_scope)
elsif @klass.respond_to?(:model_name)
scopes = ["enumerize.#{@klass.model_name.i18n_key}.#{name}"]
else
[]
end
end
|
#options(options = {}) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/enumerize/attribute.rb', line 57
def options(options = {})
values = if options.empty?
@values
else
raise ArgumentError, 'Options cannot have both :only and :except' if options[:only] && options[:except]
only = Array(options[:only]).map(&:to_s)
except = Array(options[:except]).map(&:to_s)
@values.reject do |value|
if options[:only]
!only.include?(value)
elsif options[:except]
except.include?(value)
end
end
end
values.map { |v| [v.text, v.to_s] }
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
78
79
80
|
# File 'lib/enumerize/attribute.rb', line 78
def respond_to_missing?(method, include_private=false)
@value_hash.include?(method.to_s) || super
end
|