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.



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

def props_with_defaults
  @props_with_defaults
end

#props_without_defaultsObject (readonly)

Returns the value of attribute props_without_defaults.



43
44
45
# File 'lib/types/props/optional.rb', line 43

def props_without_defaults
  @props_without_defaults
end

Instance Method Details

#add_prop_definition(prop, rules) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/types/props/optional.rb', line 45

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



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

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



78
79
80
# File 'lib/types/props/optional.rb', line 78

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

#has_default?(rules) ⇒ Boolean

Returns:



74
75
76
# File 'lib/types/props/optional.rb', line 74

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

#prop_optional?(prop) ⇒ Boolean

Returns:



28
29
30
# File 'lib/types/props/optional.rb', line 28

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

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



64
65
66
67
68
69
70
71
72
# File 'lib/types/props/optional.rb', line 64

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:



24
25
26
# File 'lib/types/props/optional.rb', line 24

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