Class: Qo::Matchers::BaseMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/qo/matchers/base_matcher.rb

Overview

Base instance of matcher which is meant to take in either Array style or Keyword style arguments to run a match against various datatypes.

Will delegate responsibilities to either Array or Hash style matchers if invoked directly.

Direct Known Subclasses

ArrayMatcher, GuardBlockMatcher, HashMatcher

Instance Method Summary collapse

Constructor Details

#initialize(type, *array_matchers, **keyword_matchers) ⇒ BaseMatcher



12
13
14
15
16
# File 'lib/qo/matchers/base_matcher.rb', line 12

def initialize(type, *array_matchers, **keyword_matchers)
  @array_matchers   = array_matchers
  @keyword_matchers = keyword_matchers
  @type             = type
end

Instance Method Details

#call(target) ⇒ type Also known as: ===, []

You can directly call a matcher as well, much like a Proc, using one of call, ===, or []



35
36
37
# File 'lib/qo/matchers/base_matcher.rb', line 35

def call(target)
  self.to_proc.call(target)
end

#to_procProc

Converts a Matcher to a proc for use in querying, such as:

data.select(&Qo[...])


23
24
25
26
27
# File 'lib/qo/matchers/base_matcher.rb', line 23

def to_proc
  @array_matchers.empty? ?
    Qo::Matchers::HashMatcher.new(@type, **@keyword_matchers).to_proc :
    Qo::Matchers::ArrayMatcher.new(@type, *@array_matchers).to_proc
end