Sha256: bafa385a95b3f2142d5812946731556bcdae361808b8bbd2a36fcba53a558187

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Dry::Initializer
  class Param < Attribute
    # part of __initializer__ definition
    def initializer_signature
      optional ? "#{target} = Dry::Initializer::UNDEFINED" : target
    end

    # part of __initializer__ body
    def initializer_presetter; end

    # part of __initializer__ body
    def initializer_setter
      "@#{target} = #{maybe_coerced}"
    end

    # part of __defaults__
    def default_hash
      default ? { :"param_#{target}" => default } : {}
    end

    # part of __coercers__
    def coercer_hash
      coercer ? { :"param_#{target}" => coercer } : {}
    end

    private

    def initialize(*args, **options)
      fail ArgumentError.new("Do not rename params") if options.key? :as
      super
    end

    def maybe_coerced
      return maybe_default unless coercer
      "__coercers__[:param_#{target}].call(#{maybe_default})"
    end

    def maybe_default
      "#{target}#{default_part}"
    end

    def default_part
      return unless default
      " == Dry::Initializer::UNDEFINED ?" \
      " instance_eval(&__defaults__[:param_#{target}]) :" \
      " #{target}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-initializer-1.0.0 lib/dry/initializer/param.rb