Class: GovukTechDocs::UniqueIdentifierGenerator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/govuk_tech_docs/unique_identifier_generator.rb

Defined Under Namespace

Classes: Anchor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUniqueIdentifierGenerator

Returns a new instance of UniqueIdentifierGenerator.



11
12
13
# File 'lib/govuk_tech_docs/unique_identifier_generator.rb', line 11

def initialize
  reset
end

Instance Attribute Details

#anchorsObject (readonly)

Returns the value of attribute anchors.



9
10
11
# File 'lib/govuk_tech_docs/unique_identifier_generator.rb', line 9

def anchors
  @anchors
end

Instance Method Details

#create(id, level) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/govuk_tech_docs/unique_identifier_generator.rb', line 15

def create(id, level)
  anchor = heading_slug(id)

  unless unique?(anchor)
    anchor = prefixed_by_parent(anchor, level)
  end

  unless unique?(anchor)
    anchor = suffixed_with_number(anchor)
  end

  @anchors << Anchor.new(anchor, level)

  anchor
end

#resetObject



31
32
33
# File 'lib/govuk_tech_docs/unique_identifier_generator.rb', line 31

def reset
  @anchors = []
end