module Muve::Model::ClassMethods
Class methods exposed to all Muve
models
Public Instance Methods
The adaptor currently set to handle persistence for all Muve::Model
classes and instances
# File lib/muve/model.rb, line 229 def adaptor raise NotConfigured, "the adaptor has not been set" unless (@adaptor || Model.handler) @adaptor or Model.handler end
Configure the adaptor to take care of handling persistence for this model. The adaptor should extend Muve::Store
.
Adaptors provide an abstraction layer between Muve
models and the actual datastore. This provides some flexibility in design as one may exchange an adaptor for another in order to support another database technology (.e.g: swithing between document databases or relational databases)
# File lib/muve/model.rb, line 223 def adaptor=(adaptor) @adaptor = adaptor end
# File lib/muve/model.rb, line 242 def connection Muve::Model.connection end
The container (e.g.: collection, tablename or anything that is analogous to this construct) of the resource
# File lib/muve/model.rb, line 256 def container raise Muve::Error::NotConfigured, "container not defined for #{self}" end
# File lib/muve/model.rb, line 238 def convert(resource) adaptor.formatter.convert_to_storeable_object(resource) end
Counts the amount of records that match the parameters
# File lib/muve/model.rb, line 295 def count(params={}) self.adaptor.count(self, params) end
Creates a new resource and persists it to the datastore
# File lib/muve/model.rb, line 312 def create(attr) self.new(attr).save end
# File lib/muve/model.rb, line 316 def create!(attr) self.new(attr).save! end
# File lib/muve/model.rb, line 246 def database Muve::Model.database end
# File lib/muve/model.rb, line 320 def destroy_all warn "Destroying of all entities for a resource is not implemented" end
# File lib/muve/model.rb, line 234 def extract(storeable, klass=nil) adaptor.formatter.convert_from_storeable_object(storeable, klass) end
Returns a Hash of the attributes for the given resource TODO: do we still need this?
# File lib/muve/model.rb, line 262 def extract_attributes(resource: self.new, fields: [], invalid_attributes: [], id: nil) data = {} fields.select{ |k| k != invalid_attributes }.each { |k| # TODO: confirm resource.respond_to? k prior to assigning data[k.to_sym] = resource.public_send(k) if resource.respond_to? k } if id data = data.merge id: id end data end
Finds a resource by id
# File lib/muve/model.rb, line 275 def find(id) result = self.new result.send(:populate, extract(self.adaptor.get(self, id), result.class)) result end
# File lib/muve/model.rb, line 250 def model_name self.to_s.split("::").last end
Querries the resource repository for all resources that match the specified parameters.
# File lib/muve/model.rb, line 283 def where(params) Enumerator.new do |item| (self.adaptor.get(self, nil, params) or []).each do |details| details result = self.new() result.send(:populate, extract(details, self.new.class)) item << result end end end
The with_field
helper allows one to declare a functioning model with less lines of code.
Instead of declaring +attr_accessor :name, :age, :hat_size+ along with the required private +#fields# method one may specify the known fields of the resource with one line of code.
# File lib/muve/model.rb, line 305 def with_fields(*args) fields = self.new.send(:fields) # TODO: Fix this sloppy mess attr_accessor *args class_eval "def fields; #{Set.new(fields + args).to_a}; end" end