Sha256: bf6f0b1ae208326e8a104fedc270ed90878e5b99de325364b2b211a71e2804af

Contents?: true

Size: 569 Bytes

Versions: 5

Compression:

Stored size: 569 Bytes

Contents

module Dry::Initializer::Plugins
  # Plugin builds either chunk of code for the #initializer,
  # or a proc for the ##__after_initialize__ callback.
  class TypeConstraint < Base
    def call
      return unless settings.key? :type

      type = settings[:type]
      fail TypeConstraintError.new(rename, type) unless type.respond_to? :call

      ivar = :"@#{rename}"
      lambda do |*|
        value = instance_variable_get(ivar)
        return if value == Dry::Initializer::UNDEFINED
        instance_variable_set ivar, type.call(value)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-initializer-0.10.1 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.10.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.9.3 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.9.2 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.9.1 lib/dry/initializer/plugins/type_constraint.rb