Class: FunWithJsonApi::SchemaValidators::CheckRelationshipNames

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

Overview

Ensures all provided relationship names are known

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, deserializer, relationship_keys) ⇒ CheckRelationshipNames

Returns a new instance of CheckRelationshipNames.



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

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

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



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

def deserializer
  @deserializer
end

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/fun_with_json_api/schema_validators/check_relationship_names.rb', line 9

def document
  @document
end

#relationship_keysObject (readonly)

Returns the value of attribute relationship_keys.



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

def relationship_keys
  @relationship_keys
end

Class Method Details

.call(*args) ⇒ Object



5
6
7
# File 'lib/fun_with_json_api/schema_validators/check_relationship_names.rb', line 5

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fun_with_json_api/schema_validators/check_relationship_names.rb', line 19

def call
  unknown = relationship_keys.reject { |rel| resource_relationships.include?(rel) }
  return if unknown.empty?

  unauthorised_relationships = unknown.select do |relationship|
    known_relationships.include?(relationship)
  end
  if unauthorised_relationships.any?
    raise build_unauthorized_relationship_error(unauthorised_relationships)
  else
    raise build_unknown_relationship_error(unknown)
  end
end