Sha256: d84a52dee86276c556341c4b55e44ae4a2b4efb6df0a6c56e0774d74c6d80640

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require 'yaml'

module DiverDown
  class Web
    class ModuleStore
      BLANK_ARRAY = [].freeze
      BLANK_RE = /\A\s*\z/

      private_constant(:BLANK_RE)

      def initialize(path)
        @path = path
        @store = load
      end

      # @param source_name [String]
      # @param module_names [Array<String>]
      def set(source_name, module_names)
        @store[source_name] = module_names.dup.reject do
          BLANK_RE.match?(_1)
        end.freeze
      end

      # @param source_name [String]
      # @return [Array<Module>]
      def get(source_name)
        @store[source_name] || BLANK_ARRAY
      end

      # @return [Hash]
      def to_h
        @store.dup
      end

      # Write store to file
      # @return [void]
      def flush
        File.write(@path, to_h.to_yaml)
      end

      private

      def load
        store = {}

        begin
          loaded = YAML.load_file(@path)
          store.merge!(loaded) if loaded
        rescue StandardError
          # Ignore error
        end

        store
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
diver_down-0.0.1.alpha3 lib/diver_down/web/module_store.rb
diver_down-0.0.1.alpha2 lib/diver_down/web/module_store.rb
diver_down-0.0.1.alpha1 lib/diver_down/web/module_store.rb