module T::Props::Serializable::DecoratorMethods
NB: This must stay in the same file where T::Props::Serializable
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
Constants
- EMPTY_EXTRA_PROPS
- VALID_RULE_KEYS
Public Instance Methods
add_prop_definition(prop, rules)
click to toggle source
Calls superclass method
# File lib/types/props/serializable.rb, line 216 def add_prop_definition(prop, rules) rules[:serialized_form] = rules.fetch(:name, prop.to_s) res = super prop_by_serialized_forms[rules[:serialized_form]] = prop if T::Configuration.use_vm_prop_serde? enqueue_lazy_vm_method_definition!(:__t_props_generated_serialize) {generate_serialize2} enqueue_lazy_vm_method_definition!(:__t_props_generated_deserialize) {generate_deserialize2} else enqueue_lazy_method_definition!(:__t_props_generated_serialize) {generate_serialize_source} enqueue_lazy_method_definition!(:__t_props_generated_deserialize) {generate_deserialize_source} end res end
extra_props(instance)
click to toggle source
# File lib/types/props/serializable.rb, line 324 def extra_props(instance) instance.instance_variable_get(:@_extra_props) || EMPTY_EXTRA_PROPS end
from_hash(hash, strict=false)
click to toggle source
# File lib/types/props/serializable.rb, line 199 def from_hash(hash, strict=false) raise ArgumentError.new("#{hash.inspect} provided to from_hash") if !(hash && hash.is_a?(Hash)) i = @class.allocate i.deserialize(hash, strict) i end
get_id(instance)
click to toggle source
# File lib/types/props/serializable.rb, line 312 def get_id(instance) prop = prop_by_serialized_forms['_id'] if prop get(instance, prop) else nil end end
message_with_generated_source_context(error, generated_method, generate_source_method)
click to toggle source
# File lib/types/props/serializable.rb, line 253 def message_with_generated_source_context(error, generated_method, generate_source_method) line_label = error.backtrace.find {|l| l.end_with?("in `#{generated_method}'")} return unless line_label line_num = line_label.split(':')[1]&.to_i return unless line_num source_lines = self.send(generate_source_method).split("\n") previous_blank = source_lines[0...line_num].rindex(&:empty?) || 0 next_blank = line_num + (source_lines[line_num..-1]&.find_index(&:empty?) || 0) context = " #{source_lines[(previous_blank + 1)...next_blank].join("\n ")}" <<~MSG Error in #{decorated_class.name}##{generated_method}: #{error.message} at line #{line_num - previous_blank - 1} in: #{context} MSG end
prop_by_serialized_forms()
click to toggle source
# File lib/types/props/serializable.rb, line 195 def prop_by_serialized_forms @class.prop_by_serialized_forms end
prop_dont_store?(prop)
click to toggle source
# File lib/types/props/serializable.rb, line 192 def prop_dont_store?(prop) prop_rules(prop)[:dont_store] end
prop_serialized_form(prop)
click to toggle source
# File lib/types/props/serializable.rb, line 208 def prop_serialized_form(prop) prop_rules(prop)[:serialized_form] end
prop_validate_definition!(name, cls, rules, type)
click to toggle source
Calls superclass method
# File lib/types/props/serializable.rb, line 294 def prop_validate_definition!(name, cls, rules, type) result = super if (rules_name = rules[:name]) unless rules_name.is_a?(String) raise ArgumentError.new("Invalid name in prop #{@class.name}.#{name}: #{rules_name.inspect}") end validate_prop_name(rules_name) end if !rules[:raise_on_nil_write].nil? && rules[:raise_on_nil_write] != true raise ArgumentError.new("The value of `raise_on_nil_write` if specified must be `true` (given: #{rules[:raise_on_nil_write]}).") end result end
raise_nil_deserialize_error(hkey)
click to toggle source
# File lib/types/props/serializable.rb, line 271 def raise_nil_deserialize_error(hkey) msg = "Tried to deserialize a required prop from a nil value. It's "\ "possible that a nil value exists in the database, so you should "\ "provide a `default: or factory:` for this prop (see go/optional "\ "for more details). If this is already the case, you probably "\ "omitted a required prop from the `fields:` option when doing a "\ "partial load." storytime = {prop: hkey, klass: decorated_class.name} # Notify the model owner if it exists, and always notify the API owner. begin if T::Configuration.class_owner_finder && (owner = T::Configuration.class_owner_finder.call(decorated_class)) T::Configuration.hard_assert_handler( msg, storytime: storytime, project: owner ) end ensure T::Configuration.hard_assert_handler(msg, storytime: storytime) end end
required_props()
click to toggle source
# File lib/types/props/serializable.rb, line 188 def required_props @class.props.select {|_, v| T::Props::Utils.required_prop?(v)}.keys end
serialized_form_prop(serialized_form)
click to toggle source
# File lib/types/props/serializable.rb, line 212 def serialized_form_prop(serialized_form) prop_by_serialized_forms[serialized_form.to_s] || raise("No such serialized form: #{serialized_form.inspect}") end
valid_rule_key?(key)
click to toggle source
Calls superclass method
# File lib/types/props/serializable.rb, line 184 def valid_rule_key?(key) super || VALID_RULE_KEYS[key] end
Private Instance Methods
generate_deserialize2()
click to toggle source
# File lib/types/props/serializable.rb, line 245 def generate_deserialize2 T::Props::Private::DeserializerGenerator.generate2( decorated_class, props, props_with_defaults || {}, ) end
generate_deserialize_source()
click to toggle source
# File lib/types/props/serializable.rb, line 234 def generate_deserialize_source T::Props::Private::DeserializerGenerator.generate( props, props_with_defaults || {}, ) end
generate_serialize2()
click to toggle source
# File lib/types/props/serializable.rb, line 241 def generate_serialize2 T::Props::Private::SerializerGenerator.generate2(decorated_class, props) end
generate_serialize_source()
click to toggle source
# File lib/types/props/serializable.rb, line 230 def generate_serialize_source T::Props::Private::SerializerGenerator.generate(props) end
inspect_instance_components(instance, multiline:, indent:)
click to toggle source
@override T::Props::PrettyPrintable
Calls superclass method
# File lib/types/props/serializable.rb, line 329 def inspect_instance_components(instance, multiline:, indent:) if (extra_props = extra_props(instance)) && !extra_props.empty? pretty_kvs = extra_props.map {|k, v| [k.to_sym, v.inspect]} extra = join_props_with_pretty_values(pretty_kvs, multiline: false) super + ["@_extra_props=<#{extra}>"] else super end end