Created
December 19, 2013 16:31
-
-
Save fsaravia/8042119 to your computer and use it in GitHub Desktop.
DataMapper timzone enabled methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ZonedTimes | |
@@zoned_methods = [] | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def zoned_times(*args) | |
if args.empty? | |
@@zoned_methods | |
else | |
@@zoned_methods = args | |
create_zoned_accessors | |
end | |
end | |
def create_zoned_accessors | |
@@zoned_methods.each do |method| | |
User.module_eval{ | |
define_method(method){|*args| | |
return super() if args.empty? | |
return super().in_time_zone(args[0]) | |
} | |
} | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.9.3p484 :001 > User.first.created_at
=> Thu, 19 Dec 2013 13:02:30 +0000
1.9.3p484 :002 > User.first.created_at('Buenos Aires')
=> Thu, 19 Dec 2013 10:02:30 ART -03:00