Module: RSpec::Hal::Matchers::HalMatcherHelpers

Included in:
HavePropertyMatcher, RelationMatcher, TemplatedRelationMatcher
Defined in:
lib/rspec/hal/matchers/hal_matcher_helpers.rb

Constant Summary collapse

NullMatcher =
Class.new do
  def matches(*args)
    true
  end
  def ===(*args)
    true
  end
  def description
    ""
  end
end.new

Instance Method Summary collapse

Instance Method Details

#matcher?(obj) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 58

def matcher?(obj)
  obj.respond_to?(:matches?) and (obj.respond_to?(:failure_message) or
                                  obj.respond_to?(:failure_message_for_should))
end

#matcherize(expected) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 48

def matcherize(expected)
  if matcher? expected
    expected
  elsif expected.kind_of? Regexp
    RSpec::Matchers::BuiltIn::Match.new(expected)
  else
    RSpec::Matchers::BuiltIn::Eq.new(expected)
  end
end

#parse(jsonish) ⇒ Object

Returns HalClient::Representation of the provided jsonish thing.

jsonish - A HAL document (as a string or pre-parsed hash) or

an object that can be converted into one via its `#to_hal`
or `#to_json` methods.


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 34

def parse(jsonish)
  json = if jsonish.kind_of? String
           jsonish

         elsif jsonish.respond_to? :to_hal
           jsonish.to_hal

         else jsonish.respond_to? :to_json
           jsonish.to_json
         end

  HalClient::Representation.new(parsed_json: MultiJson.load(json))
end

#sentencize(*clauses) ⇒ Object

Returns string composed of the specified clauses with proper spacing between them. Empty and nil clauses are ignored.



19
20
21
22
23
24
25
26
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 19

def sentencize(*clauses)
  clauses
    .flatten
    .compact
    .reject(&:empty?)
    .map(&:strip)
    .join(" ")
end