Created
May 21, 2015 18:42
-
-
Save danielberkompas/1cfd1d936c6397faf56d to your computer and use it in GitHub Desktop.
DateTime marshaler for attr_encrypted
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
require "attr_encrypted" | |
require "active_support/core_ext/date_time" | |
module DateTimeMarshaler | |
FORMAT = "%F %H:%M:%S" | |
def self.dump(datetime) | |
datetime.strftime(FORMAT) | |
end | |
def self.load(string) | |
DateTime.parse(string) | |
end | |
end | |
class Thing | |
attr_encrypted :datetime, marshal: true, | |
marshaler: DateTimeMarshaler, | |
dump_method: :dump, | |
load_method: :load, | |
key: "secret" | |
end | |
marshaled = DateTimeMarshaler.dump(Time.now) # => "2015-05-21 11:42:20" | |
DateTimeMarshaler.load(marshaled) # => Thu, 21 May 2015 11:42:20 +0000 | |
thing = Thing.new | |
thing.datetime = Time.now | |
thing.datetime # => 2015-05-21 11:42:20 -0700 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment