Module: Sinclair::ClassMethods

Included in:
Sinclair
Defined in:
lib/sinclair/class_methods.rb

Overview

Class methods for Sinclair

Author:

  • darthjee

Instance Method Summary collapse

Instance Method Details

#build(*args, **opts, &block) { ... } ⇒ Array<MethodDefinition>

Runs build using a block for adding the methods

The block is executed adding the methods and after the builder runs build building all the methods

Examples:

Simple usage

class MyPerson
end

Sinclair.build(model_class) do
  add_method(:random_name, cached: true) do
    "John #{Random.rand(1000)} Doe"
  end
end

model = MyPerson.new

model.random_name # returns 'John 803 Doe'

Parameters:

  • block (Proc)

    block to be executed by the builder in order to add the methods before running build

  • klass (Class)

    Class that will receive the methods

  • options (Hash)

    open hash options to be used by builders inheriting from Sinclair through the Sinclair::OptionsParser concern

Yields:

Returns:



35
36
37
38
39
# File 'lib/sinclair/class_methods.rb', line 35

def build(*args, **opts, &block)
  new(*args, **opts).tap do |builder|
    builder.instance_eval(&block) if block_given?
  end.build
end