Class: JSONAPI::Resources::Matchers::Relationship
- Inherits:
-
Object
- Object
- JSONAPI::Resources::Matchers::Relationship
- Defined in:
- lib/jsonapi/resources/matchers/relationship.rb
Instance Attribute Summary collapse
-
#expected_class_name ⇒ Object
Returns the value of attribute expected_class_name.
-
#expected_relation_name ⇒ Object
Returns the value of attribute expected_relation_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#relationship_type ⇒ Object
Returns the value of attribute relationship_type.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #has_key_in_relationships? ⇒ Boolean
- #humanized_relationship_type ⇒ Object
-
#initialize(relationship_type, name) ⇒ Relationship
constructor
A new instance of Relationship.
- #matches?(resource) ⇒ Boolean
- #matches_class_name? ⇒ Boolean
- #matches_relation_name? ⇒ Boolean
- #with_class_name(name) ⇒ Object
- #with_relation_name(name) ⇒ Object
Constructor Details
#initialize(relationship_type, name) ⇒ Relationship
Returns a new instance of Relationship.
12 13 14 15 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 12 def initialize(relationship_type, name) self.relationship_type = relationship_type 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/relationship.rb', line 6 def expected_class_name @expected_class_name end |
#expected_relation_name ⇒ Object
Returns the value of attribute expected_relation_name.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 6 def expected_relation_name @expected_relation_name end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 6 def name @name end |
#relationship_type ⇒ Object
Returns the value of attribute relationship_type.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 6 def relationship_type @relationship_type end |
#resource ⇒ Object
Returns the value of attribute resource.
6 7 8 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 6 def resource @resource end |
Instance Method Details
#description ⇒ Object
17 18 19 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 17 def description "#{humanized_relationship_type} `#{name}`" end |
#failure_message ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 52 def resource_name = resource.class.name.demodulize = ["expected `#{resource_name}` to #{humanized_relationship_type} `#{name}`"] if self.expected_class_name << "with class name `#{self.expected_class_name}`" end if self.expected_relation_name << "with relation name `#{self.expected_relation_name}`" end .join(" ") end |
#has_key_in_relationships? ⇒ Boolean
33 34 35 36 37 38 39 40 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 33 def has_key_in_relationships? serialized_hash = JSONAPI::ResourceSerializer.new(resource.class). serialize_to_hash(resource).with_indifferent_access expected_key = JSONAPI.configuration.key_formatter.format(name.to_s) relationships = serialized_hash["data"]["relationships"] return false if relationships.nil? relationships.has_key?(expected_key) end |
#humanized_relationship_type ⇒ Object
21 22 23 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 21 def humanized_relationship_type relationship_type.to_s.gsub('_',' ') end |
#matches?(resource) ⇒ Boolean
25 26 27 28 29 30 31 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 25 def matches?(resource) self.resource = resource has_key_in_relationships? && matches_class_name? && matches_relation_name? end |
#matches_class_name? ⇒ Boolean
64 65 66 67 68 69 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 64 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 |
#matches_relation_name? ⇒ Boolean
71 72 73 74 75 76 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 71 def matches_relation_name? return true if self.expected_relation_name.nil? association = resource.class._relationships[name] actual_relation_name = association.relation_name({}) self.expected_relation_name == actual_relation_name end |
#with_class_name(name) ⇒ Object
42 43 44 45 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 42 def with_class_name(name) self.expected_class_name = name self end |
#with_relation_name(name) ⇒ Object
47 48 49 50 |
# File 'lib/jsonapi/resources/matchers/relationship.rb', line 47 def with_relation_name(name) self.expected_relation_name = name self end |