Sha256: edc251bc4043a3ad87f0558d26095753a7418d0fd592f5bc8b6f01a1772708b5

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

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

    # parts of __initalizer__
    def presetter; end

    def safe_setter
      "@#{target} = #{maybe_coerced}"
    end

    def fast_setter
      safe_setter
    end

    # part of __defaults__
    def default_hash
      super :param
    end

    # part of __coercers__
    def coercer_hash
      super :param
    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
      " == #{undefined} ?" \
      " instance_exec(&__defaults__[:param_#{target}]) :" \
      " #{target}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-1.4.1 lib/dry/initializer/param.rb
dry-initializer-1.4.0 lib/dry/initializer/param.rb