Class: JSONAPI::Resources::Matchers::HaveAttribute
- Inherits:
-
Object
- Object
- JSONAPI::Resources::Matchers::HaveAttribute
- Defined in:
- lib/jsonapi/resources/matchers/have_attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
-
#initialize(name) ⇒ HaveAttribute
constructor
A new instance of HaveAttribute.
- #matches?(resource) ⇒ Boolean
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
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 6 def name @name end |
#resource ⇒ Object
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
#description ⇒ Object
30 31 32 |
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 30 def description "have attribute #{name}" end |
#failure_message ⇒ Object
25 26 27 28 |
# File 'lib/jsonapi/resources/matchers/have_attribute.rb', line 25 def resource_name = resource.class.name.demodulize %Q(expected #{resource_name} to have attribute #{name}) end |
#matches?(resource) ⇒ 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 |