Sha256: f47e9b217ba60513846347fcdbf09ceb680d9071796ba423a9546e35296c4c9a

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 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

    attr_reader :parts

    def initialize(parts)
      @parts = parts.to_a.freeze
    end

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

    def to_s
      "#{self.class}[#{parts.join(PART_SEPARATOR)}]"
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
eac_tools-0.12.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.11.1 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.11.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.10.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_tools-0.9.0 sub/eac_config/lib/eac_config/entry_path.rb
eac_config-0.9.0 lib/eac_config/entry_path.rb
eac_tools-0.8.0 sub/eac_config/lib/eac_config/entry_path.rb