Sha256: 0677f58eb3f3b9ff9b1bab69c8a5e8dab943620fd09299b26748b0905bffc66d

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 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 = 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)
				return instance_variable_get('@'+name.to_s)
			end

			module ClassMethods
				private
				
				def define_enumerated_attribute_new_method
					class_eval <<-NEWMETH
						class << self
							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
					NEWMETH
				end
				
			end
		
		end	
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jeffp-enumerated_attribute-0.1.6 lib/enumerated_attribute/integrations/object.rb
jeffp-enumerated_attribute-0.1.7 lib/enumerated_attribute/integrations/object.rb