Skip to content

Instantly share code, notes, and snippets.

@danielberkompas
Created May 21, 2015 18:42
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 danielberkompas/1cfd1d936c6397faf56d to your computer and use it in GitHub Desktop.
Save danielberkompas/1cfd1d936c6397faf56d to your computer and use it in GitHub Desktop.
DateTime marshaler for attr_encrypted
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