-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement strip_component source feature #1128
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,13 @@ class Name | |
INVALID_CHARACTERS = %r[\W] | ||
|
||
def initialize(name, opts) | ||
@name = name | ||
@opts = opts | ||
|
||
@source = opts[:source] | ||
@prefix = opts[:prefix] | ||
@invalid = opts[:invalid] | ||
|
||
@name = derive_name(name, opts[:strip_component]) | ||
@opts = opts | ||
|
||
case @invalid | ||
when 'correct_and_warn' | ||
@validate = true | ||
|
@@ -71,8 +71,24 @@ def dirname | |
|
||
private | ||
|
||
def derive_prefix(source,prefix) | ||
def derive_name(name, strip_component) | ||
if strip_component == nil | ||
name | ||
elsif %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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, it's simpler to test the class of the input then doing a rescue afterwards. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Funny how code structure sometimes reveals developer (my) chronological thought process, and highlights afterthoughts 😅 . Pushing improvement. |
||
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) | ||
if prefix == true | ||
"#{source}_" | ||
elsif prefix.is_a? String | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
false
a value you could see users put in here? Should we instead handle "falsey" values?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that would make sense. Updated to handle "falsey" instead of nil.