Class: FunWithJsonApi::SchemaValidators::CheckAttributes

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, deserializer) ⇒ CheckAttributes

Returns a new instance of CheckAttributes.



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

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

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



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

def deserializer
  @deserializer
end

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/fun_with_json_api/schema_validators/check_attributes.rb', line 8

def document
  @document
end

Class Method Details

.call(document, deserializer) ⇒ Object



4
5
6
# File 'lib/fun_with_json_api/schema_validators/check_attributes.rb', line 4

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

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
# File 'lib/fun_with_json_api/schema_validators/check_attributes.rb', line 16

def call
  attributes = document['data'].fetch('attributes', {}).keys
  unknown = attributes.reject { |attribute| resource_attributes.include?(attribute) }

  return true if unknown.empty?

  raise build_unknown_attribute_error(unknown)
end

#resource_attributesObject



25
26
27
# File 'lib/fun_with_json_api/schema_validators/check_attributes.rb', line 25

def resource_attributes
  @resource_attributes ||= deserializer.attributes.map(&:name).map(&:to_s)
end