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

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

Constant Summary collapse

VALID_OPTIONAL_RULES =

TODO: clean this up. This set of options is confusing, and some of them are not universally applicable (e.g., :on_load only applies when using T::Serializable).

Set[
  :existing, # deprecated
  :on_load,
  false,
  true,
].freeze

Instance Method Summary collapse

Instance Method Details

#add_prop_definition(prop, rules) ⇒ Object



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

def add_prop_definition(prop, rules)
  compute_derived_rules(rules)
  super
end

#compute_derived_rules(rules) ⇒ Object



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

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



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

def get_default(rules, instance_class)
  if rules.include?(:default)
    default = rules[:default]
    T::Props::Utils.deep_clone_object(default)
  elsif rules.include?(:factory)
    # Factory should never be nil if the key is specified, but
    # we do this rather than 'elsif rules[:factory]' for
    # consistency with :default.
    factory = rules[:factory]
    instance_class.class_exec(&factory)
  else
    nil
  end
end

#has_default?(rules) ⇒ Boolean

Returns:



67
68
69
# File 'lib/types/props/optional.rb', line 67

def has_default?(rules)
  rules.include?(:default) || rules.include?(:factory)
end

#mutate_prop_backdoor!(prop, key, value) ⇒ Object



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

def mutate_prop_backdoor!(prop, key, value)
  rules = props.fetch(prop)
  rules = rules.merge(key => value)
  compute_derived_rules(rules)
  @props = props.merge(prop => rules.freeze).freeze
end

#prop_optional?(prop) ⇒ Boolean

Returns:



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

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

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



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

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

  if (rules_optional = rules[:optional])
    if !VALID_OPTIONAL_RULES.include?(rules_optional)
      raise ArgumentError.new(":optional must be one of #{VALID_OPTIONAL_RULES.inspect}")
    end
  end

  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_propsObject



24
25
26
27
28
29
30
# File 'lib/types/props/optional.rb', line 24

def valid_props
  super + [
    :default,
    :factory,
    :optional,
  ]
end