Sha256: 784801fda8dcd1912175cc1dce1b8dd369e09e9b31f452da41ff9ec019c7ca83

Contents?: true

Size: 565 Bytes

Versions: 7

Compression:

Stored size: 565 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(name, type) unless type.respond_to? :call

      ivar = :"@#{name}"
      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

7 entries across 7 versions & 1 rubygems

Version Path
dry-initializer-0.9.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.8.1 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.8.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.7.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.6.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.5.0 lib/dry/initializer/plugins/type_constraint.rb
dry-initializer-0.4.0 lib/dry/initializer/plugins/type_constraint.rb