Skip to content

Instantly share code, notes, and snippets.

@fsaravia
Created December 19, 2013 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsaravia/8042119 to your computer and use it in GitHub Desktop.
Save fsaravia/8042119 to your computer and use it in GitHub Desktop.
DataMapper timzone enabled methods
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
@fsaravia
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment