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)



27
28
29
# File 'lib/vedeu/repositories/model.rb', line 27

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.

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.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vedeu/repositories/model.rb', line 37

def build(attributes = {}, &block)
  model = new(attributes)

  Vedeu.log(type:    :debug,
            message: "DSL building: '#{model.class.name}' for " \
                     "'#{model.name}'")

  model.deputy.instance_eval(&block) if block_given?

  model
end

#repo(klass) ⇒ void

This method returns an undefined value.

Allow models to specify their repository using a class method.

Parameters:

  • klass (void)


54
55
56
# File 'lib/vedeu/repositories/model.rb', line 54

def repo(klass)
  @repository = klass
end

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

Create and store a model with the given attributes.

Parameters:

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

    A collection of attributes specific to the model.

  • block (Proc)

    A block of code to be executing whilst storing.

Returns:

  • (Object)

    An instance of the model.



65
66
67
# File 'lib/vedeu/repositories/model.rb', line 65

def store(attributes = {}, &block)
  new(attributes).store(&block)
end