Sha256: afcce015067cefc3697d352d2c51f42ad50da3e50ef5467a609b527fd0c20bd6

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Dry::Initializer
  # Class-level DSL for the initializer
  module Mixin
    # Declares a plain argument
    #
    # @param [#to_sym] name
    #
    # @option options [Object]  :default The default value
    # @option options [#call]   :type    The type constraings via `dry-types`
    # @option options [Boolean] :reader (true) Whether to define attr_reader
    #
    # @return [self] itself
    #
    def param(name, type = nil, **options)
      options[:type]   = type if type
      options[:option] = false
      @initializer_builder = initializer_builder.define(name, **options)
      initializer_builder.call(self)
    end

    # Declares a named argument
    #
    # @param  (see #param)
    # @option (see #param)
    # @return (see #param)
    #
    def option(name, type = nil, **options)
      options[:type]   = type if type
      options[:option] = true
      @initializer_builder = initializer_builder.define(name, **options)
      initializer_builder.call(self)
    end

    # Adds new plugin to the builder
    #
    # @param  [Dry::Initializer::Plugins::Base] plugin
    # @return [self] itself
    #
    def register_initializer_plugin(plugin)
      @initializer_builder = initializer_builder.register(plugin)
      initializer_builder.call(self)
    end

    private

    def initializer_builder
      @initializer_builder ||= Builder.new
    end

    def inherited(klass)
      klass.instance_variable_set :@initializer_builder, initializer_builder.dup

      super
    end

    def self.extended(klass)
      klass.send(:initializer_builder).call(klass)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-initializer-0.10.2 lib/dry/initializer/mixin.rb