Module: Muve::Store
Overview
Muve::Store takes care of resource persistence and retrieval. Use stores as adaptors to connect your implementation of Muve to whichever datastore you please.
Adaptors or Stores only take of the interaction with the datastore but leave the model’s housekeeping up the respective model. Make sure to conform to the expected return formats for the different adaptor methods.
Take a look at Muve::Store::Mongo
to find an implementation of a store adaptor.
Defined Under Namespace
Modules: Formatter
Instance Method Summary collapse
-
#count(resource, details = {}) ⇒ Object
counts the resources matching the details, if any.
-
#create(resource, details) ⇒ Object
creates a resource containing the specified details in the repository.
-
#delete(resource, id, details = nil) ⇒ Object
(also: #destroy, #remove)
removes a resource matching the optional
id
anddetails
from the repository. -
#fetch(resource, id, details = {}) ⇒ Object
collect a single resource from the repository that matches the given id and details.
-
#find(resource, details) ⇒ Object
find resources from its repository that match the given id and details Returns an
Enumerator
that returns a hash with the keyid
containing the primary key for the respective resource. - #formatter ⇒ Object
-
#get(resource, id = nil, details = nil) ⇒ Object
gets data from the given container matching the provided details.
-
#index_hash(index_values) ⇒ Object
composes the id hash for the used store.
-
#update(resource, id, details) ⇒ Object
update a resource with the identified by
id
with the givendetails
.
Methods included from Helper
Instance Method Details
#count(resource, details = {}) ⇒ Object
counts the resources matching the details, if any. Returns a integer that represents the amount of matching entities found.
91 92 93 |
# File 'lib/muve/store.rb', line 91 def count(resource, details={}) raise IncompleteImplementation, "implement a count handler for #{self}" end |
#create(resource, details) ⇒ Object
creates a resource containing the specified details in the repository. Returns the id of the created object on success, raises an error otherwise
46 47 48 |
# File 'lib/muve/store.rb', line 46 def create(resource, details) raise IncompleteImplementation, "implement a create handler for #{self}" end |
#delete(resource, id, details = nil) ⇒ Object Also known as: destroy, remove
removes a resource matching the optional id
and details
from the repository. A successful removal operation should returns true
while any other value is considered an error.
54 55 56 |
# File 'lib/muve/store.rb', line 54 def delete(resource, id, details=nil) raise IncompleteImplementation, "implement a delete handler for #{self}" end |
#fetch(resource, id, details = {}) ⇒ Object
collect a single resource from the repository that matches the given id and details. Upon the successful retrieval of a resource the id of the resource is presented under the key id
while other attributes of the resource bear arbitrary names.
{ id: 12, name: 'Spock', organization: 'The Enterprise' }
69 70 71 |
# File 'lib/muve/store.rb', line 69 def fetch(resource, id, details={}) raise IncompleteImplementation, "implement a fetch handler for #{self}" end |
#find(resource, details) ⇒ Object
find resources from its repository that match the given id and details Returns an Enumerator
that returns a hash with the key id
containing the primary key for the respective resource.
def find(resource, details)
details = {} unless details.kind_of? Hash
Enumerator.new do |item|
fetched_result_from_datastore.each do |data|
item << format_data(data) # format_data composes the required hash
end
end
end
85 86 87 |
# File 'lib/muve/store.rb', line 85 def find(resource, details) raise IncompleteImplementation, "implement a find handler for #{self}" end |
#formatter ⇒ Object
105 106 107 |
# File 'lib/muve/store.rb', line 105 def formatter raise IncompleteImplementation, "specify a formatter" end |
#get(resource, id = nil, details = nil) ⇒ Object
gets data from the given container matching the provided details
Given a Place
resource the following calls may be acceptable
-
Adaptor.get(Place, 1232) # find one resource where id = 1232
-
Adaptor.get(Place, nil, { city: ‘NYC’, rating: 5 }) # find more
34 35 36 37 38 39 40 41 42 |
# File 'lib/muve/store.rb', line 34 def get(resource, id=nil, details=nil) raise InvalidQuery unless (id || details) if details find(resource, details) else fetch(resource, id, {}) end end |
#index_hash(index_values) ⇒ Object
composes the id hash for the used store. Some in some cases the index is the id
or _id
field, while in other cases the index field may be different. The store should take care of index naming.
101 102 103 |
# File 'lib/muve/store.rb', line 101 def index_hash(index_values) raise IncompleteImplementation, "implement the index_hash handler for #{self}" end |
#update(resource, id, details) ⇒ Object
update a resource with the identified by id
with the given details
59 60 61 |
# File 'lib/muve/store.rb', line 59 def update(resource, id, details) raise IncompleteImplementation, "implement a update handler for #{self}" end |