Module: Qo

Defined in:
lib/qo.rb,
lib/qo/matcher.rb,
lib/qo/version.rb,
lib/qo/guard_block_matcher.rb

Defined Under Namespace

Classes: GuardBlockMatcher, Matcher

Constant Summary collapse

WILDCARD_MATCH =
:*
VERSION =
'0.1.10'

Class Method Summary collapse

Class Method Details

.and(*array_matchers, **keyword_matchers) ⇒ Object Also known as: []



59
60
61
# File 'lib/qo.rb', line 59

def and(*array_matchers, **keyword_matchers)
  Qo::Matcher.new('and', *array_matchers, **keyword_matchers)
end

.count_by(targets, &fn) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/qo.rb', line 84

def count_by(targets, &fn)
  fn ||= -> v { v }

  targets.each_with_object(Hash.new(0)) { |target, counts|
    counts[fn[target]] += 1
  }
end

.dig(path_map, expected_value) ⇒ Object

Utility functions. Consider placing these elsewhere.



75
76
77
78
79
80
81
82
# File 'lib/qo.rb', line 75

def dig(path_map, expected_value)
  -> hash {
    segments = path_map.split('.')

    expected_value === hash.dig(*segments) ||
    expected_value === hash.dig(*segments.map(&:to_sym))
  }
end

.match(target, *qo_matchers) ⇒ type

Takes a set of Guard Block matchers, runs each in sequence, then unfolds the response from the first passing block.

Parameters:

  • target (Any)

    Target object to run against

  • *qo_matchers (Array[GuardBlockMatcher])

    Collection of matchers to run

Returns:

  • (type)
    description


39
40
41
42
43
44
45
46
47
# File 'lib/qo.rb', line 39

def match(target, *qo_matchers)
  all_are_guards = qo_matchers.all? { |q| q.is_a?(Qo::GuardBlockMatcher) }
  raise 'Must patch Qo GuardBlockMatchers!' unless all_are_guards

  qo_matchers.reduce(nil) { |_, matcher|
    did_match, match_result = matcher.call(target)
    break match_result if did_match
  }
end

.match_fn(*qo_matchers) ⇒ Proc[Any]

Wraps match to allow it to be used in a points-free style like regular matchers.

Parameters:

Returns:

  • (Proc[Any])

    Any -> Any



55
56
57
# File 'lib/qo.rb', line 55

def match_fn(*qo_matchers)
  -> target { match(target, *qo_matchers) }
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



25
26
27
# File 'lib/qo.rb', line 25

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

.not(*array_matchers, **keyword_matchers) ⇒ Object



69
70
71
# File 'lib/qo.rb', line 69

def not(*array_matchers, **keyword_matchers)
  Qo::Matcher.new('not', *array_matchers, **keyword_matchers)
end

.or(*array_matchers, **keyword_matchers) ⇒ Object



65
66
67
# File 'lib/qo.rb', line 65

def or(*array_matchers, **keyword_matchers)
  Qo::Matcher.new('or', *array_matchers, **keyword_matchers)
end