Module: Qo::PublicApi
Overview
The Public API consists of methods that should be openly accessible to the top level Qo namespace, and should not change. It should be used as the subject of Acceptance level tests for the library and should not have its externally facing methods renamed or moved under pain of a look of profound disappointment from the creator.
Instance Method Summary collapse
-
#and(*array_matchers, **keyword_matchers) ⇒ Proc[Any]
(also: #[])
Creates an
and
type query matcher. -
#case(value, destructure: false, &fn) ⇒ Any
Similar to the common case statement of Ruby, except in that it behaves as if
Array#===
andHash#===
exist in the form of Qo matchers. -
#create_branch(name:, precondition: Any, extractor: IDENTITY, destructure: false, default: false) ⇒ Class
Dynamically creates a new branch to be used with custom pattern matchers.
-
#create_pattern_match(branches:) ⇒ Class
Creates a new type of pattern matcher from a set of branches.
-
#match(destructure: false, &fn) ⇒ Qo::PatternMatch
A pattern match will try and run all guard block style matchers in sequence until it finds one that "matches".
-
#not(*array_matchers, **keyword_matchers) ⇒ Proc[Any]
Creates a
not
type query matcher. -
#or(*array_matchers, **keyword_matchers) ⇒ Proc[Any]
Creates an
or
type query matcher. -
#result_case(target, destructure: false, &fn) ⇒ Any
Similar to
case
, except it uses aResultPatternMatch
instead. -
#result_match(destructure: false, &fn) ⇒ Proc[Any] => Any
Similar to
match
, except it uses aResultPatternMatch
which instead responds to tuple types:.
Instance Method Details
#and(*array_matchers, **keyword_matchers) ⇒ Proc[Any] Also known as: []
Creates an and
type query matcher. All conditions in this type of matcher
must pass to be considered a "match". It will short-circuit in the case of
a false match.
26 27 28 |
# File 'lib/qo/public_api.rb', line 26 def and(*array_matchers, **keyword_matchers) create_matcher('and', array_matchers, keyword_matchers) end |
#case(value, destructure: false, &fn) ⇒ Any
I refer to the potential 2.6+ features currently being discussed here:
Hash#===
- https://bugs.ruby-lang.org/issues/14869Array#===
- https://bugs.ruby-lang.org/issues/14916
Similar to the common case statement of Ruby, except in that it behaves
as if Array#===
and Hash#===
exist in the form of Qo matchers.
121 122 123 |
# File 'lib/qo/public_api.rb', line 121 def case(value, destructure: false, &fn) Qo::PatternMatchers::PatternMatch.new(destructure: destructure, &fn).call(value) end |
#create_branch(name:, precondition: Any, extractor: IDENTITY, destructure: false, default: false) ⇒ Class
Dynamically creates a new branch to be used with custom pattern matchers.
220 221 222 223 224 225 226 227 228 |
# File 'lib/qo/public_api.rb', line 220 def create_branch(name:, precondition: Any, extractor: IDENTITY, destructure: false, default: false) Qo::Branches::Branch.create( name: name, precondition: precondition, extractor: extractor, destructure: destructure, default: default ) end |
#create_pattern_match(branches:) ⇒ Class
Creates a new type of pattern matcher from a set of branches
237 238 239 |
# File 'lib/qo/public_api.rb', line 237 def create_pattern_match(branches:) Qo::PatternMatchers::PatternMatch.create(branches: branches) end |
#match(destructure: false, &fn) ⇒ Qo::PatternMatch
A pattern match will try and run all guard block style matchers in sequence until it finds one that "matches". Once found, it will pass the target into the associated matcher's block function.
85 86 87 88 89 |
# File 'lib/qo/public_api.rb', line 85 def match(destructure: false, &fn) return proc { false } unless block_given? Qo::PatternMatchers::PatternMatch.new(destructure: destructure, &fn) end |
#not(*array_matchers, **keyword_matchers) ⇒ Proc[Any]
Creates a not
type query matcher. No conditions in this type of matcher
should pass to be considered a "match". It will short-circuit in the case of
a true match.
61 62 63 |
# File 'lib/qo/public_api.rb', line 61 def not(*array_matchers, **keyword_matchers) create_matcher('not', array_matchers, keyword_matchers) end |
#or(*array_matchers, **keyword_matchers) ⇒ Proc[Any]
Creates an or
type query matcher. Any conditions in this type of matcher
must pass to be considered a "match". It will short-circuit in the case of
a true match.
45 46 47 |
# File 'lib/qo/public_api.rb', line 45 def or(*array_matchers, **keyword_matchers) create_matcher('or', array_matchers, keyword_matchers) end |
#result_case(target, destructure: false, &fn) ⇒ Any
Similar to case
, except it uses a ResultPatternMatch
instead.
178 179 180 181 182 |
# File 'lib/qo/public_api.rb', line 178 def result_case(target, destructure: false, &fn) Qo::PatternMatchers::ResultPatternMatch .new(destructure: destructure, &fn) .call(target) end |
#result_match(destructure: false, &fn) ⇒ Proc[Any] => Any
Similar to match
, except it uses a ResultPatternMatch
which instead
responds to tuple types:
154 155 156 157 158 |
# File 'lib/qo/public_api.rb', line 154 def result_match(destructure: false, &fn) return proc { false } unless block_given? Qo::PatternMatchers::ResultPatternMatch.new(destructure: destructure, &fn) end |