Module: Vedeu::Model::ClassMethods

Defined in:
lib/vedeu/repositories/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.



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

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.



36
37
38
# File 'lib/vedeu/repositories/model.rb', line 36

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

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

The default values for a new instance of this class.

Returns:

  • (Hash<Symbol => NilClass, String>)


58
59
60
61
62
63
# File 'lib/vedeu/repositories/model.rb', line 58

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

#repository(klass, as:) ⇒ void

Note:

Not current used.

This method returns an undefined value.

Parameters:

  • klass (void)
  • as (void)


48
49
50
51
# File 'lib/vedeu/repositories/model.rb', line 48

def repository(klass, as:)
  self.repository = as
  klass.register(as, self)
end