module SmartTime::ActiveRecordExtension::ClassMethods

Public Instance Methods

smart_time(*args) click to toggle source
# 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_attributes() click to toggle source
# File lib/smart_time/active_record_extension.rb, line 10
def smart_time_attributes
        @smart_time_attributes
end
write_attribute(attr_name, value) click to toggle source
Calls superclass method
# File lib/smart_time/active_record_extension.rb, line 34
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