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
eac_ruby_utils-0.58.1 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.88.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.87.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
ehbrs-tools-0.19.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.58.0 lib/eac_ruby_utils/paths_hash.rb
ehbrs-tools-0.18.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.57.2 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.87.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.57.1 lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.57.0 lib/eac_ruby_utils/paths_hash.rb
ehbrs-tools-0.17.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.86.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.85.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.56.2 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.85.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.56.1 lib/eac_ruby_utils/paths_hash.rb
ehbrs-tools-0.16.5 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.84.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb
eac_ruby_utils-0.56.0 lib/eac_ruby_utils/paths_hash.rb
avm-tools-0.83.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb