Sha256: 71c97ec0bd2fc26d6fdb8b3f3eb2df70ba6a8006faa2135a14ecbe717a63b6ec

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 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
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-0.10.1 lib/dry/initializer/mixin.rb
dry-initializer-0.10.0 lib/dry/initializer/mixin.rb