Class: ConventionalCommits::BranchNameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/branch/branch_name_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader: reader = Configuration::BranchConfigurationReader.new) ⇒ BranchNameGenerator

Returns a new instance of BranchNameGenerator.



7
8
9
# File 'lib/branch/branch_name_generator.rb', line 7

def initialize(reader: reader = Configuration::BranchConfigurationReader.new)
  @reader = reader
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



5
6
7
# File 'lib/branch/branch_name_generator.rb', line 5

def reader
  @reader
end

Instance Method Details

#branch_name_components(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/branch/branch_name_generator.rb', line 23

def branch_name_components(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH)
  configuration = reader.get_configuration(path:)
  out = components_using(_input, configuration.pattern)

  scope_is_included = out.length == Configuration::MAX_ELEMENTS_IN_PATTERN
  idx_adj = scope_is_included ? 1 : 0
  components = { scope: nil, type: nil, ticket_number: nil, description: nil }
  components[:scope] = out.length == Configuration::MAX_ELEMENTS_IN_PATTERN ? sanitize(out[0]) : nil
  components[:type] = out[0 + idx_adj]
  components[:ticket_number] = out[1 + idx_adj]
  components[:description] = out[2 + idx_adj]
  components
end

#generate_name_for(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/branch/branch_name_generator.rb', line 11

def generate_name_for(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH)
  configuration = reader.get_configuration(path:)
  delimiters = find_delimiters_from_pattern(pattern: configuration.pattern)
  input_order = transform_input_using_delimiters(_input, delimiters)

  name = input_order.each_with_index.reduce("") do |acc, (e, i)|
    acc + e + (delimiters[i] || (i + 1 == input_order.length ? "" : "-"))
  end

  name.downcase if configuration.lowercase == true
end

#is_valid_branch(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object



37
38
39
# File 'lib/branch/branch_name_generator.rb', line 37

def is_valid_branch(_input, path: Configuration::DEFAULT_CONFIGURATION_PATH)
  !branch_name_components(_input, path:).empty?
end