Sha256: d49caad5b133f13ef39b53af37c93fc8da634c3b0cdee422e382ab547700330d

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

require "set"

# Namespace for gems in a dry-rb community
module Dry
  #
  # DSL for declaring params and options of class initializers
  #
  module Initializer
    # Singleton for unassigned values
    UNDEFINED = Object.new.freeze

    require_relative "initializer/dsl"
    require_relative "initializer/definition"
    require_relative "initializer/builders"
    require_relative "initializer/config"
    require_relative "initializer/mixin"

    # Adds methods [.[]] and [.define]
    extend DSL

    # Gem-related configuration
    # @return [Dry::Initializer::Config]
    def dry_initializer
      @dry_initializer ||= Config.new(self)
    end

    # Adds or redefines a parameter of [#dry_initializer]
    # @param  [Symbol]       name
    # @param  [#call, nil]   coercer (nil)
    # @option opts [#call]   :type
    # @option opts [Proc]    :default
    # @option opts [Boolean] :optional
    # @option opts [Symbol]  :as
    # @option opts [true, false, :protected, :public, :private] :reader
    # @return [self] itself
    def param(name, type = nil, **opts)
      dry_initializer.param(name, type, opts)
      self
    end

    # Adds or redefines an option of [#dry_initializer]
    # @param  (see #param)
    # @option (see #param)
    # @return (see #param)
    def option(name, type = nil, **opts)
      dry_initializer.option(name, type, opts)
      self
    end

    private

    def inherited(klass)
      super
      config = Config.new(klass, null: dry_initializer.null)
      klass.send(:instance_variable_set, :@dry_initializer, config)
      dry_initializer.children << config
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-initializer-2.3.0 lib/dry/initializer.rb
dry-initializer-2.2.0 lib/dry/initializer.rb
dry-initializer-2.1.0 lib/dry/initializer.rb
dry-initializer-2.0.0 lib/dry/initializer.rb