Module: T::Props::Serializable::DecoratorMethods

Defined in:
lib/types/props/serializable.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#add_prop_definition(prop, rules) ⇒ Object



300
301
302
303
304
305
# File 'lib/types/props/serializable.rb', line 300

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
  res
end

#extra_props(instance) ⇒ Object



337
338
339
# File 'lib/types/props/serializable.rb', line 337

def extra_props(instance)
  get(instance, '_extra_props') || EMPTY_EXTRA_PROPS
end

#from_hash(hash, strict = false) ⇒ Object

Raises:

  • (ArgumentError)


283
284
285
286
287
288
289
290
# File 'lib/types/props/serializable.rb', line 283

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) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/types/props/serializable.rb', line 325

def get_id(instance)
  prop = prop_by_serialized_forms['_id']
  if prop
    get(instance, prop)
  else
    nil
  end
end

#prop_by_serialized_formsObject



281
# File 'lib/types/props/serializable.rb', line 281

def prop_by_serialized_forms; @class.prop_by_serialized_forms; end

#prop_dont_store?(prop) ⇒ Boolean

Returns:



280
# File 'lib/types/props/serializable.rb', line 280

def prop_dont_store?(prop); prop_rules(prop)[:dont_store]; end

#prop_serialized_form(prop) ⇒ Object



292
293
294
# File 'lib/types/props/serializable.rb', line 292

def prop_serialized_form(prop)
  prop_rules(prop)[:serialized_form]
end

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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/types/props/serializable.rb', line 307

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

#required_propsObject



276
277
278
# File 'lib/types/props/serializable.rb', line 276

def required_props
  @class.props.select {|_, v| T::Utils::Props.required_prop?(v)}.keys
end

#serialized_form_prop(serialized_form) ⇒ Object



296
297
298
# File 'lib/types/props/serializable.rb', line 296

def serialized_form_prop(serialized_form)
  prop_by_serialized_forms[serialized_form.to_s] || raise("No such serialized form: #{serialized_form.inspect}")
end

#valid_propsObject



268
269
270
271
272
273
274
# File 'lib/types/props/serializable.rb', line 268

def valid_props
  super + [
    :dont_store,
    :name,
    :raise_on_nil_write,
  ]
end