Class: Brightbox::Config::SectionNameDeduplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/brightbox-cli/config/section_name_deduplicator.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(name, in_use) ⇒ SectionNameDeduplicator

Returns a new instance of SectionNameDeduplicator.

Parameters:

  • name (String)

    The name to deduplicate

  • in_use (Array<String>)

    The names already in use in the config



13
14
15
16
# File 'lib/brightbox-cli/config/section_name_deduplicator.rb', line 13

def initialize(name, in_use)
  @name = name
  @in_use = in_use
end

Instance Method Details

#nextString

Return the next name given those in use already

Returns:

  • (String)


21
22
23
24
25
26
27
28
29
30
31
32
# 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