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

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

Constant Summary collapse

NullMatcher =

A matcher that always matches. Useful for avoiding special cases in value testing logic.

Class.new do
  def matches?(*args)
    true
  end

  def failure_message
    ""
  end
  alias_method :failure_message_for_should, :failure_message

  def description
    ""
  end

  def ===(*args)
    true
  end

end.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reprObject

Returns the value of attribute repr.



34
35
36
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 34

def repr
  @repr
end

Instance Method Details

#matcher?(obj) ⇒ Boolean

Returns true if object is an RSpec matcher

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 83

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

Returns ‘expected` coerced into an RSpec matcher



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 70

def matcherize(expected)
  if matcher? expected
    expected

  elsif expected.respond_to? :===
    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.


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 55

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.



40
41
42
43
44
45
46
47
# File 'lib/rspec/hal/matchers/hal_matcher_helpers.rb', line 40

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