Class: SumsUp::Core::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/sums_up/core/matcher.rb

Overview

Matching DSL for sum type variants. Methods in this class are prefixed with an _ so as not to conflict with the names of user-defined variant names. Use .build_matcher_class to generate a new subclass for a variant given the other variant names.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant_instance) ⇒ Matcher

Returns a new instance of Matcher.



37
38
39
40
41
42
43
44
# File 'lib/sums_up/core/matcher.rb', line 37

def initialize(variant_instance)
  @variant_instance = variant_instance

  @matched = false
  @matched_variants = []
  @wildcard_matched = false
  @result = nil
end

Class Method Details

.build_matcher_class(variant, other_variants) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sums_up/core/matcher.rb', line 10

def self.build_matcher_class(variant, other_variants)
  Class.new(self) do
    const_set(:VARIANT, variant)
    const_set(:ALL_VARIANTS, [variant, *other_variants].freeze)
    const_set(:IncorrectMatcher, incorrect_matcher_module(other_variants))

    include(const_get(:IncorrectMatcher))

    alias_method(variant, :_correct_variant_matcher)
  end
end

.incorrect_matcher_module(variants) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sums_up/core/matcher.rb', line 22

def self.incorrect_matcher_module(variants)
  Module.new do
    variants.each do |variant|
      define_method(variant) do |_value = nil|
        _ensure_wildcard_not_matched!(variant)
        _ensure_no_duplicate_match!(variant)

        @matched_variants << variant

        self
      end
    end
  end
end

Instance Method Details

#_(value = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sums_up/core/matcher.rb', line 46

def _(value = nil)
  _ensure_wildcard_not_matched!(:_)

  @wildcard_matched = true

  return self if @matched

  @result = block_given? ? yield(@variant_instance) : value

  self
end

#_correct_variant_matcher(value = nil) ⇒ Object

Defining #_correct_variant_matcher “statically” allows us to use yield instead of block.call, which is much faster in most ruby versions. github.com/JuanitoFatas/fast-ruby/blob/256fa4916b577befb40ba5ffaa22af08dc16565c/README.md#proc–block



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sums_up/core/matcher.rb', line 96

def _correct_variant_matcher(value = nil)
  variant = self.class::VARIANT

  _ensure_wildcard_not_matched!(variant)
  _ensure_no_duplicate_match!(variant)

  @matched_variants << variant
  @matched = true

  @result =
    if block_given?
      yield(*@variant_instance.members(dup: false))
    else
      value
    end

  self
end

#_fetch_resultObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sums_up/core/matcher.rb', line 79

def _fetch_result
  variants = self.class::ALL_VARIANTS

  return @result if @wildcard_matched
  return @result if @matched_variants.length == variants.length

  unmatched_variants = (variants - @matched_variants).join(", ")

  raise(
    UnmatchedVariantError,
    "Did not match the following variants: #{unmatched_variants}"
  )
end

#_match_hash(hash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sums_up/core/matcher.rb', line 58

def _match_hash(hash)
  variants = self.class::ALL_VARIANTS
  unknown_variants = hash
    .each_key
    .reject { |key| (key == :_) || variants.include?(key) }

  if unknown_variants.any?
    raise(
      UnknownVariantError,
      "Unknown variant(s): #{unknown_variants.join(", ")}; " \
      "valid variant(s) are: #{variants.join(", ")}"
    )
  end

  hash.each do |variant, value|
    public_send(variant, value)
  end

  self
end