class Rambling::Trie::Serializers::Yaml

Serializer for Ruby yaml format (.yaml, or .yml) files.

Attributes

serializer[R]

Public Class Methods

new(serializer = nil) click to toggle source

Creates a new Yaml serializer. @param [Serializer] serializer the serializer responsible to write to

and read from disk.
Calls superclass method
# File lib/rambling/trie/serializers/yaml.rb, line 11
def initialize serializer = nil
  @serializer = serializer || Rambling::Trie::Serializers::File.new
  super()
end

Public Instance Methods

dump(node, filepath) click to toggle source

Serializes a {Nodes::Node Node} and dumps it as a YAML object into filepath. @param [Nodes::Node] node the node to serialize @param [String] filepath the full path of the file to dump the YAML

object into.

@return [Numeric] number of bytes written to disk. @see ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-dump

Psych.dump
# File lib/rambling/trie/serializers/yaml.rb, line 44
def dump node, filepath
  require 'yaml'
  serializer.dump ::YAML.dump(node), filepath
end
load(filepath) click to toggle source

Loads serialized object from YAML file in filepath and deserializes it into a {Nodes::Node Node}. @param [String] filepath the full path of the file to load the

serialized YAML object from.

@return [Nodes::Node] The deserialized {Nodes::Node Node}. @see ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load

Psych.safe_load
# File lib/rambling/trie/serializers/yaml.rb, line 23
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