Module: T::Props::Serializable

Includes:
Optional, Plugin, PrettyPrintable
Included in:
InexactStruct
Defined in:
lib/types/props/serializable.rb

Overview

typed: false

Defined Under Namespace

Modules: ClassMethods, DecoratorMethods

Constant Summary

Constants included from Helpers

Helpers::Private

Instance Method Summary collapse

Methods included from PrettyPrintable

#inspect, #pretty_inspect, #pretty_print

Methods included from Helpers

#abstract!, #final!, #interface!, #mixes_in_class_methods, #requires_ancestor, #sealed!

Methods included from Sig

#sig

Instance Method Details

#deserialize(hash, strict = false) ⇒ void

This method returns an undefined value.

Populates the property values on this object with the values from a hash. In general, prefer to use from_hash to construct a new instance, instead of loading into an existing instance.

Parameters:

  • hash (Hash<String, Object>)

    The hash to take property values from.

  • strict (T::Boolean) (defaults to: false)

    (false) If true, raise an exception if the hash contains keys that do not correspond to any known props on this instance.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/types/props/serializable.rb', line 59

def deserialize(hash, strict=false)
  begin
    hash_keys_matching_props = __t_props_generated_deserialize(hash)
  rescue => e
    msg = self.class.decorator.message_with_generated_source_context(
      e,
      :__t_props_generated_deserialize,
      :generate_deserialize_source
    )
    if msg
      begin
        raise e.class.new(msg)
      rescue ArgumentError
        raise TypeError.new(msg)
      end
    else
      raise
    end
  end

  if hash.size > hash_keys_matching_props
    serialized_forms = self.class.decorator.prop_by_serialized_forms
    extra = hash.reject {|k, _| serialized_forms.key?(k)}

    # `extra` could still be empty here if the input matches a `dont_store` prop;
    # historically, we just ignore those
    if !extra.empty?
      if strict
        raise "Unknown properties for #{self.class.name}: #{extra.keys.inspect}"
      else
        @_extra_props = extra
      end
    end
  end
end

#serialize(strict = true) ⇒ Hash

Serializes this object to a hash, suitable for conversion to JSON/BSON.

Parameters:

  • strict (T::Boolean) (defaults to: true)

    (true) If false, do not raise an exception if this object has mandatory props with missing values.

Returns:

  • (Hash)

    A serialization of this object.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/types/props/serializable.rb', line 18

def serialize(strict=true)
  begin
    h = __t_props_generated_serialize(strict)
  rescue => e
    msg = self.class.decorator.message_with_generated_source_context(
      e,
      :__t_props_generated_serialize,
      :generate_serialize_source
    )
    if msg
      begin
        raise e.class.new(msg)
      rescue ArgumentError
        raise TypeError.new(msg)
      end
    else
      raise
    end
  end

  h.merge!(@_extra_props) if defined?(@_extra_props)
  h
end

#with(changed_props) ⇒ Object

with() will clone the old object to the new object and merge the specified props to the new object.



103
104
105
# File 'lib/types/props/serializable.rb', line 103

def with(changed_props)
  with_existing_hash(changed_props, existing_hash: self.serialize)
end