Module: SmartTime::ActiveRecordExtension::ClassMethods

Defined in:
lib/smart_time/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#smart_time(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smart_time/active_record_extension.rb', line 14

def smart_time(*args)
	options = args.extract_options!
	new_attributes = args
	first_call = !defined?(@smart_time_attributes)
	@smart_time_attributes ||= {}

	# If no attributes were specified then add all parsable attributes
	if new_attributes.empty?
		columns.each do |column|
			@smart_time_attributes[column.name.to_sym] = options if SmartTime.can_parse?(column.klass)
		end
	else
		new_attributes.each do |attribute|
			@smart_time_attributes[attribute.to_sym] = options
		end
	end

	# Override attribute write method
	unless first_call
		self.class_eval do
			def write_attribute(attr_name, value)
				if self.class.smart_time_attributes[attr_name.to_sym]
					value = SmartTime.parse(value, column_for_attribute(attr_name).klass, self.class.smart_time_attributes[attr_name.to_sym])
				end
				super(attr_name, value)
			end
		end
	end

end

#smart_time_attributesObject



10
11
12
# File 'lib/smart_time/active_record_extension.rb', line 10

def smart_time_attributes
	@smart_time_attributes
end