module Muve::Store
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.
Public Instance Methods
counts the resources matching the details, if any. Returns a integer that represents the amount of matching entities found.
# File lib/muve/store.rb, line 91 def count(resource, details={}) raise IncompleteImplementation, "implement a count handler for #{self}" end
creates a resource containing the specified details in the repository. Returns the id of the created object on success, raises an error otherwise
# File lib/muve/store.rb, line 46 def create(resource, details) raise IncompleteImplementation, "implement a create handler for #{self}" end
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.
# File lib/muve/store.rb, line 54 def delete(resource, id, details=nil) raise IncompleteImplementation, "implement a delete handler for #{self}" end
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' }
# File lib/muve/store.rb, line 69 def fetch(resource, id, details={}) raise IncompleteImplementation, "implement a fetch handler for #{self}" end
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
# File lib/muve/store.rb, line 85 def find(resource, details) raise IncompleteImplementation, "implement a find handler for #{self}" end
# File lib/muve/store.rb, line 105 def formatter raise IncompleteImplementation, "specify a formatter" end
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
# 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
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.
# File lib/muve/store.rb, line 101 def index_hash(index_values) raise IncompleteImplementation, "implement the index_hash handler for #{self}" end
update a resource with the identified by id
with the given details
# File lib/muve/store.rb, line 59 def update(resource, id, details) raise IncompleteImplementation, "implement a update handler for #{self}" end