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



309
310
311
312
313
314
# File 'lib/types/props/serializable.rb', line 309

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



346
347
348
# File 'lib/types/props/serializable.rb', line 346

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

#from_hash(hash, strict = false) ⇒ Object

Raises:

  • (ArgumentError)


292
293
294
295
296
297
298
299
# File 'lib/types/props/serializable.rb', line 292

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



334
335
336
337
338
339
340
341
# File 'lib/types/props/serializable.rb', line 334

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

#prop_by_serialized_formsObject



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

def prop_by_serialized_forms; @class.prop_by_serialized_forms; end

#prop_dont_store?(prop) ⇒ Boolean

Returns:



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

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

#prop_serialized_form(prop) ⇒ Object



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

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

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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/types/props/serializable.rb', line 316

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



285
286
287
# File 'lib/types/props/serializable.rb', line 285

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

#serialized_form_prop(serialized_form) ⇒ Object



305
306
307
# File 'lib/types/props/serializable.rb', line 305

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



277
278
279
280
281
282
283
# File 'lib/types/props/serializable.rb', line 277

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