Class: Hyperion::Kim::Matcher

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(func = nil, &block) ⇒ Matcher

Returns a new instance of Matcher.



15
16
17
# File 'lib/hyperion_test/kim/matcher.rb', line 15

def initialize(func=nil, &block)
  @func = Matcher.wrap(block || func)
end

Instance Attribute Details

#funcObject (readonly)

Fancy predicates for HTTP requests. Features:

  • and/or/not combinators

  • If a predicate raises an error, it is caught and treated as falsey. simplifies predicates. For example: headers.starts_with?(‘application/’) will raise if no Allow header was sent, however we really just want to treat that as a non-match.

  • Parameter extraction. A matcher can return an augmented Request as the truthy value.



13
14
15
# File 'lib/hyperion_test/kim/matcher.rb', line 13

def func
  @func
end

Class Method Details

.and(*ms) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/hyperion_test/kim/matcher.rb', line 41

def self.and(*ms)
  m, *rest = ms
  if rest.empty?
    m
  else
    m.and(Matcher.and(*rest))
  end
end

Instance Method Details

#and(other) ⇒ Object



23
24
25
26
27
# File 'lib/hyperion_test/kim/matcher.rb', line 23

def and(other)
  Matcher.new do |req|
    (req2 = @func.call(req)) && other.call(req2)
  end
end

#call(req) ⇒ Object



19
20
21
# File 'lib/hyperion_test/kim/matcher.rb', line 19

def call(req)
  @func.call(req)
end

#notObject



35
36
37
38
39
# File 'lib/hyperion_test/kim/matcher.rb', line 35

def not
  Matcher.new do |req|
    @func.call(req) ? nil : req
  end
end

#or(other) ⇒ Object



29
30
31
32
33
# File 'lib/hyperion_test/kim/matcher.rb', line 29

def or(other)
  Matcher.new do |req|
    @func.call(req) || other.call(req)
  end
end