Module: T::Props::Constructor::DecoratorMethods

Extended by:
Sig
Defined in:
lib/types/props/constructor.rb

Instance Method Summary collapse

Methods included from Sig

sig

Instance Method Details

#construct_props_without_defaults(instance, hash) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/types/props/constructor.rb', line 19

def construct_props_without_defaults(instance, hash)
  # Use `each_pair` rather than `count` because, as of Ruby 2.6, the latter delegates to Enumerator
  # and therefore allocates for each entry.
  result = 0
  props_without_defaults&.each_pair do |p, setter_proc|
    begin
      val = hash[p]
      instance.instance_exec(val, &setter_proc)
      if val || hash.key?(p)
        result += 1
      end
    rescue TypeError, T::Props::InvalidValueError
      if !hash.key?(p)
        raise ArgumentError.new("Missing required prop `#{p}` for class `#{instance.class.name}`")
      else
        raise
      end
    end
  end
  result
end