Class: Ramcrest::HasAttribute::Matcher

Inherits:
Object
  • Object
show all
Includes:
Matcher
Defined in:
lib/ramcrest/has_attribute.rb

Instance Method Summary collapse

Methods included from Matcher

#do_match, #mismatch, #success

Constructor Details

#initialize(attribute_name, value_matcher) ⇒ Matcher

Returns a new instance of Matcher.



15
16
17
18
# File 'lib/ramcrest/has_attribute.rb', line 15

def initialize(attribute_name, value_matcher)
  @attribute_name = attribute_name
  @value_matcher = value_matcher
end

Instance Method Details

#descriptionObject



33
34
35
# File 'lib/ramcrest/has_attribute.rb', line 33

def description
  "an object with an attribute named '#{@attribute_name}' and value #{@value_matcher.description}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ramcrest/has_attribute.rb', line 20

def matches?(actual)
  if actual.respond_to?(@attribute_name)
    match = @value_matcher.matches?(actual.send(@attribute_name))
    if match.matched?
      return success
    else
      return mismatch("object <#{actual}> attribute '#{@attribute_name}' #{match.description}")
    end
  else
    return mismatch("object <#{actual}> was missing attribute '#{@attribute_name}'")
  end
end