Module: EnumeratedAttribute::Integrations::ActiveRecord

Defined in:
lib/enumerated_attribute/integrations/active_record.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 5

def self.included(klass)
	klass.extend(ClassMethods)
end

Instance Method Details

#[](attr_name) ⇒ Object



62
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 62

def [](attr_name); read_enumerated_attribute(attr_name); end

#[]=(attr_name, value) ⇒ Object



63
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 63

def []=(attr_name, value); write_enumerated_attribute(attr_name, value); end

#attributesObject



52
53
54
55
56
57
58
59
60
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 52

def attributes
	atts = super
	atts.each do |k,v|
		if self.class.has_enumerated_attribute?(k)
			atts[k] = v.to_sym if v
		end
	end
	atts
end

#attributes=(attrs, guard_protected_attributes = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 37

def attributes=(attrs, guard_protected_attributes=true)
	return if attrs.nil?
	#check the attributes then turn them over 
	attrs.each do |k, v|
		if self.class.has_enumerated_attribute?(k)
			unless self.class.enumerated_attribute_allows_value?(k, v)
				raise InvalidEnumeration, ":#{v.to_s} is not a defined enumeration value for the '#{k.to_s}' attribute", caller
			end
			attrs[k] = v.to_s
		end
	end
	
	super
end

#read_enumerated_attribute(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 24

def read_enumerated_attribute(name)
	name = name.to_s
	#if not enumerated - let active record handle it
	return read_attribute(name) unless self.class.has_enumerated_attribute?(name)
	#if enumerated, is it an active record attribute, if not, the value is stored in an instance variable
	name_sym = name.to_sym
	return instance_variable_get('@'+name) unless self.has_attribute?(name)
	#this is an enumerated active record attribute
	val = read_attribute(name)
	val = val.to_sym if !!val
	val
end

#write_enumerated_attribute(name, val) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/enumerated_attribute/integrations/active_record.rb', line 9

def write_enumerated_attribute(name, val)
	name = name.to_s
	return write_attribute(name, val) unless self.class.has_enumerated_attribute?(name)
	val = nil if val == ''
	val_str = val.to_s if val
	val_sym = val.to_sym if val
	unless self.class.enumerated_attribute_allows_value?(name, val_sym)
		raise(InvalidEnumeration, "nil is not allowed on '#{name}' attribute, set :nil=>true option", caller) unless val
		raise(InvalidEnumeration, ":#{val_str} is not a defined enumeration value for the '#{name}' attribute", caller)
	end
	return instance_variable_set('@'+name, val_sym) unless self.has_attribute?(name)
	write_attribute(name, val_str)
	val_sym
end