Class: Qo::Matchers::PatternMatch
- Inherits:
-
Object
- Object
- Qo::Matchers::PatternMatch
- Defined in:
- lib/qo/matchers/pattern_match.rb
Overview
Creates a PatternMatch, which will evaluate once given a match target.
Each GuardBlockMatcher given will be run in sequence until a “match” is located. Once that condition is met, it will call the associated block function of that GuardBlockMatcher with the match target.
This is done as an effort to emulate Right Hand Assignment seen in other functionally oriented languages pattern matching systems. Most notably this is done in Scala: (docs.scala-lang.org/tour/pattern-matching.html)
“‘scala notification match
case Email(email, title, _) =>
s"You got an email from $email with title: $title"
case SMS(number, ) =>
s"You got an SMS from $number! Message: $message"
case VoiceRecording(name, link) =>
s"You received a Voice Recording from $name! Click the link to hear it: $link"
case other => other
“‘
Qo will instead pipe the entire matched object, and might look something like this:
“‘ruby # Assuming notification is in the form of a tuple
Qo.match(notification,
Qo.m(EMAIL_REGEX, String, :*) { |email, title, _|
"You got an email from #{email} with title: #{title}"
},
Qo.m(PHONE_REGEX, String) { |number, , _|
"You got an SMS from #{number}! Message: #{message}"
},
Qo.m(String, LINK_REGEX) { |name, link, _|
"You received a Voice Recording from #{name}! Click the link to hear it: #{link}"
},
Qo.m(:*)
) “‘
Efforts to emulate the case class mechanic may be present in later versions, but for now the associated performance penalty may be too steep to consider.
We’ll evaluate those options in a few experiments later.
Instance Method Summary collapse
-
#call(target) ⇒ Any | nil
Immediately invokes a PatternMatch.
-
#initialize(*matchers) ⇒ PatternMatch
constructor
A new instance of PatternMatch.
-
#to_proc ⇒ Proc
Function return of a PatternMatch waiting for a target to run.
Constructor Details
#initialize(*matchers) ⇒ PatternMatch
Returns a new instance of PatternMatch.
60 61 62 63 64 65 66 |
# File 'lib/qo/matchers/pattern_match.rb', line 60 def initialize(*matchers) raise Qo::Exceptions::NotAllGuardMatchersProvided unless matchers.all? { |q| q.is_a?(Qo::Matchers::GuardBlockMatcher) } @matchers = matchers end |
Instance Method Details
#call(target) ⇒ Any | nil
Immediately invokes a PatternMatch
83 84 85 86 87 88 89 90 |
# File 'lib/qo/matchers/pattern_match.rb', line 83 def call(target) @matchers.each { |guard_block_matcher| did_match, match_result = guard_block_matcher.call(target) return match_result if did_match } nil end |
#to_proc ⇒ Proc
Function return of a PatternMatch waiting for a target to run
72 73 74 |
# File 'lib/qo/matchers/pattern_match.rb', line 72 def to_proc Proc.new { |target| self.call(target) } end |