Skip to content

Commit

Permalink
Improve strip_component based on PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmv committed Feb 25, 2021
1 parent 3f33dac commit 76c99a9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/r10k/environment/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ def dirname
private

def derive_name(name, strip_component)
if strip_component == nil
name
elsif %r{^/.*/$}.match(strip_component)
return name unless strip_component

unless strip_component.is_a?(String)
raise _('Improper configuration value given for strip_component setting in %{src} source. ' \
'Value must be a string, a /regex/, false, or omitted. Got "%{val}" (%{type})' \
% {src: @source, val: strip_component, type: strip_component.class})
end

if %r{^/.*/$}.match(strip_component)
regex = Regexp.new(strip_component[1..-2])
name.gsub(regex, '')
elsif name.start_with?(strip_component)
name[strip_component.size..-1]
else
name
end
rescue TypeError, NoMethodError
raise _('Improper configuration value given for strip_component setting in %{src} source. ' \
'Value must be a string, a /regex/, or omitted. Got "%{val}" (%{type})' \
% {src: @source, val: strip_component, type: strip_component.class})
end

def derive_prefix(source,prefix)
Expand Down

0 comments on commit 76c99a9

Please sign in to comment.