Class: Katello::Util::PathWithSubstitutions
- Inherits:
-
Object
- Object
- Katello::Util::PathWithSubstitutions
- Includes:
- Comparable
- Defined in:
- app/lib/katello/util/path_with_substitutions.rb
Constant Summary collapse
- SUBSTITUTABLE_REGEX =
/^(.*?)\$([^\/]*)/.freeze
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#substitutions ⇒ Object
readonly
Returns the value of attribute substitutions.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #apply_substitutions ⇒ Object
-
#initialize(path, substitutions) ⇒ PathWithSubstitutions
constructor
path /content/rhel/server/$arch/$releasever/os substitutions => ‘x86_64’.
- #resolve_token(value) ⇒ Object
- #split_path ⇒ Object
- #substitutable? ⇒ Boolean
- #substitutions_needed ⇒ Object
- #unused_substitutions ⇒ Object
Constructor Details
#initialize(path, substitutions) ⇒ PathWithSubstitutions
path /content/rhel/server/$arch/$releasever/os substitutions => ‘x86_64’
12 13 14 15 16 17 18 19 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 12 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 |
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
6 7 8 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 6 def base_path @base_path end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 6 def path @path end |
#substitutions ⇒ Object (readonly)
Returns the value of attribute substitutions.
6 7 8 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 6 def substitutions @substitutions end |
Instance Method Details
#<=>(other) ⇒ Object
53 54 55 56 57 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 53 def <=>(other) key1 = path + substitutions.to_s key2 = other.path + other.substitutions.to_s key1 <=> key2 end |
#apply_substitutions ⇒ Object
47 48 49 50 51 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 47 def apply_substitutions substitutions.reduce(path) do |url, (key, value)| url.gsub("$#{key}", value) end end |
#resolve_token(value) ⇒ Object
35 36 37 38 39 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 35 def resolve_token(value) new_substitutions = substitutions.merge(@token => value) new_path = path.sub("$#{@token}", value) PathWithSubstitutions.new(new_path, new_substitutions) end |
#split_path ⇒ Object
21 22 23 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 21 def split_path @split ||= path.split('/') end |
#substitutable? ⇒ Boolean
31 32 33 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 31 def substitutable? @token.present? end |
#substitutions_needed ⇒ Object
25 26 27 28 29 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 25 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 |
#unused_substitutions ⇒ Object
41 42 43 44 45 |
# File 'app/lib/katello/util/path_with_substitutions.rb', line 41 def unused_substitutions substitutions.keys.reject do |key| path.include?("$#{key}") || split_path.include?(substitutions[key]) end end |