Sha256: 35e25e4a8e89e63ed97e127f6adf009794caafbe597739cc4d7432d3595e6bc2

Contents?: true

Size: 1.4 KB

Versions: 69

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacRubyUtils
  class PathsHash
    require_sub __FILE__

    class << self
      def parse_entry_key(entry_key)
        r = entry_key.to_s.strip
        raise ::EacRubyUtils::PathsHash::EntryKeyError, 'Entry key cannot start or end with dot' if
        r.start_with?('.') || r.end_with?('.')

        r = r.split('.').map(&:strip)
        if r.empty?
          raise ::EacRubyUtils::PathsHash::EntryKeyError, "Entry key \"#{entry_key}\" is empty"
        end
        return r.map(&:to_sym) unless r.any?(&:blank?)

        raise ::EacRubyUtils::PathsHash::EntryKeyError,
              "Entry key \"#{entry_key}\" has at least one blank part"
      end
    end

    attr_reader :root

    def initialize(source_hash = {})
      @root = Node.new(source_hash)
    end

    def [](entry_key)
      root.read_entry(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
    end

    def []=(entry_key, entry_value)
      root.write_entry(
        ::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key), entry_value
      )
    end

    def fetch(entry_key)
      root.fetch(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
    end

    def key?(entry_key)
      root.entry?(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
    end

    delegate :to_h, to: :root

    private

    attr_reader :data
  end
end

Version data entries

69 entries across 69 versions & 3 rubygems

Version Path
avm-tools-0.98.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.97.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.96.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.63.0 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.95.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.62.1 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.94.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.62.0 lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.61.0 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.94.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.60.0 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.94.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.59.0 lib/eac_ruby_utils/paths_hash.rb
ehbrs-tools-0.20.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.94.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.93.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.92.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.91.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.90.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.89.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb