Class: Enumerize::Attribute
- Inherits:
-
Object
- Object
- Enumerize::Attribute
- Defined in:
- lib/enumerize/attribute.rb
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#i18n_scope ⇒ Object
readonly
Returns the value of attribute i18n_scope.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #define_methods!(mod) ⇒ Object
- #find_default_value(value) ⇒ Object
- #find_value(value) ⇒ Object
- #i18n_scopes ⇒ Object
-
#initialize(klass, name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #options(options = {}) ⇒ Object
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 |
# File 'lib/enumerize/attribute.rb', line 5 def initialize(klass, name, ={}) raise ArgumentError, ':in option is required' unless [:in] extend Multiple if [:multiple] @klass = klass @name = name.to_sym @values = Array([:in]).map { |v| Value.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 [:i18n_scope] raise ArgumentError, ':i18n_scope option accepts only String or Array of strings' unless Array([:i18n_scope]).all? { |s| s.is_a?(String) } @i18n_scope = [:i18n_scope] end if [:default] @default_value = find_default_value([:default]) raise ArgumentError, 'invalid default value' unless @default_value end end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
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 (readonly)
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 (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/enumerize/attribute.rb', line 3 def name @name end |
#values ⇒ Object (readonly)
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
70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/enumerize/attribute.rb', line 70 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) _enumerized_values_for_validation[:#{name}] = new_value.nil? ? nil : new_value.to_s 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 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
27 28 29 30 31 32 33 |
# File 'lib/enumerize/attribute.rb', line 27 def find_default_value(value) if value.respond_to?(:call) value else find_value(value) end end |
#find_value(value) ⇒ Object
35 36 37 |
# File 'lib/enumerize/attribute.rb', line 35 def find_value(value) @value_hash[value.to_s] unless value.nil? end |
#i18n_scopes ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/enumerize/attribute.rb', line 39 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/enumerize/attribute.rb', line 49 def ( = {}) values = if .empty? @values else raise ArgumentError, 'Options cannot have both :only and :except' if [:only] && [:except] only = Array([:only]).map(&:to_s) except = Array([:except]).map(&:to_s) @values.reject do |value| if [:only] !only.include?(value) elsif [:except] except.include?(value) end end end values.map { |v| [v.text, v.to_s] } end |