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_records(document, failure_message_or_callable = nil, &block) ⇒ Object
Inserts all records from a document into the parent resource Either provide a block method that adds an individual item or override this method.
-
#load_collection(document) ⇒ Object
Loads a collection from the document Use this method when implementinog a CollectionManager.
-
#remove_records(document, failure_message_or_callable = nil, &block) ⇒ Object
Removes all records from a document from the parent resource Either provide a block method that removes an individual item or override this method.
- #replace_all_records(_document) ⇒ Object
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
68 69 70 71 72 73 74 75 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 68 def (resource, ) resource_id = deserializer.format_resource_id(resource) = if .respond_to?(:call) = .call(resource_id) end || (resource_id) end |
#insert_records(document, failure_message_or_callable = nil, &block) ⇒ Object
Inserts all records from a document into the parent resource Either provide a block method that adds an individual item or override this method
The block will be provided with a resource and must return true, or 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
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 30 def insert_records(document, = nil, &block) # Action is not supported unless overridden, or a block is defined unless block_given? raise build_relationship_not_supported_exception( "Override #{self.class.name}#insert_records or supply a block", ) end update_collection_items(load_collection(document), , &block) end |
#load_collection(document) ⇒ Object
Loads a collection from the document Use this method when implementinog a CollectionManager
18 19 20 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 18 def load_collection(document) FunWithJsonApi::FindCollectionFromDocument.find(document, deserializer) end |
#remove_records(document, failure_message_or_callable = nil, &block) ⇒ Object
Removes all records from a document from the parent resource Either provide a block method that removes an individual item or override this method
The block will be provided with a resource and must return true, or 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
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 49 def remove_records(document, = nil, &block) # Action is not supported unless overridden, or a block is defined unless block_given? raise build_relationship_not_supported_exception( "Override #{self.class.name}#remove_records or supply a block", ) end update_collection_items(load_collection(document), , &block) end |
#replace_all_records(_document) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/fun_with_json_api/collection_manager.rb', line 60 def replace_all_records(_document) # Action is not supported unless overridden raise build_relationship_not_supported_exception( "Override #{self.class.name}#replace_all_records to implement replace all", ) end |