Module: Vedeu::Model::ClassMethods

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

Overview

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

Instance Method Summary collapse

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.

Parameters:

  • attributes (Hash) (defaults to: {})

    A collection of attributes specific to the model.

  • block (Proc)

    The block passed to the build method.

Returns:

  • (Object)

    An instance of the model.



22
23
24
25
26
27
28
# File 'lib/vedeu/models/model.rb', line 22

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

  model = new(attributes)
  model.deputy(attributes[:client]).instance_eval(&block) if block_given?
  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.

Parameters:

  • klass (Class)

    The member (singular) or collection (multiple) class name for the respective model.



35
36
37
# File 'lib/vedeu/models/model.rb', line 35

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

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


46
47
48
49
50
51
# File 'lib/vedeu/models/model.rb', line 46

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