Sha256: 5ff1076d7fa5677d532f5590a5d9a4a7254ad88ea22c041e53cf4e729a8c7425

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 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 <<-NEWMETH
						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
					NEWMETH
				end
				
			end
		
		end	
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jeffp-enumerated_attribute-0.2.3 lib/enumerated_attribute/integrations/object.rb