Skip to content

Commit

Permalink
Merge pull request #25 from danielma/dma/schedule-pattern-match
Browse files Browse the repository at this point in the history
Support Ruby 2.7+ Pattern Matching
  • Loading branch information
molawson authored Apr 25, 2024
2 parents c53085d + 3e595c4 commit fcc1fc0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### Unreleased

Changes:

* Support pattern matching for `Expression` and `Schedule`

[Commits](https://github.com/molawson/repeatable/compare/v1.1.0...main)

### 1.1.0 (2022-02-25)
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ schedule.to_h
# can be used to recreate an identical Schedule object at a later time
```

#### Pattern Matching

Both `Repeatable::Schedule` and all `Repeatable::Expression` classes support Ruby 2.7+ [pattern matching][ruby-pattern-matching] which is particularly useful for parsing or presenting an existing schedule.

```ruby
case schedule
in weekday: { weekday: }
"Weekly on #{Date::DAYNAMES[weekday]}"
in day_in_month: { day: }
"Every month on the #{day.ordinalize}"
in weekday_in_month: { weekday:, count: }
"Every month on the #{count.ordinalize} #{Date::DAYNAMES[weekday]}"
end
```

#### Equivalence

Both `Repeatable::Schedule` and all `Repeatable::Expression` classes have equivalence `#==` defined according to what's appropriate for each class, so regardless of the order of arguments passed to each, you can tell whether one object is equivalent to the other in terms of whether or not, when asked the same questions, you'd receive the same results from each.
Expand Down Expand Up @@ -218,3 +233,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
## Code of Conduct

Everyone interacting in the Repeatable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/molawson/repeatable/blob/main/CODE_OF_CONDUCT.md).

[ruby-pattern-matching]: https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html
5 changes: 5 additions & 0 deletions lib/repeatable/expression/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def to_h
{hash_key => hash_value}
end

sig { params(_keys: T.nilable(T::Array[Symbol])).returns(T::Hash[Symbol, T.any(Types::SymbolHash, T::Array[Types::SymbolHash])]) }
def deconstruct_keys(_keys)
to_h
end

sig { params(other: Expression::Base).returns(Expression::Union) }
def union(other)
Union.new(self, other)
Expand Down
5 changes: 5 additions & 0 deletions lib/repeatable/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def to_h
expression.to_h
end

sig { params(_keys: T.nilable(T::Array[Symbol])).returns(T::Hash[Symbol, T.any(Types::SymbolHash, T::Array[Types::SymbolHash])]) }
def deconstruct_keys(_keys)
to_h
end

sig { params(other: Object).returns(T::Boolean) }
def ==(other)
other.is_a?(self.class) && expression == other.expression
Expand Down
8 changes: 8 additions & 0 deletions spec/repeatable/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ module Repeatable
end
end

describe "pattern matching" do
let(:arg) { nested_set_expression_hash }

it "can #deconstruct_keys" do
expect(subject.deconstruct_keys(nil)).to eq subject.to_h
end
end

describe "#==" do
it "returns true if the schedules have the same identity" do
schedule = described_class.new(nested_set_expression_object)
Expand Down
6 changes: 6 additions & 0 deletions spec/support/expression_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
expect { subject.to_h }.not_to raise_error
end
end

describe "pattern matching" do
it "can #deconstruct_keys" do
expect(subject.deconstruct_keys(nil)).to be_a(Hash)
end
end
end

0 comments on commit fcc1fc0

Please sign in to comment.