Class: Benchmark::Memory::HeldResults::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/memory/held_results/serializer.rb

Overview

Serialize objects for holding between runs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil) ⇒ Serializer

Instantiate a new serializer.

Parameters:

  • object (Object) (defaults to: nil)

    The object to serialize.



21
22
23
# File 'lib/benchmark/memory/held_results/serializer.rb', line 21

def initialize(object = nil)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns The object to serialize.

Returns:

  • (Object)

    The object to serialize.



26
27
28
# File 'lib/benchmark/memory/held_results/serializer.rb', line 26

def object
  @object
end

Class Method Details

.load(json) ⇒ Object

Load an object from a JSON document.

Parameters:

  • json (String)

    A JSON document as a string.

Returns:

  • (Object)

    The object converted from the JSON document.



13
14
15
16
# File 'lib/benchmark/memory/held_results/serializer.rb', line 13

def self.load(json)
  json = JSON.parse(json) if json.is_a?(String)
  new.load(json).object
end

Instance Method Details

#load(_hash) ⇒ Object

Convert a JSON document into an object.

Parameters:

  • _hash (Hash)

    A JSON document hash.

Returns:

  • (Object)

Raises:

  • (NotImplementedError)

    If the inheriting subclass didn’t implement.



35
36
37
38
39
40
# File 'lib/benchmark/memory/held_results/serializer.rb', line 35

def load(_hash)
  fail(
    NotImplementedError,
    "You must implement a concrete version in a subclass"
  )
end

#to_hHash

Convert the object to a Hash.

Returns:

  • (Hash)

    The object as a Hash.

Raises:

  • (NotImplementedError)

    If the inheriting subclass didn’t implement.



47
48
49
50
51
52
# File 'lib/benchmark/memory/held_results/serializer.rb', line 47

def to_h
  fail(
    NotImplementedError,
    "You must implement a concrete version in a subclass"
  )
end

#to_jsonString Also known as: to_s

Convert the object to a JSON document.

Returns:

  • (String)

    The object as a JSON document.



57
58
59
# File 'lib/benchmark/memory/held_results/serializer.rb', line 57

def to_json
  JSON.generate(to_h)
end