module Muve::Model::ClassMethods

Class methods exposed to all Muve models

Public Instance Methods

adaptor() click to toggle source

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
adaptor=(adaptor) click to toggle source

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
connection() click to toggle source
# File lib/muve/model.rb, line 242
def connection
  Muve::Model.connection
end
container() click to toggle source

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
convert(resource) click to toggle source
# File lib/muve/model.rb, line 238
def convert(resource)
  adaptor.formatter.convert_to_storeable_object(resource)
end
count(params={}) click to toggle source

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
create(attr) click to toggle source

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
create!(attr) click to toggle source
# File lib/muve/model.rb, line 316
def create!(attr)
  self.new(attr).save!
end
database() click to toggle source
# File lib/muve/model.rb, line 246
def database
  Muve::Model.database
end
destroy_all() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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
find(id) click to toggle source

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
model_name() click to toggle source
# File lib/muve/model.rb, line 250
def model_name
  self.to_s.split("::").last
end
where(params) click to toggle source

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
with_fields(*args) click to toggle source

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