Skip to content

Commit

Permalink
Add custom comparison for Montrose::Recurrence
Browse files Browse the repository at this point in the history
In the same vein as adding `Montrose::Schedule#==`

This allows `Montrose::Recurrence` objects to be compared by their
underlying hash options.
  • Loading branch information
thewatts committed Oct 24, 2021
1 parent fb927bb commit ffa86a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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.respond_to?(:to_hash)
to_hash == other.to_hash
else
super
end
end

private

def event_enum
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

0 comments on commit ffa86a8

Please sign in to comment.