Class: Qo::Matcher

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

Direct Known Subclasses

GuardBlockMatcher

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Matcher.



3
4
5
6
7
# File 'lib/qo/matcher.rb', line 3

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

Instance Method Details

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

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

Parameters:

  • match_target (Any)

    Object to match against

Returns:

  • (type)
    description


28
29
30
# File 'lib/qo/matcher.rb', line 28

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

#to_procProc

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

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

Returns:

  • (Proc)


14
15
16
17
18
19
20
# File 'lib/qo/matcher.rb', line 14

def to_proc
  if @array_matchers.empty?
    match_against_hash(@keyword_matchers)
  else
    match_against_array(@array_matchers)
  end
end