Skip to content

Commit

Permalink
Add custom comparison for Montrose::Schedule
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thewatts committed Oct 22, 2021
1 parent 1c52ff3 commit 0c6ed63
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/montrose/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
super
else
to_a == other.to_a
end
end

private

def active_enums(enums)
Expand Down
23 changes: 23 additions & 0 deletions spec/montrose/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c6ed63

Please sign in to comment.