Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom comparison for Montrose::{Schedule/Recurrence} #148

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/montrose/recurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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)
to_a == other.to_a
else
super
end
end

private

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