module Enumerize::Base

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/enumerize/base.rb, line 49
def initialize(*)
  super
  self.class.enumerized_attributes.each do |attr|
    if !public_send(attr.name) && !_enumerized_values_for_validation.key?(attr.name)
      public_send("#{attr.name}=", attr.default_value)
    end
  end
end

Public Instance Methods

read_attribute_for_validation(key) click to toggle source
Calls superclass method
# File lib/enumerize/base.rb, line 58
def read_attribute_for_validation(key)
  if _enumerized_values_for_validation.has_key?(key)
    _enumerized_values_for_validation[key]
  else
    super
  end
end

Private Instance Methods

_enumerized_values_for_validation() click to toggle source
# File lib/enumerize/base.rb, line 68
def _enumerized_values_for_validation
  @_enumerized_values_for_validation ||= {}
end
_validate_enumerized_attributes() click to toggle source
# File lib/enumerize/base.rb, line 72
def _validate_enumerized_attributes
  self.class.enumerized_attributes.each do |attr|
    value = read_attribute_for_validation(attr.name)
    next if value.blank?

    if attr.kind_of? Multiple
      errors.add attr.name unless value.respond_to?(:all?) && value.all? { |v| v.blank? || attr.find_value(v) }
    else
      errors.add attr.name, :inclusion unless attr.find_value(value)
    end
  end
end