Module: Vedeu::Repositories::Model::ClassMethods

Defined in:
lib/vedeu/repositories/model.rb

Overview

When Vedeu::Repositories::Model is included in a class, the methods within this module are included as class methods on that class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#repositoryVedeu::Repositories::Repository (readonly)



24
25
26
# File 'lib/vedeu/repositories/model.rb', line 24

def repository
  @repository
end

Instance Method Details

#build(attributes = {}, &block) ⇒ Object

Build models using a simple DSL when a block is given, otherwise returns a new instance of the class including this module.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vedeu/repositories/model.rb', line 34

def build(attributes = {}, &block)
  attributes = defaults.merge!(attributes)

  model = new(attributes)

  if block_given?
    model.deputy(attributes[:client]).instance_eval(&block)
  end

  model
end

#child(klass) ⇒ void Also known as: member, collection

This method returns an undefined value.

Provide a convenient way to define the child or children of a model.



52
53
54
# File 'lib/vedeu/repositories/model.rb', line 52

def child(klass)
  send(:define_method, __callee__) { klass }
end

#defaultsHash<Symbol => NilClass, String> (private)

The default values for a new instance of this class.



72
73
74
75
76
77
# File 'lib/vedeu/repositories/model.rb', line 72

def defaults
  {
    client: nil,
    name:   '',
  }
end

#repo(klass) ⇒ void

This method returns an undefined value.

Allow models to specify their repository using a class method.



63
64
65
# File 'lib/vedeu/repositories/model.rb', line 63

def repo(klass)
  @repository = klass
end