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.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/enumerize/attribute.rb', line 7
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
@i18n_scope = options[:i18n_scope]
@arguments = options
value_class = options.fetch(:value_class, Value)
@values = Array(options[:in]).map { |v| value_class.new(self, *v).freeze }
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
if options[:default]
@default_value = find_default_value(options[:default])
raise ArgumentError, 'invalid default value' unless @default_value
end
@skip_validations_value = options.fetch(:skip_validations, false)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
136
137
138
139
140
141
142
|
# File 'lib/enumerize/attribute.rb', line 136
def method_missing(method)
if @value_hash.include?(method.to_s)
find_value(method)
else
super
end
end
|
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def arguments
@arguments
end
|
#default_value ⇒ Object
Returns the value of attribute default_value.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def default_value
@default_value
end
|
#i18n_scope ⇒ Object
Returns the value of attribute i18n_scope.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def i18n_scope
@i18n_scope
end
|
#klass ⇒ Object
Returns the value of attribute klass.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def klass
@klass
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def name
@name
end
|
#skip_validations_value ⇒ Object
Returns the value of attribute skip_validations_value.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def skip_validations_value
@skip_validations_value
end
|
#values ⇒ Object
Returns the value of attribute values.
5
6
7
|
# File 'lib/enumerize/attribute.rb', line 5
def values
@values
end
|
Instance Method Details
#define_methods!(mod) ⇒ Object
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
124
125
126
127
128
129
130
131
132
|
# File 'lib/enumerize/attribute.rb', line 91
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
|
#each_value ⇒ Object
48
49
50
|
# File 'lib/enumerize/attribute.rb', line 48
def each_value
values.each { |value| yield value }
end
|
#find_default_value(value) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/enumerize/attribute.rb', line 32
def find_default_value(value)
if value.respond_to?(:call)
value
else
find_value(value)
end
end
|
#find_value(value) ⇒ Object
40
41
42
|
# File 'lib/enumerize/attribute.rb', line 40
def find_value(value)
@value_hash[value.to_s] unless value.nil?
end
|
#find_values(*values) ⇒ Object
44
45
46
|
# File 'lib/enumerize/attribute.rb', line 44
def find_values(*values)
values.map { |value| find_value(value) }.compact
end
|
#i18n_scopes ⇒ Object
56
57
58
59
60
61
62
63
64
|
# File 'lib/enumerize/attribute.rb', line 56
def i18n_scopes
@i18n_scopes ||= if i18n_scope
Array(i18n_scope)
elsif @klass.respond_to?(:model_name)
["enumerize.#{@klass.model_name.i18n_key}.#{name}"]
else
[]
end
end
|
#options(options = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/enumerize/attribute.rb', line 66
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
87
88
89
|
# File 'lib/enumerize/attribute.rb', line 87
def respond_to_missing?(method, include_private=false)
@value_hash.include?(method.to_s) || super
end
|
#value?(value) ⇒ Boolean
52
53
54
|
# File 'lib/enumerize/attribute.rb', line 52
def value?(value)
values.include?(value)
end
|