Sha256: d0714e0489fc25735a88da44e2e3c771fca5e3db44f95d5b2c82d043975ccb9e
Contents?: true
Size: 1.2 KB
Versions: 8
Compression:
Stored size: 1.2 KB
Contents
module EnumeratedAttribute module Integrations module Object def self.included(klass) klass.extend(ClassMethods) end def write_enumerated_attribute(name, val) name = name.to_s val = nil if val == '' val = val.to_sym if val unless self.class.enumerated_attribute_allows_value?(name, val) raise(InvalidEnumeration, "nil is not allowed on '#{name}' attribute, set :nil=>true option", caller) unless val raise(InvalidEnumeration, ":#{val} is not a defined enumeration value for the '#{name}' attribute", caller) end instance_variable_set('@'+name, val) end def read_enumerated_attribute(name) instance_variable_get('@'+name.to_s) end module ClassMethods private def define_enumerated_attribute_new_method class_eval do class << self unless method_defined?(:new_without_enumerated_attribute) alias_method :new_without_enumerated_attribute, :new def new(*args, &block) result = new_without_enumerated_attribute(*args) result.initialize_enumerated_attributes yield result if block_given? result end end end end end end end end end
Version data entries
8 entries across 8 versions & 2 rubygems