Skip to content

Instantly share code, notes, and snippets.

@texel
Created June 25, 2009 18:18
Show Gist options
  • Save texel/136043 to your computer and use it in GitHub Desktop.
Save texel/136043 to your computer and use it in GitHub Desktop.
module TimeAttributeExtensions
module ClassMethods
def define_attribute_methods_with_time_parsing
if define_attribute_methods_without_time_parsing
columns_hash.each do |name, column|
if [:datetime, :timestamp].include?(column.type)
unless method_defined?(:"#{name}_without_time_parsing=")
define_method("#{name}_with_time_parsing=") do |time|
send(:"#{name}_without_time_parsing=", parse_time_from_hash(time))
end
alias_method_chain :"#{name}=", :time_parsing
end
end
end
return true
end
end
end
module InstanceMethods
def parse_time_from_hash(time)
# Do time parsing here
end
end
def self.included(receiver)
receiver.send(:include, InstanceMethods)
receiver.extend ClassMethods
unless receiver.respond_to?(:define_attribute_methods_without_time_parsing)
class << receiver
alias_method_chain :define_attribute_methods, :time_parsing
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment