Class: JsonapiCompliable::Util::ValidationResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/util/validation_response.rb

Overview

We need to know two things in the response of a persistence call:

* The model we just tried to persist
* Was the persistence successful?

This object wraps those bits of data. The call is considered unsuccessful when it adheres to the ActiveModel#errors interface, and #errors is not blank. In other words, it is not successful if there were validation errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, deserialized_params) ⇒ ValidationResponse

Returns a new instance of ValidationResponse

Parameters:

  • object

    the model instance we tried to save

  • deserialized_params

    see Base#deserialized_params



17
18
19
20
# File 'lib/jsonapi_compliable/util/validation_response.rb', line 17

def initialize(object, deserialized_params)
  @object = object
  @deserialized_params = deserialized_params
end

Instance Attribute Details

#objectObject (readonly)

the object we are saving

Returns:

  • (Object)

    the current value of object



12
13
14
# File 'lib/jsonapi_compliable/util/validation_response.rb', line 12

def object
  @object
end

Instance Method Details

#success?Boolean

Check to ensure no validation errors.

Returns:

  • (Boolean)

    did the persistence call succeed?



24
25
26
# File 'lib/jsonapi_compliable/util/validation_response.rb', line 24

def success?
  all_valid?(object, @deserialized_params.relationships)
end

#to_aArray

Returns the object and success state

Returns:

  • (Array)

    the object and success state



29
30
31
# File 'lib/jsonapi_compliable/util/validation_response.rb', line 29

def to_a
  [object, success?]
end