Sha256: 2df3b7b9debf0db386c034fea9d2aa45d3c9a2b5e8320e3801406465afadda96

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 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(*args, **opts, &block)
      new(*args, **opts).tap do |builder|
        builder.instance_eval(&block) if block_given?
      end.build
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/class_methods.rb
sinclair-2.1.0 lib/sinclair/class_methods.rb
sinclair-2.0.1 lib/sinclair/class_methods.rb