Sha256: d8545c1a818abd8a905b6f1c8d3b4d0bdfae912f79c96b069dd6e7970a6c0683

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 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, **options)
      arguments_builder.define_initializer(name, option: false, **options)
      self
    end

    # Declares a named argument
    #
    # @param  (see #param)
    # @option (see #param)
    # @return (see #param)
    #
    def option(name, **options)
      arguments_builder.define_initializer(name, option: true, **options)
      self
    end

    private

    def arguments_builder
      @arguments_builder ||= begin
        builder = Builder.new
        include builder.mixin
        builder
      end
    end

    def inherited(klass)
      klass.instance_variable_set(:@arguments_builder, arguments_builder)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-0.1.1 lib/dry/initializer/mixin.rb
dry-initializer-0.1.0 lib/dry/initializer/mixin.rb