Class: FunWithJsonApi::SchemaValidators::CheckRelationships

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/schema_validators/check_relationships.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, deserializer) ⇒ CheckRelationships

Returns a new instance of CheckRelationships.



13
14
15
16
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 13

def initialize(document, deserializer)
  @document = document
  @deserializer = deserializer
end

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



11
12
13
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 11

def deserializer
  @deserializer
end

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 10

def document
  @document
end

Class Method Details

.call(document, deserializer) ⇒ Object



6
7
8
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 6

def self.call(document, deserializer)
  new(document, deserializer).call
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 18

def call
  relationships = document['data'].fetch('relationships', {})

  CheckRelationshipNames.call(document, deserializer, relationships.keys)

  check_for_invalid_relationship_type! relationships

  true
end

#check_for_invalid_relationship_type!(relationships_hash) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 28

def check_for_invalid_relationship_type!(relationships_hash)
  payload = build_invalid_relationship_type_payload(relationships_hash)
  return if payload.empty?

  message = 'A relationship received data with an incorrect type'
  raise FunWithJsonApi::Exceptions::InvalidRelationshipType.new(message, payload)
end

#check_for_invalid_relationship_type_in_collection!(relationship, collection_data) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 36

def check_for_invalid_relationship_type_in_collection!(relationship, collection_data)
  return unless collection_data.is_a?(Array)

  collection_data.each_with_index.map do |item, index|
    next if item['type'] == relationship.type

    build_invalid_collection_item_payload(relationship, index)
  end
end

#check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data) ⇒ Object



46
47
48
49
50
51
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 46

def check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data)
  return unless relationship_data.is_a?(Hash)
  return if relationship_data['type'] == relationship.type

  build_invalid_relationship_item_payload(relationship)
end