Class: FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource
- Inherits:
-
Object
- Object
- FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource
- Defined in:
- lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb
Instance Attribute Summary collapse
-
#schema_validator ⇒ Object
readonly
Returns the value of attribute schema_validator.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #check_resource_id_can_be_client_generated ⇒ Object
- #check_resource_id_has_not_already_been_used ⇒ Object
- #check_resource_id_is_a_string ⇒ Object
- #check_resource_id_matches_document_id ⇒ Object
-
#initialize(schema_validator) ⇒ CheckDocumentIdMatchesResource
constructor
A new instance of CheckDocumentIdMatchesResource.
Constructor Details
#initialize(schema_validator) ⇒ CheckDocumentIdMatchesResource
Returns a new instance of CheckDocumentIdMatchesResource.
16 17 18 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 16 def initialize(schema_validator) @schema_validator = schema_validator end |
Instance Attribute Details
#schema_validator ⇒ Object (readonly)
Returns the value of attribute schema_validator.
8 9 10 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 8 def schema_validator @schema_validator end |
Class Method Details
.call(schema_validator) ⇒ Object
4 5 6 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 4 def self.call(schema_validator) new(schema_validator).call end |
Instance Method Details
#call ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 20 def call if resource.try(:persisted?) # Ensure correct update document is being sent check_resource_id_is_a_string check_resource_id_matches_document_id elsif document_id # Ensure correct create document is being sent check_resource_id_is_a_string check_resource_id_can_be_client_generated check_resource_id_has_not_already_been_used end end |
#check_resource_id_can_be_client_generated ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 55 def check_resource_id_can_be_client_generated # Ensure id has been provided as an attribute if deserializer.attributes.none? { |attribute| attribute.name == :id } deserializer_name = deserializer.class.name || 'Deserializer' = "id parameter for '#{resource_type}' cannot be set"\ " as it has not been defined as a #{deserializer_name} attribute" payload = ExceptionPayload.new( detail: ) raise Exceptions::IllegalClientGeneratedIdentifier.new(, payload) end end |
#check_resource_id_has_not_already_been_used ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 68 def check_resource_id_has_not_already_been_used if (existing = deserializer.load_resource_from_id_value(document_id)) deserializer_class = deserializer.class.name || 'Deserializer' = "#{deserializer_class}#load_resource_from_id_value for '#{resource_type}' has"\ ' found a existing resource matching document id'\ ": #{existing.class.name}##{existing.id}" payload = ExceptionPayload.new( detail: ) raise Exceptions::InvalidClientGeneratedIdentifier.new(, payload) end end |
#check_resource_id_is_a_string ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 33 def check_resource_id_is_a_string unless document_id.is_a?(String) payload = ExceptionPayload.new( detail: , pointer: '/data/id' ) = "document id is not a string: #{document_id.class.name}" raise Exceptions::InvalidDocumentIdentifier.new(, payload) end end |
#check_resource_id_matches_document_id ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 44 def check_resource_id_matches_document_id if document_id != resource_id = "resource id '#{resource_id}' does not match the expected id for"\ " '#{resource_type}': '#{document_id}'" payload = ExceptionPayload.new( detail: ) raise Exceptions::InvalidDocumentIdentifier.new(, payload) end end |