Sha256: da1fb139c0e122033520f9a858afaec978c368a22441c54cbab86c167c2ba441

Contents?: true

Size: 682 Bytes

Versions: 13

Compression:

Stored size: 682 Bytes

Contents

module Dry::Initializer::Plugins
  # Builds a block to be evaluated by initializer (__after_initialize__)
  # to assign a default value to the argument
  class DefaultProc < Base
    def call
      return unless default

      ivar = :"@#{name}"
      default_proc = default

      proc do
        if instance_variable_get(ivar) == Dry::Initializer::UNDEFINED
          instance_variable_set ivar, instance_eval(&default_proc)
        end
      end
    end

    private

    def default
      return unless settings.key? :default

      @default ||= settings[:default].tap do |value|
        fail DefaultValueError.new(name, value) unless Proc === value
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-initializer-0.9.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.8.1 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.8.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.7.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.6.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.5.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.4.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.3.3 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.3.2 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.3.1 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.3.0 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.2.1 lib/dry/initializer/plugins/default_proc.rb
dry-initializer-0.2.0 lib/dry/initializer/plugins/default_proc.rb