module T::Props::WeakConstructor::DecoratorMethods
def construct_props_with_defaults(instance, hash)
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
def construct_props_without_defaults(instance, hash)
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