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, **options, &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'

Yields:



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

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