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

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

Overview

Serializer for Ruby yaml format (.yaml, or .yml) 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.



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

def initialize serializer = nil
  super()
  @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:



37
38
39
40
# File 'lib/rambling/trie/serializers/yaml.rb', line 37

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:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rambling/trie/serializers/yaml.rb', line 19

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