Implement (override) hash method and supply hash serializer
IceCube::Schedule should implement the :hash method so that in-place modifications (rails) are sense, and can be saved.
https://github.com/rails/rails/pull/15458
Also, a native hash serializer would be nice. YAML is slower and (IMHO) not any easier to read than a hash.
class IceCube::Hasher
def self.dump(schedule)
return {} if schedule.blank?
schedule.to_hash
end
def self.load(hash)
hash = {} if hash.blank?
return IceCube::Schedule.from_yaml(hash) if !!(hash =~ /\A---/)
IceCube::Schedule.from_hash(hash)
end
end
Adding the hash method makes sense, for purposes of object value equality (which just returns an integer for comparison). (PR welcome if I don't get to it!)
I don't see how the "native hash serializer" is related to this though, I'm assuming you mean postgres Hstore? (Just to be sure we're on the same page, #to_hash is not the same thing as #hash.)
YAML is used because we need to include time zone, not just a fixed offset. Note how "PST" gets lost, at least when converting to JSON:
Time.zone = 'America/Vancouver'
#=> "America/Vancouver"
Time.zone.now
#=> Fri, 20 Feb 2015 11:53:07 PST -08:00
Time.zone.now.to_json
#=> "\"2015-02-20T11:53:17.161-08:00\""
You would need to keep the correct time zone to handle Daylight Saving Time. I'm not familiar with recent changes in Rails serialization, is your proposed implementation able to handle that?
Some of this looks like what I already have in ScheduleAttributes:
https://github.com/avit/schedule_attributes/blob/master/lib/schedule_attributes/serializer.rb
It probably makes sense to move it into this gem.