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.



23
24
25
# File 'lib/benchmark/memory/held_results/serializer.rb', line 23

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

Instance Attribute Details

#objectObject (readonly)

Returns The object to serialize.

Returns:

  • (Object)

    The object to serialize.



28
29
30
# File 'lib/benchmark/memory/held_results/serializer.rb', line 28

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.



15
16
17
18
# File 'lib/benchmark/memory/held_results/serializer.rb', line 15

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.



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

def load(_hash)
  raise(
    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.



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

def to_h
  raise(
    NotImplementedError,
    'You must implement a concrete version in a subclass'
  )
end

#to_json(*args) ⇒ String Also known as: to_s

Convert the object to a JSON document.

Returns:

  • (String)

    The object as a JSON document.



59
60
61
# File 'lib/benchmark/memory/held_results/serializer.rb', line 59

def to_json(*args)
  JSON.generate(to_h, *args)
end