Sha256: dcc149aa69cbefa49a4f03bd3fc69d02f2c6a01288acb42e522ee6d0b0e12d91

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Rambling
  module Trie
    module Serializers
      # Serializer for Ruby yaml format (.yaml) files.
      class Yaml
        # Creates a new Yaml serializer.
        # @param [Serializer] serializer the serializer responsible to write to
        #   and read from disk.
        def initialize serializer = nil
          @serializer = serializer || Rambling::Trie::Serializers::File.new
        end

        # 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 https://ruby-doc.org/stdlib-2.5.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load
        #   Psych.safe_load
        def load filepath
          require 'yaml'
          ::YAML.safe_load(
            serializer.load(filepath),
            [
              Symbol,
              Rambling::Trie::Nodes::Raw,
              Rambling::Trie::Nodes::Compressed,
            ],
            [],
            true,
          )
        end

        # 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 https://ruby-doc.org/stdlib-2.5.0/libdoc/psych/rdoc/Psych.html#method-c-dump
        #   Psych.dump
        def dump node, filepath
          require 'yaml'
          serializer.dump ::YAML.dump(node), filepath
        end

        private

        attr_reader :serializer
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rambling-trie-2.2.1 lib/rambling/trie/serializers/yaml.rb
rambling-trie-2.2.0 lib/rambling/trie/serializers/yaml.rb
rambling-trie-opal-2.1.1.1 lib/rambling/trie/serializers/yaml.rb
rambling-trie-opal-2.1.1 lib/rambling/trie/serializers/yaml.rb
rambling-trie-2.1.1 lib/rambling/trie/serializers/yaml.rb
rambling-trie-2.1.0 lib/rambling/trie/serializers/yaml.rb
rambling-trie-2.0.0 lib/rambling/trie/serializers/yaml.rb