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
- #add_prop_definition(prop, rules) ⇒ Object
- #get_default(rules, instance_class) ⇒ Object
- #has_default?(rules) ⇒ Boolean
- #prop_optional?(prop) ⇒ Boolean
- #prop_validate_definition!(name, cls, rules, type) ⇒ Object
- #valid_props ⇒ Object
Instance Method Details
#add_prop_definition(prop, rules) ⇒ Object
34 35 36 37 |
# File 'lib/types/props/optional.rb', line 34 def add_prop_definition(prop, rules) rules[:fully_optional] = !T::Props::Utils.need_nil_write_check?(rules) super end |
#get_default(rules, instance_class) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/types/props/optional.rb', line 59 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
55 56 57 |
# File 'lib/types/props/optional.rb', line 55 def has_default?(rules) rules.include?(:default) || rules.include?(:factory) end |
#prop_optional?(prop) ⇒ Boolean
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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/types/props/optional.rb', line 39 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_props ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/types/props/optional.rb', line 24 def valid_props super + [ :default, :factory, :optional, ] end |