Module: Qo::PublicApi

Includes:
Exceptions
Included in:
Qo
Defined in:
lib/qo/public_api.rb

Overview

The Public API consists of methods that should be openly accessible to the top level Qo namespace, and should not change. It should be used as the subject of Acceptance level tests for the library and should not have its externally facing methods renamed or moved under pain of a look of profound disappointment from the creator.

Author:

  • baweaver

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

#and(*array_matchers, **keyword_matchers) ⇒ Proc[Any] Also known as: []

Creates an and type query matcher. All conditions in this type of matcher must pass to be considered a "match". It will short-circuit in the case of a false match.

Parameters:

  • *array_matchers (Array)

    Array-like conditionals

  • **keyword_matchers (Hash)

    Keyword style conditionals

Returns:

  • (Proc[Any])

    Any -> Bool # Given a target, will return if it "matches"

Since:

  • 0.2.0



23
24
25
# File 'lib/qo/public_api.rb', line 23

def and(*array_matchers, **keyword_matchers)
  create_matcher('and', *array_matchers, **keyword_matchers)
end

#match(*args) ⇒ Qo::PatternMatch | Any

"Curried" function that waits for a target, or evaluates immediately if given one.

A PatternMatch will try and run all GuardBlock matchers in sequence until it finds one that "matches". Once found, it will pass the target into the associated matcher's block function.

Parameters:

  • *args (Array[Any, *GuardBlockMatcher])

    Collection of matchers to run, potentially prefixed by a target object

Returns:

  • (Qo::PatternMatch | Any)

    Returns a PatternMatch waiting for a target, or an evaluated PatternMatch response

Since:

  • 0.2.0



90
91
92
93
94
95
96
97
# File 'lib/qo/public_api.rb', line 90

def match(*args)
  if args.first.is_a?(Qo::Matchers::GuardBlockMatcher)
    Qo::Matchers::PatternMatch.new(*args)
  else
    match_target, *qo_matchers = args
    Qo::Matchers::PatternMatch.new(*qo_matchers).call(match_target)
  end
end

#matcher(*array_matchers, **keyword_matchers, &fn) ⇒ Proc[Any] Also known as: m

Creates a Guard Block matcher.

A guard block matcher is used to guard a function from running unless the left-hand matcher passes. Once called with a value, it will either return [false, false] or [true, Any].

This wrapping is done to preserve intended false or nil responses, and is unwrapped with match below.

Parameters:

  • *array_matchers (Array)

    varargs matchers

  • **keyword_matchers (Hash)

    kwargs matchers

  • &fn (Proc)

    Guarded function

Returns:

  • (Proc[Any])

    Any -> Proc[Any]

Since:

  • 0.2.0



71
72
73
# File 'lib/qo/public_api.rb', line 71

def matcher(*array_matchers, **keyword_matchers, &fn)
  Qo::Matchers::GuardBlockMatcher.new(*array_matchers, **keyword_matchers, &fn)
end

#not(*array_matchers, **keyword_matchers) ⇒ Proc[Any]

Creates a not type query matcher. No conditions in this type of matcher should pass to be considered a "match". It will short-circuit in the case of a true match.

Parameters:

  • *array_matchers (Array)

    Array-like conditionals

  • **keyword_matchers (Hash)

    Keyword style conditionals

Returns:

  • (Proc[Any])

    Any -> Bool # Given a target, will return if it "matches"

Since:

  • 0.2.0



52
53
54
# File 'lib/qo/public_api.rb', line 52

def not(*array_matchers, **keyword_matchers)
  create_matcher('not', *array_matchers, **keyword_matchers)
end

#or(*array_matchers, **keyword_matchers) ⇒ Proc[Any]

Creates an or type query matcher. Any conditions in this type of matcher must pass to be considered a "match". It will short-circuit in the case of a true match.

Parameters:

  • *array_matchers (Array)

    Array-like conditionals

  • **keyword_matchers (Hash)

    Keyword style conditionals

Returns:

  • (Proc[Any])

    Any -> Bool # Given a target, will return if it "matches"

Since:

  • 0.2.0



39
40
41
# File 'lib/qo/public_api.rb', line 39

def or(*array_matchers, **keyword_matchers)
  create_matcher('or', *array_matchers, **keyword_matchers)
end