Module: Enumerize::Multiple

Defined in:
lib/enumerize/attribute.rb

Instance Method Summary collapse

Instance Method Details

#define_methods!(mod) ⇒ Object


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/enumerize/attribute.rb', line 153

def define_methods!(mod)
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      unless defined?(@_#{name}_enumerized_set)
        if defined?(super)
          self.#{name} = super
        elsif respond_to?(:read_attribute)
          self.#{name} = read_attribute(:#{name})
        else
          if defined?(@#{name})
            self.#{name} = @#{name}
          else
            self.#{name} = []
          end
        end
      end

      @_#{name}_enumerized_set
    end

    def #{name}=(values)
      @_#{name}_enumerized_set = Enumerize::Set.new(self, self.class.enumerized_attributes[:#{name}], values)
      raw_values = self.#{name}.values.map(&:value)

      if defined?(super)
        super raw_values
      elsif respond_to?(:write_attribute, true)
        write_attribute '#{name}', raw_values
      else
        @#{name} = raw_values
      end

      _enumerized_values_for_validation['#{name}'] = values.respond_to?(:map) ? values.reject(&:blank?).map(&:to_s) : values

      self.#{name}
    end
  RUBY
end

#find_default_value(value) ⇒ Object


145
146
147
148
149
150
151
# File 'lib/enumerize/attribute.rb', line 145

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