Sha256: 4af2049e725d07ad17804c1f165664370fa1d539395cf1772905362c55fd0084

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacConfig
  class PathsHash
    require_sub __FILE__

    class << self
      def parse_entry_key(entry_key) # rubocop:disable Metrics/CyclomaticComplexity
        r = entry_key.to_s.strip
        raise ::EacConfig::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 ::EacConfig::PathsHash::EntryKeyError, "Entry key \"#{entry_key}\" is empty"
        end
        return r.map(&:to_sym) unless r.any?(&:blank?)

        raise ::EacConfig::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(::EacConfig::PathsHash::PathSearch.parse_entry_key(entry_key))
    end

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

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

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

    delegate :to_h, to: :root

    # @return [EacConfig::PathsHash
    def write(entry_key, entry_value)
      self[entry_key] = entry_value

      self
    end

    private

    attr_reader :data
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
eac_config-0.14.3 lib/eac_config/paths_hash.rb
eac_tools-0.97.2 sub/eac_config/lib/eac_config/paths_hash.rb
eac_config-0.14.2 lib/eac_config/paths_hash.rb