Class: JSONAPI::Resources::Matchers::HaveMany
- Inherits:
-
Object
- Object
- JSONAPI::Resources::Matchers::HaveMany
- Defined in:
- lib/jsonapi/resources/matchers/have_many.rb
Instance Attribute Summary collapse
-
#expected_class_name ⇒ Object
Returns the value of attribute expected_class_name.
-
#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
- #has_key_in_relationships? ⇒ Boolean
-
#initialize(name) ⇒ HaveMany
constructor
A new instance of HaveMany.
- #matches?(resource) ⇒ Boolean
- #matches_class_name? ⇒ Boolean
- #with_class_name(name) ⇒ Object
Constructor Details
#initialize(name) ⇒ HaveMany
Returns a new instance of HaveMany.
8 9 10 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 8 def initialize(name) self.name = name end |
Instance Attribute Details
#expected_class_name ⇒ Object
Returns the value of attribute expected_class_name.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6 def expected_class_name @expected_class_name end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6 def name @name end |
#resource ⇒ Object
Returns the value of attribute resource.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6 def resource @resource end |
Instance Method Details
#description ⇒ Object
12 13 14 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 12 def description "have many `#{name}`" end |
#failure_message ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 36 def resource_name = resource.class.name.demodulize = ["expected `#{resource_name}` to have many `#{name}`"] if self.expected_class_name << "with class name `#{self.expected_class_name}`" end .join(" ") end |
#has_key_in_relationships? ⇒ Boolean
22 23 24 25 26 27 28 29 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 22 def has_key_in_relationships? serialized_hash = JSONAPI::ResourceSerializer.new(resource.class). serialize_to_hash(resource).with_indifferent_access expected_key = name.to_s.dasherize relationships = serialized_hash["data"]["relationships"] return false if relationships.nil? relationships.has_key?(expected_key) end |
#matches?(resource) ⇒ Boolean
16 17 18 19 20 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 16 def matches?(resource) self.resource = resource has_key_in_relationships? && matches_class_name? end |
#matches_class_name? ⇒ Boolean
45 46 47 48 49 50 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 45 def matches_class_name? return true if self.expected_class_name.nil? association = resource.class._relationships[name] actual_class_name = association.class_name self.expected_class_name == actual_class_name end |
#with_class_name(name) ⇒ Object
31 32 33 34 |
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 31 def with_class_name(name) self.expected_class_name = name self end |