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.

Author:

  • baweaver

Since:

  • 0.2.0

Direct Known Subclasses

ArrayMatcher, GuardBlockMatcher, HashMatcher

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BaseMatcher

Since:

  • 0.2.0



25
26
27
28
29
# File 'lib/qo/matchers/base_matcher.rb', line 25

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

Instance Method Details

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

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

Parameters:

  • target (Any)

    Object to match against

Returns:

  • (Boolean)

    Result of the match

Since:

  • 0.2.0



48
49
50
# File 'lib/qo/matchers/base_matcher.rb', line 48

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

#to_procProc[Any]

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

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

Returns:

  • (Proc[Any])

Since:

  • 0.2.0



36
37
38
39
40
# File 'lib/qo/matchers/base_matcher.rb', line 36

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