Module: T::Props::WeakConstructor::DecoratorMethods
- Extended by:
- Sig
- Defined in:
- lib/types/props/weak_constructor.rb
Instance Method Summary collapse
- #construct_props_with_defaults(instance, hash) ⇒ Object
- #construct_props_without_defaults(instance, hash) ⇒ Object
Methods included from Sig
Methods included from T::Private::Methods::SingletonMethodHooks
Methods included from T::Private::Methods::MethodHooks
Instance Method Details
#construct_props_with_defaults(instance, hash) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/types/props/weak_constructor.rb', line 58 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) default_struct.bound_setter_proc.call(instance, hash[p]) result += 1 else default_struct.set_default(instance) end end result end |
#construct_props_without_defaults(instance, hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/types/props/weak_constructor.rb', line 37 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, bound_setter| if hash.key?(p) bound_setter.call(instance, hash[p]) result += 1 end end result end |