Module: Dubs::Pattern

Defined in:
lib/dubs/pattern.rb

Class Method Summary collapse

Class Method Details

.interpolate(template, adjective:, noun:, token:, separator: "-") ⇒ Object

Interpolate a pattern template with values.

Pattern.interpolate("{adjective}-{noun}-{token}", adjective: "vicious", noun: "sazabi", token: "a3f1", separator: "-")
# => "vicious-sazabi-a3f1"


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dubs/pattern.rb', line 12

def interpolate(template, adjective:, noun:, token:, separator: "-")
  result = template
    .gsub("{adjective}", adjective)
    .gsub("{noun}", noun)
    .gsub("{token}", token)

  # Replace literal hyphens in the template with the configured separator
  if separator != "-"
    result = result.gsub("-", separator)
  end

  # Clean up: remove trailing/leading separators if token is empty
  if token.empty?
    result = result
      .chomp(separator)
      .delete_prefix(separator)
      .gsub("#{separator}#{separator}", separator)
  end

  result
end

.resolve(pattern) ⇒ Object

Resolve a pattern name to its template string.



35
36
37
38
39
40
41
42
# File 'lib/dubs/pattern.rb', line 35

def resolve(pattern)
  case pattern
  when Symbol, String
    Data.pattern(pattern)
  else
    pattern.to_s
  end
end