From fb927bbb93fb7b4705f74e2d3c0399e1e9cf395d Mon Sep 17 00:00:00 2001 From: Nathaniel Watts <1141717+thewatts@users.noreply.github.com> Date: Fri, 22 Oct 2021 17:12:36 -0500 Subject: [PATCH 1/2] Add custom comparison for Montrose::Schedule In our application using Montrose, we have some instances where we want to be able to check to see if a schedule has changed. The specific use case for us is with a Rails model that is serializing a schedule - we want to be able to know if the schedule itself has changed, ie: `event.schedule_changed? #=> false` The same attributes may be assigned for the schedule's configuration - but Rails thinks that the schedule has changed b/c it's comparing the objects themselves instead of the underlying recurrence configurations. This commit overloads `Montrose::Schedule#==` so that schedules can be compared against each other within the context of their configurations. --- lib/montrose/schedule.rb | 8 ++++++++ spec/montrose/schedule_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/montrose/schedule.rb b/lib/montrose/schedule.rb index aaa3c6a..8e51ba6 100644 --- a/lib/montrose/schedule.rb +++ b/lib/montrose/schedule.rb @@ -169,6 +169,14 @@ def inspect "#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>" end + def ==(other) + if other.is_a?(self.class) || other.is_a?(Array) + to_a == other.to_a + else + super + end + end + private def active_enums(enums) diff --git a/spec/montrose/schedule_spec.rb b/spec/montrose/schedule_spec.rb index 1573a1b..4394533 100644 --- a/spec/montrose/schedule_spec.rb +++ b/spec/montrose/schedule_spec.rb @@ -337,4 +337,27 @@ _(array).must_equal [{"every" => "month"}, {"every" => "day"}] end end + + describe "#==" do + before do + schedule << {every: :month} + schedule << {every: :day} + end + + it "compares two schedules against each other" do + identical_other = new_schedule.tap do |schedule| + schedule << {every: :month} + schedule << {every: :day} + end + + different_other = new_schedule.tap do |schedule| + schedule << {every: :month} + schedule << {every: :day} + schedule << {every: :year} + end + + _(schedule).must_equal identical_other + _(schedule).wont_equal different_other + end + end end From d1d60bd428702fd17edce1eae054a25cf5c967e6 Mon Sep 17 00:00:00 2001 From: Nathaniel Watts <1141717+thewatts@users.noreply.github.com> Date: Sun, 24 Oct 2021 13:09:27 -0500 Subject: [PATCH 2/2] Add custom comparison for Montrose::Recurrence In the same vein as adding `Montrose::Schedule#==` This allows `Montrose::Recurrence` objects to be compared by their underlying hash options. --- lib/montrose/recurrence.rb | 13 +++++++++++++ spec/montrose/recurrence_spec.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/montrose/recurrence.rb b/lib/montrose/recurrence.rb index 8ba1f46..429210e 100644 --- a/lib/montrose/recurrence.rb +++ b/lib/montrose/recurrence.rb @@ -407,6 +407,19 @@ def later?(timestamp) ends_at && timestamp > ends_at end + # Return true/false if hash configuration is equal to + # the other given recurrence's hash configuration + # + # @return [Boolean] whether or not the other's hash matches + # + def ==(other) + if other.is_a?(self.class) || other.is_a?(Hash) + to_hash == other.to_hash + else + super + end + end + private def event_enum diff --git a/spec/montrose/recurrence_spec.rb b/spec/montrose/recurrence_spec.rb index 9e45a52..235ae78 100644 --- a/spec/montrose/recurrence_spec.rb +++ b/spec/montrose/recurrence_spec.rb @@ -100,6 +100,19 @@ end end + describe "#==" do + it "compares the recurrence with another recurrence" do + options = {every: :day, total: 3, starts: now, interval: 1} + recurrence = new_recurrence(options) + + identical_recurrence = new_recurrence(options) + different_recurrence = new_recurrence(options.merge(interval: 2)) + + _(recurrence).must_equal identical_recurrence + _(recurrence).wont_equal different_recurrence + end + end + describe ".dump" do it "returns options as JSON string" do options = {every: :day, total: 3, starts: now, interval: 1}