Class: JSONAPI::CreateResourceOperation
- Defined in:
- lib/jsonapi/operation.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Attributes inherited from Operation
Instance Method Summary collapse
- #apply(context) ⇒ Object
-
#initialize(resource_klass, values = {}) ⇒ CreateResourceOperation
constructor
A new instance of CreateResourceOperation.
Constructor Details
#initialize(resource_klass, values = {}) ⇒ CreateResourceOperation
Returns a new instance of CreateResourceOperation.
17 18 19 20 |
# File 'lib/jsonapi/operation.rb', line 17 def initialize(resource_klass, values = {}) @values = values super(resource_klass) end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
15 16 17 |
# File 'lib/jsonapi/operation.rb', line 15 def values @values end |
Instance Method Details
#apply(context) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/jsonapi/operation.rb', line 22 def apply(context) resource = @resource_klass.create(context) resource.replace_fields(@values) return JSONAPI::OperationResult.new(:created, resource) rescue JSONAPI::Exceptions::Error => e return JSONAPI::OperationResult.new(e.errors[0].code, nil, e.errors) end |