Module: Muve::Model::ClassMethods
- Includes:
- Error
- Defined in:
- lib/muve/model.rb
Overview
Class methods exposed to all Muve models
Instance Method Summary collapse
-
#adaptor ⇒ Object
The adaptor currently set to handle persistence for all Muve::Model classes and instances.
-
#adaptor=(adaptor) ⇒ Object
Configure the adaptor to take care of handling persistence for this model.
- #connection ⇒ Object
-
#container ⇒ Object
The container (e.g.: collection, tablename or anything that is analogous to this construct) of the resource.
- #convert(resource) ⇒ Object
-
#count(params = {}) ⇒ Object
Counts the amount of records that match the parameters.
-
#create(attr) ⇒ Object
Creates a new resource and persists it to the datastore.
- #create!(attr) ⇒ Object
- #database ⇒ Object
- #destroy_all ⇒ Object
- #extract(storeable, klass = nil) ⇒ Object
-
#extract_attributes(resource: self.new, fields: [], invalid_attributes: [], id: nil) ⇒ Object
Returns a Hash of the attributes for the given resource TODO: do we still need this?.
-
#find(id) ⇒ Object
Finds a resource by id.
- #model_name ⇒ Object
-
#where(params) ⇒ Object
Querries the resource repository for all resources that match the specified parameters.
-
#with_fields(*args) ⇒ Object
The
with_field
helper allows one to declare a functioning model with less lines of code.
Instance Method Details
#adaptor ⇒ Object
The adaptor currently set to handle persistence for all Muve::Model classes and instances
229 230 231 232 |
# 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 |
#adaptor=(adaptor) ⇒ Object
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)
223 224 225 |
# File 'lib/muve/model.rb', line 223 def adaptor=(adaptor) @adaptor = adaptor end |
#connection ⇒ Object
242 243 244 |
# File 'lib/muve/model.rb', line 242 def connection Muve::Model.connection end |
#container ⇒ Object
The container (e.g.: collection, tablename or anything that is analogous to this construct) of the resource
256 257 258 |
# File 'lib/muve/model.rb', line 256 def container raise Muve::Error::NotConfigured, "container not defined for #{self}" end |
#convert(resource) ⇒ Object
238 239 240 |
# File 'lib/muve/model.rb', line 238 def convert(resource) adaptor.formatter.convert_to_storeable_object(resource) end |
#count(params = {}) ⇒ Object
Counts the amount of records that match the parameters
295 296 297 |
# File 'lib/muve/model.rb', line 295 def count(params={}) self.adaptor.count(self, params) end |
#create(attr) ⇒ Object
Creates a new resource and persists it to the datastore
312 313 314 |
# File 'lib/muve/model.rb', line 312 def create(attr) self.new(attr).save end |
#create!(attr) ⇒ Object
316 317 318 |
# File 'lib/muve/model.rb', line 316 def create!(attr) self.new(attr).save! end |
#database ⇒ Object
246 247 248 |
# File 'lib/muve/model.rb', line 246 def database Muve::Model.database end |
#destroy_all ⇒ Object
320 321 322 |
# File 'lib/muve/model.rb', line 320 def destroy_all warn "Destroying of all entities for a resource is not implemented" end |
#extract(storeable, klass = nil) ⇒ Object
234 235 236 |
# File 'lib/muve/model.rb', line 234 def extract(storeable, klass=nil) adaptor.formatter.convert_from_storeable_object(storeable, klass) end |
#extract_attributes(resource: self.new, fields: [], invalid_attributes: [], id: nil) ⇒ Object
Returns a Hash of the attributes for the given resource TODO: do we still need this?
262 263 264 265 266 267 268 269 270 271 272 |
# 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 |
#find(id) ⇒ Object
Finds a resource by id
275 276 277 278 279 |
# 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 |
#model_name ⇒ Object
250 251 252 |
# File 'lib/muve/model.rb', line 250 def model_name self.to_s.split("::").last end |
#where(params) ⇒ Object
Querries the resource repository for all resources that match the specified parameters.
283 284 285 286 287 288 289 290 291 292 |
# 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 |
#with_fields(*args) ⇒ Object
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.
305 306 307 308 309 |
# 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 |