Class: JSONAPI::Resources::Matchers::HaveAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/resources/matchers/have_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HaveAttribute

Returns a new instance of HaveAttribute.



8
9
10
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 8

def initialize(name)
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 6

def name
  @name
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 6

def resource
  @resource
end

Instance Method Details

#descriptionObject



30
31
32
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 30

def description
  "have attribute #{name}"
end

#failure_messageObject



25
26
27
28
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 25

def failure_message
  resource_name = resource.class.name.demodulize
  %Q(expected #{resource_name} to have attribute #{name})
end

#matches?(resource) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 12

def matches?(resource)
  self.resource = resource

  resource_class = resource.class

  serialized_hash = JSONAPI::ResourceSerializer.new(resource_class).
    serialize_to_hash(resource).with_indifferent_access
  expected_key = JSONAPI.configuration.key_formatter.format(name.to_s)
  attributes = serialized_hash["data"]["attributes"]
  return false if attributes.nil?
  attributes.has_key?(expected_key)
end