Sha256: 1c9cf8814136b451c50664d0c51fc2540e11290e2a1f77c5b2b2bc771d0171d5

Contents?: true

Size: 1.6 KB

Versions: 62

Compression:

Stored size: 1.6 KB

Contents

module Katello
  module Util
    class PathWithSubstitutions
      include Comparable

      attr_reader :base_path, :path, :substitutions

      SUBSTITUTABLE_REGEX = /^(.*?)\$([^\/]*)/.freeze

      #path /content/rhel/server/$arch/$releasever/os
      #substitutions  {$arch => 'x86_64'}
      def initialize(path, substitutions)
        @substitutions = substitutions
        @path = path

        if @path =~ SUBSTITUTABLE_REGEX
          @base_path, @token = Regexp.last_match[1], Regexp.last_match[2]
        end
      end

      def split_path
        @split ||= path.split('/')
      end

      def substitutions_needed
        # e.g. if content_url = "/content/dist/rhel/server/7/$releasever/$basearch/kickstart"
        #      return ['releasever', 'basearch']
        split_path.map { |word| word.start_with?('$') ? word[1..-1] : nil }.compact
      end

      def substitutable?
        @token.present?
      end

      def resolve_token(value)
        new_substitutions = substitutions.merge(@token => value)
        new_path = path.sub("$#{@token}", value)
        PathWithSubstitutions.new(new_path, new_substitutions)
      end

      def unused_substitutions
        substitutions.keys.reject do |key|
          path.include?("$#{key}") || split_path.include?(substitutions[key])
        end
      end

      def apply_substitutions
        substitutions.reduce(path) do |url, (key, value)|
          url.gsub("$#{key}", value)
        end
      end

      def <=>(other)
        key1 = path + substitutions.to_s
        key2 = other.path + other.substitutions.to_s
        key1 <=> key2
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
katello-4.13.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.13.0 app/lib/katello/util/path_with_substitutions.rb
katello-4.12.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.13.0.rc1 app/lib/katello/util/path_with_substitutions.rb
katello-4.12.0 app/lib/katello/util/path_with_substitutions.rb
katello-4.12.0.rc3 app/lib/katello/util/path_with_substitutions.rb
katello-4.12.0.rc2 app/lib/katello/util/path_with_substitutions.rb
katello-4.12.0.rc1 app/lib/katello/util/path_with_substitutions.rb
katello-4.11.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.11.0 app/lib/katello/util/path_with_substitutions.rb
katello-4.11.0.rc2 app/lib/katello/util/path_with_substitutions.rb
katello-4.11.0.rc1 app/lib/katello/util/path_with_substitutions.rb
katello-4.10.0 app/lib/katello/util/path_with_substitutions.rb
katello-4.9.2 app/lib/katello/util/path_with_substitutions.rb
katello-4.10.0.rc2 app/lib/katello/util/path_with_substitutions.rb
katello-4.10.0.rc1 app/lib/katello/util/path_with_substitutions.rb
katello-4.8.4 app/lib/katello/util/path_with_substitutions.rb
katello-4.9.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.8.3 app/lib/katello/util/path_with_substitutions.rb
katello-4.9.0 app/lib/katello/util/path_with_substitutions.rb