Module: T::Props::WeakConstructor::DecoratorMethods

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

Instance Method Summary collapse

Methods included from Sig

sig

Instance Method Details

#construct_props_with_defaults(instance, hash) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/types/props/weak_constructor.rb', line 53

def construct_props_with_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_with_defaults&.each_pair do |p, default_struct|
    if hash.key?(p)
      instance.instance_exec(hash[p], &default_struct.setter_proc)
      result += 1
    else
      default_struct.set_default(instance)
    end
  end
  result
end

#construct_props_without_defaults(instance, hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/types/props/weak_constructor.rb', line 32

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|
    if hash.key?(p)
      instance.instance_exec(hash[p], &setter_proc)
      result += 1
    end
  end
  result
end