Class: Rambling::Trie::Serializers::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/rambling/trie/serializers/yaml.rb

Overview

Serializer for Ruby yaml format (.yaml) files.

Instance Method Summary collapse

Constructor Details

#initialize(serializer = nil) ⇒ Yaml

Creates a new Yaml serializer.

Parameters:

  • serializer (Serializer) (defaults to: nil)

    the serializer responsible to write to and read from disk.



11
12
13
# File 'lib/rambling/trie/serializers/yaml.rb', line 11

def initialize serializer = nil
  @serializer = serializer || Rambling::Trie::Serializers::File.new
end

Instance Method Details

#dump(node, filepath) ⇒ Numeric

Serializes a Node and dumps it as a YAML object into filepath.

Parameters:

  • node (Nodes::Node)

    the node to serialize

  • filepath (String)

    the full path of the file to dump the YAML object into.

Returns:

  • (Numeric)

    number of bytes written to disk.

See Also:



44
45
46
47
# File 'lib/rambling/trie/serializers/yaml.rb', line 44

def dump node, filepath
  require 'yaml'
  serializer.dump ::YAML.dump(node), filepath
end

#load(filepath) ⇒ Nodes::Node

Loads serialized object from YAML file in filepath and deserializes it into a Node.

Parameters:

  • filepath (String)

    the full path of the file to load the serialized YAML object from.

Returns:

See Also:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rambling/trie/serializers/yaml.rb', line 22

def load filepath
  require 'yaml'
  ::YAML.safe_load(
    serializer.load(filepath),
    [
      Symbol,
      Rambling::Trie::Nodes::Raw,
      Rambling::Trie::Nodes::Compressed,
    ],
    [],
    true,
  )
end