Module: T::Props::Optional::DecoratorMethods

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

Overview

NB: This must stay in the same file where T::Props::Optional is defined due to T::Props::Decorator#apply_plugin; see git.corp.stripe.com/stripe-internal/pay-server/blob/fc7f15593b49875f2d0499ffecfd19798bac05b3/chalk/odm/lib/chalk-odm/document_decorator.rb#L716-L717

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sig

sig

Instance Attribute Details

#props_with_defaultsObject (readonly)

Returns the value of attribute props_with_defaults.



47
48
49
# File 'lib/types/props/optional.rb', line 47

def props_with_defaults
  @props_with_defaults
end

#props_without_defaultsObject (readonly)

Returns the value of attribute props_without_defaults.



51
52
53
# File 'lib/types/props/optional.rb', line 51

def props_without_defaults
  @props_without_defaults
end

Instance Method Details

#add_prop_definition(prop, rules) ⇒ Object



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

def add_prop_definition(prop, rules)
  compute_derived_rules(rules)

  default_setter = T::Props::Private::ApplyDefault.for(decorated_class, rules)
  if default_setter
    @props_with_defaults ||= {}
    @props_with_defaults[prop] = default_setter
    props_without_defaults&.delete(prop) # Handle potential override

    rules[DEFAULT_SETTER_RULE_KEY] = default_setter
  else
    @props_without_defaults ||= {}
    @props_without_defaults[prop] = rules.fetch(:setter_proc)
    props_with_defaults&.delete(prop) # Handle potential override
  end

  super
end

#compute_derived_rules(rules) ⇒ Object



40
41
42
43
# File 'lib/types/props/optional.rb', line 40

def compute_derived_rules(rules)
  rules[:fully_optional] = !T::Props::Utils.need_nil_write_check?(rules)
  rules[:need_nil_read_check] = T::Props::Utils.need_nil_read_check?(rules)
end

#get_default(rules, instance_class) ⇒ Object



86
87
88
# File 'lib/types/props/optional.rb', line 86

def get_default(rules, instance_class)
  rules[DEFAULT_SETTER_RULE_KEY]&.default
end

#has_default?(rules) ⇒ Boolean

Returns:



82
83
84
# File 'lib/types/props/optional.rb', line 82

def has_default?(rules)
  rules.include?(DEFAULT_SETTER_RULE_KEY)
end

#prop_optional?(prop) ⇒ Boolean

Returns:



36
37
38
# File 'lib/types/props/optional.rb', line 36

def prop_optional?(prop)
  prop_rules(prop)[:fully_optional]
end

#prop_validate_definition!(name, cls, rules, type) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/types/props/optional.rb', line 72

def prop_validate_definition!(name, cls, rules, type)
  result = super

  if rules.key?(:default) && rules.key?(:factory)
    raise ArgumentError.new("Setting both :default and :factory is invalid. See: go/chalk-docs")
  end

  result
end

#valid_rule_key?(key) ⇒ Boolean

Returns:



32
33
34
# File 'lib/types/props/optional.rb', line 32

def valid_rule_key?(key)
  super || VALID_RULE_KEYS[key]
end