Skip to content

Commit 0c6ed63

Browse files
committed
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.
1 parent 1c52ff3 commit 0c6ed63

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/montrose/schedule.rb

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def inspect
169169
"#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>"
170170
end
171171

172+
def ==(other)
173+
if !other.is_a?(self.class) && !other.is_a?(Array)
174+
super
175+
else
176+
to_a == other.to_a
177+
end
178+
end
179+
172180
private
173181

174182
def active_enums(enums)

spec/montrose/schedule_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,27 @@
337337
_(array).must_equal [{"every" => "month"}, {"every" => "day"}]
338338
end
339339
end
340+
341+
describe "#==" do
342+
before do
343+
schedule << {every: :month}
344+
schedule << {every: :day}
345+
end
346+
347+
it "compares two schedules against each other" do
348+
identical_other = new_schedule.tap do |schedule|
349+
schedule << {every: :month}
350+
schedule << {every: :day}
351+
end
352+
353+
different_other = new_schedule.tap do |schedule|
354+
schedule << {every: :month}
355+
schedule << {every: :day}
356+
schedule << {every: :year}
357+
end
358+
359+
_(schedule).must_equal identical_other
360+
_(schedule).wont_equal different_other
361+
end
362+
end
340363
end

0 commit comments

Comments
 (0)