Class: Enumerize::Attribute

Inherits:
Object
  • Object
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.

Raises:

  • (ArgumentError)

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

  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

  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 (private)


135
136
137
138
139
140
141
# File 'lib/enumerize/attribute.rb', line 135

def method_missing(method)
  if @value_hash.include?(method.to_s)
    find_value(method)
  else
    super
  end
end

Instance Attribute Details

#default_valueObject (readonly)

Returns the value of attribute default_value


5
6
7
# File 'lib/enumerize/attribute.rb', line 5

def default_value
  @default_value
end

#i18n_scopeObject (readonly)

Returns the value of attribute i18n_scope


5
6
7
# File 'lib/enumerize/attribute.rb', line 5

def i18n_scope
  @i18n_scope
end

#klassObject (readonly)

Returns the value of attribute klass


5
6
7
# File 'lib/enumerize/attribute.rb', line 5

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/enumerize/attribute.rb', line 5

def name
  @name
end

#skip_validations_valueObject (readonly)

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

#valuesObject (readonly)

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


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
124
125
126
127
128
129
130
131
# File 'lib/enumerize/attribute.rb', line 90

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_valueObject


51
52
53
# File 'lib/enumerize/attribute.rb', line 51

def each_value
  values.each { |value| yield value }
end

#find_default_value(value) ⇒ Object


35
36
37
38
39
40
41
# File 'lib/enumerize/attribute.rb', line 35

def find_default_value(value)
  if value.respond_to?(:call)
    value
  else
    find_value(value)
  end
end

#find_value(value) ⇒ Object


43
44
45
# File 'lib/enumerize/attribute.rb', line 43

def find_value(value)
  @value_hash[value.to_s] unless value.nil?
end

#find_values(*values) ⇒ Object


47
48
49
# File 'lib/enumerize/attribute.rb', line 47

def find_values(*values)
  values.map { |value| find_value(value) }.compact
end

#i18n_scopesObject


55
56
57
58
59
60
61
62
63
# File 'lib/enumerize/attribute.rb', line 55

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/enumerize/attribute.rb', line 65

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

Returns:

  • (Boolean)

86
87
88
# File 'lib/enumerize/attribute.rb', line 86

def respond_to_missing?(method, include_private=false)
  @value_hash.include?(method.to_s) || super
end