ice_cube icon indicating copy to clipboard operation
ice_cube copied to clipboard

Implement (override) hash method and supply hash serializer

Open voltechs opened this issue 10 years ago • 2 comments

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

voltechs avatar Feb 14 '15 08:02 voltechs

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?

avit avatar Feb 20 '15 20:02 avit

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.

avit avatar Jan 31 '16 04:01 avit