Sha256: 0b5348894141c21af341773a1fa5709768ce312497ca827cecec858dea4d702a

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

class Sinclair
  # @author darthjee
  # @api public
  #
  # Class methods for {Sinclair}
  module ClassMethods
    # 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
    #
    # @param (see Sinclair#initialize)
    # @param block [Proc] block to be executed by the builder
    #   in order to add the methods before running build
    #
    # @yield an instance of a builder ({Sinclair})
    #
    # @return (see Sinclair#build)
    #
    # @example 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'
    def build(klass, options = {}, &block)
      new(klass, options).tap do |builder|
        builder.instance_eval(&block) if block_given?
      end.build
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinclair-2.0.0 lib/sinclair/class_methods.rb
sinclair-1.16.3 lib/sinclair/class_methods.rb