Class: RSpec::Dry::Struct::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/dry/struct/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, attr_type = nil) ⇒ Matcher

Returns a new instance of Matcher.



7
8
9
10
# File 'lib/rspec/dry/struct/matcher.rb', line 7

def initialize(attr_name, attr_type = nil)
  @attr_name = attr_name
  @attr_type = attr_type
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



5
6
7
# File 'lib/rspec/dry/struct/matcher.rb', line 5

def attr_name
  @attr_name
end

#attr_typeObject (readonly)

Returns the value of attribute attr_type.



5
6
7
# File 'lib/rspec/dry/struct/matcher.rb', line 5

def attr_type
  @attr_type
end

Instance Method Details

#descriptionObject



18
19
20
# File 'lib/rspec/dry/struct/matcher.rb', line 18

def description
  "have #{@attr_name.inspect} attribute"
end

#failure_messageObject



22
23
24
25
# File 'lib/rspec/dry/struct/matcher.rb', line 22

def failure_message
  return attr_not_found_message(@actual) if @actual_attribute.nil?
  attr_type_mismatch_message(@actual)
end

#failure_message_when_negatedObject



27
28
29
# File 'lib/rspec/dry/struct/matcher.rb', line 27

def failure_message_when_negated
  "#{unexpected_message(@actual)}, but it was found"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/rspec/dry/struct/matcher.rb', line 12

def matches?(actual)
  @actual = actual
  @actual_attribute = actual.schema[@attr_name]
  attr_present? && correct_attr_type?
end