class Brightbox::Config::SectionNameDeduplicator
The CLI
config file is broken into sections, each with a unique name. When adding a new section as a conveinence if a name would clash instead of the original error we select a suitable alternative.
If the name is not in use then it is returned unchanged.
Public Class Methods
Source
# File lib/brightbox-cli/config/section_name_deduplicator.rb, line 13 def initialize(name, in_use) @name = name @in_use = in_use end
@param [String] name The name to deduplicate @param [Array<String>] in_use The names already in use in the config
Public Instance Methods
Source
# File lib/brightbox-cli/config/section_name_deduplicator.rb, line 21 def next # Sanity check. If the name is not in use, offer it back if @in_use.include?(@name) @in_use.sort.select do |name| name == @name end "#{normalised_name}_#{next_suffix}" else @name end end
Return the next name given those in use already
@return [String]
Private Instance Methods
Source
# File lib/brightbox-cli/config/section_name_deduplicator.rb, line 48 def max_suffix_value related_names_in_use.map { |match| match.captures.first.to_i }.max end
Find the largest number suffix used
Source
# File lib/brightbox-cli/config/section_name_deduplicator.rb, line 53 def next_suffix if max_suffix_value.respond_to?(:next) max_suffix_value.next else # If it's not a number then something is wrong 1 end end
Find and increment the suffix
Source
# File lib/brightbox-cli/config/section_name_deduplicator.rb, line 37 def normalised_name /(.*?)(_\d+)?\z/.match(@name).captures.first end
Returns name with no numerical suffix (_d+)