Sha256: 14239d6b574a32c1ed24c46f4d7b42f3a562c58d4ef91b9c6da598e64ac82d56

Contents?: true

Size: 1.5 KB

Versions: 154

Compression:

Stored size: 1.5 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)
        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

154 entries across 154 versions & 3 rubygems

Version Path
eac_tools-0.82.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.81.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.80.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.79.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_config-0.14.1 lib/eac_config/paths_hash.rb
eac_tools-0.78.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.77.1 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.77.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.76.1 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.76.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.75.2 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.75.1 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.75.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.74.1 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.74.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_config-0.14.0 lib/eac_config/paths_hash.rb
eac_tools-0.73.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_config-0.13.0 lib/eac_config/paths_hash.rb
eac_tools-0.72.0 sub/eac_config/lib/eac_config/paths_hash.rb
eac_tools-0.70.1 sub/eac_config/lib/eac_config/paths_hash.rb