Class: FIRM::Serializable::ID

Inherits:
Object
  • Object
show all
Extended by:
JSON::SerializeClassMethods, XML::SerializeClassMethods
Includes:
FIRM::Serializable, JSON::SerializeInstanceMethods, XML::SerializeInstanceMethods
Defined in:
lib/firm/serializable.rb,
lib/firm/serialize/id.rb,
lib/firm/serializer/xml.rb,
lib/firm/serializer/json.rb,
lib/firm/serializer/yaml.rb

Overview

Serializable unique ids. This class makes sure to maintain uniqueness across serialization/deserialization cycles and keeps all shared instances within a single (serialized/deserialized) object set in sync.

Constant Summary

Constants included from FIRM::Serializable

TLS_VARS_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON::SerializeClassMethods

json_create

Methods included from XML::SerializeClassMethods

from_xml

Methods included from JSON::SerializeInstanceMethods

#as_json

Methods included from XML::SerializeInstanceMethods

#to_xml

Methods included from FIRM::Serializable

[], default_format, default_format=, formatters, included, register, serializables, serialize, tls_vars

Class Method Details

.deserialize(source, format: Serializable.default_format) ⇒ Object

Deserializes object from source data

Parameters:

  • source (IO, String)

    source data (String or IO(-like object))

  • format (Symbol, String) (defaults to: Serializable.default_format)

    data format of source

Returns:

  • (Object)

    deserialized object



19
20
21
# File 'lib/firm/serialize/id.rb', line 19

def deserialize(source, format: Serializable.default_format)
  Serializable.deserialize(source, format: format)
end

Instance Method Details

#encode_with(coder) ⇒ Object



127
128
129
# File 'lib/firm/serializer/yaml.rb', line 127

def encode_with(coder)
  for_serialize(coder)
end

#finalize_from_serializedself (protected)

Noop for ID instances.

Returns:

  • (self)


75
76
77
78
# File 'lib/firm/serialize/id.rb', line 75

def finalize_from_serialized
  # no finalization necessary
  self
end

#for_serialize(hash, _excludes = nil) ⇒ Object (protected)

Noop for ID instances.

Parameters:

  • hash (Object)

    hash-like property serialization container

  • _excludes (Set) (defaults to: nil)

    ignored

Returns:

  • (Object)

    property hash-like serialization container



57
58
59
# File 'lib/firm/serialize/id.rb', line 57

def for_serialize(hash, _excludes = nil)
  hash
end

#from_serialized(_hash) ⇒ self (protected)

Noop for ID instances.

Parameters:

  • _hash (Hash)

    ignored

Returns:

  • (self)


66
67
68
69
# File 'lib/firm/serialize/id.rb', line 66

def from_serialized(_hash)
  # no deserializing necessary
  self
end

#init_from_serialized(_data) ⇒ Object (protected)

Initializes a newly allocated instance for subsequent deserialization (optionally initializing using the given data hash). The default implementation calls the standard #initialize method without arguments (default constructor) and leaves the property restoration to a subsequent call to the instance method #from_serialized(data). Classes that do not support a default constructor can override this class method and implement a custom initialization scheme.

Parameters:

  • _data (Object)

    hash-like object containing deserialized property data (symbol keys)

Returns:

  • (Object)

    the initialized object



47
48
49
50
# File 'lib/firm/serialize/id.rb', line 47

def init_from_serialized(_data)
  initialize
  self
end

#init_with(_coder) ⇒ Object



131
132
133
# File 'lib/firm/serializer/yaml.rb', line 131

def init_with(_coder)
  # noop
end

#inspectObject



92
93
94
# File 'lib/firm/serialize/id.rb', line 92

def inspect
  to_s
end

#serialize(pretty: false, format: Serializable.default_format) ⇒ String #serialize(io, pretty: false, format: Serializable.default_format) ⇒ IO

Serialize this object

Overloads:

  • #serialize(pretty: false, format: Serializable.default_format) ⇒ String

    Returns serialized data.

    Parameters:

    • pretty (Boolean) (defaults to: false)

      if true specifies to generate pretty formatted output if possible

    • format (Symbol, String) (defaults to: Serializable.default_format)

      specifies output format

    Returns:

    • (String)

      serialized data

  • #serialize(io, pretty: false, format: Serializable.default_format) ⇒ IO

    Parameters:

    • io (IO)

      output stream to write serialized data to

    • pretty (Boolean) (defaults to: false)

      if true specifies to generate pretty formatted output if possible

    • format (Symbol, String) (defaults to: Serializable.default_format)

      specifies output format

    Returns:

    • (IO)


35
36
37
# File 'lib/firm/serialize/id.rb', line 35

def serialize(io = nil, pretty: false, format: Serializable.default_format)
  Serializable[format].dump(self, io, pretty: pretty)
end

#serialize_disabled?Boolean

Always returns false for IDs.

Returns:

  • (Boolean)


84
85
86
# File 'lib/firm/serialize/id.rb', line 84

def serialize_disabled?
  false
end

#to_iObject



96
97
98
# File 'lib/firm/serialize/id.rb', line 96

def to_i
  object_id
end

#to_sObject



88
89
90
# File 'lib/firm/serialize/id.rb', line 88

def to_s
  "FIRM::Serializable::ID<#{object_id}>"
end