Module: Qo::PatternMatchers::Branching::ClassMethods

Defined in:
lib/qo/pattern_matchers/branching.rb

Overview

Class methods to extend the including class with

Author:

  • baweaver

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#register_branch(branch) ⇒ Object

Registers a branch to a pattern matcher.

This defines a method on the pattern matcher matching the name of the branch. If the name is where, the pattern matcher will now be given a method called where with which to match with.

When called, this will either ammend a matcher to the list of matchers or set a default matcher if the branch happens to be a default.

Parameters:

  • branch (Branch)

    Branch object to register with a pattern matcher

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qo/pattern_matchers/branching.rb', line 34

def register_branch(branch)
  define_method(branch.name) do |*conditions, **keyword_conditions, &function|
    qo_matcher = Qo::Matchers::Matcher.new('and', conditions, keyword_conditions)

    branch_matcher = branch.create_matcher(
      qo_matcher, destructure: @destructure, &function
    )

    if branch.default?
      @default = branch_matcher
    else
      @matchers << branch_matcher
    end
  end
end