Sha256: a87cabf89528d8a279796784a183ed4e6a3822da6ce7c3222175311109b80db3

Contents?: true

Size: 1.53 KB

Versions: 79

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacConfig
  class EntryPath
    PART_SEPARATOR = '.'

    class << self
      def assert(source)
        source.is_a?(self) ? source : new(build_parts(source))
      end

      def build_parts(source)
        return validate_parts!(source.split(PART_SEPARATOR)) if source.is_a?(::String)
        return validate_parts!(source.flat_map { |part| build_parts(part) }) if
        source.is_a?(::Enumerable)

        build_parts(source.to_s)
      end

      def validate_parts!(parts)
        parts.assert_argument(::Enumerable, 'parts')
        parts.each_with_index do |part, index|
          raise ::ArgumentError, "Part #{index} of #{parts} is blank" if part.blank?
          raise ::ArgumentError, "Part #{index} is not a string" unless part.is_a?(::String)
        end
      end
    end

    common_constructor :parts, default: [[]] do
      self.parts = ::Array.new(parts).freeze
    end

    compare_by :parts
    delegate :any?, :count, :each, :each_value, :each_with_index, :empty?, :fetch, :first, :index,
             :inject, :last, :map, :size, to: :parts

    # @return [EacConfig::EntryPath]
    def +(other)
      self.class.new(parts + self.class.assert(other).parts)
    end

    def [](*args)
      slice(*args)
    end

    def slice(*args)
      r = parts.slice(*args)
      r.is_a?(::Enumerable) ? self.class.new(r) : r
    end

    def to_s
      "#{self.class}[#{to_string}]"
    end

    # @return [String]
    def to_string
      parts.join(PART_SEPARATOR)
    end
  end
end

Version data entries

79 entries across 79 versions & 2 rubygems

Version Path
eac_tools-0.70.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.70.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.69.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.69.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.68.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.67.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.67.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.66.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.65.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.65.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.64.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.63.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.62.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.62.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.61.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.61.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.60.3 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.60.2 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.60.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.60.0 sub/eac_config/lib/eac_config/entry_path.rb