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/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/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} 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