Class: FunWithJsonApi::CollectionManager
- Inherits:
-
Object
- Object
- FunWithJsonApi::CollectionManager
- Defined in:
- lib/fun_with_json_api/collection_manager.rb
Overview
Abstract Handles updating a collection relationship. Override the ‘insert`, `remove` and `replace` methods, or provide a block that perfoms the desired action on the collection.
Instance Attribute Summary collapse
-
#deserializer ⇒ Object
readonly
Returns the value of attribute deserializer.
-
#parent_resource ⇒ Object
readonly
Returns the value of attribute parent_resource.
Instance Method Summary collapse
- #failure_message_for_resource(resource, failure_message_or_callable) ⇒ Object
-
#initialize(parent_resource, deserializer_class, deserializer_options) ⇒ CollectionManager
constructor
A new instance of CollectionManager.
-
#insert_record(_record) ⇒ Object
Inserts a single record into a collection Must return true for a successful update, or return false for any failures.
-
#insert_records(collection, failure_message_or_callable = nil) ⇒ Object
Inserts all records from a collection into the parent resource.
-
#remove_record(_record) ⇒ Object
Removes a single record into a collection Must return true for a successful update, or return false for any failures.
-
#remove_records(collection, failure_message_or_callable = nil) ⇒ Object
Removes all records from a collection into the parent resource.
-
#replace_all_records(_collection) ⇒ Object
Replaces all records.
Constructor Details
#initialize(parent_resource, deserializer_class, deserializer_options) ⇒ CollectionManager
Returns a new instance of CollectionManager.
11 12 13 14 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 11 def initialize(parent_resource, deserializer_class, ) @parent_resource = parent_resource @deserializer = deserializer_class.create() end |
Instance Attribute Details
#deserializer ⇒ Object (readonly)
Returns the value of attribute deserializer.
9 10 11 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 9 def deserializer @deserializer end |
#parent_resource ⇒ Object (readonly)
Returns the value of attribute parent_resource.
8 9 10 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 8 def parent_resource @parent_resource end |
Instance Method Details
#failure_message_for_resource(resource, failure_message_or_callable) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 75 def (resource, ) resource_id = deserializer.format_resource_id(resource) = if .respond_to?(:call) = .call(resource_id) end || (resource_id) end |
#insert_record(_record) ⇒ Object
Inserts a single record into a collection Must return true for a successful update, or return false for any failures
18 19 20 21 22 23 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 18 def insert_record(_record) raise build_relationship_not_supported_exception( "Override #{self.class.name}#insert_record", ) end |
#insert_records(collection, failure_message_or_callable = nil) ⇒ Object
Inserts all records from a collection into the parent resource
Will attempt to call ‘insert_record` for each item in the `collection`. If false is received for any `insert_record` call, an exception with a payload will be raised after all items have been iterated through
You need to reverse all changes made in the event of an exception, wrapping an an ActiveRecord::Base.transaction block will usually work
Action is not supported unless ‘insert_records` or the `insert_record` method is overridden
44 45 46 47 48 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 44 def insert_records(collection, = nil) update_collection_items(collection, ) do |record| insert_record(record) end end |
#remove_record(_record) ⇒ Object
Removes a single record into a collection Must return true for a successful update, or return false for any failures
27 28 29 30 31 32 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 27 def remove_record(_record) raise build_relationship_not_supported_exception( "Override #{self.class.name}#remove_record", ) end |
#remove_records(collection, failure_message_or_callable = nil) ⇒ Object
Removes all records from a collection into the parent resource
Will attempt to call ‘remove_record` for each item in the `collection`. If false is received for any `remove_record` call, an exception with a payload will be raised after all items have been iterated through
You need to reverse all changes made in the event of an exception, wrapping an an ActiveRecord::Base.transaction block will usually work
Action is not supported unless ‘remove_records` or the `remove_record` method is overridden
60 61 62 63 64 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 60 def remove_records(collection, = nil) update_collection_items(collection, ) do |record| remove_record(record) end end |
#replace_all_records(_collection) ⇒ Object
Replaces all records
67 68 69 70 71 72 73 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 67 def replace_all_records(_collection) # Action is not supported unless overridden raise build_relationship_not_supported_exception( "Override #{self.class.name}#replace_all_records to implement replace all", ) end |