Skip to content

Commit

Permalink
Stop UniqueIdentifierGenerator from being a Singleton
Browse files Browse the repository at this point in the history
It no longer needs to be as its lifecycle is now fully tied to the Redcarpet renderer
  • Loading branch information
romaricpascal committed Nov 21, 2022
1 parent 5db95e6 commit c63ea99
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/govuk_tech_docs/tech_docs_html_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def initialize(options = {})
end

def preprocess(document)
UniqueIdentifierGenerator.instance.reset

@unique_identifier_generator = UniqueIdentifierGenerator.new
document
end

Expand All @@ -22,7 +22,7 @@ def paragraph(text)
end

def header(text, level)
anchor = UniqueIdentifierGenerator.instance.create(text, level)
anchor = @unique_identifier_generator.create(text, level)
%(<h#{level} id="#{anchor}">#{text}</h#{level}>\n)
end

Expand Down
9 changes: 1 addition & 8 deletions lib/govuk_tech_docs/unique_identifier_generator.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
require "singleton"

module GovukTechDocs
class UniqueIdentifierGenerator
include Singleton

Anchor = Struct.new(:id, :level)

attr_reader :anchors

def initialize
reset
@anchors = []
end

def create(id, level)
Expand All @@ -28,10 +25,6 @@ def create(id, level)
anchor
end

def reset
@anchors = []
end

private

def prefixed_by_parent(anchor, level)
Expand Down
3 changes: 0 additions & 3 deletions spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ def hello_world
end

describe "#render unique heading IDs" do
# Reset the UniqueIdentifierGenerator between each tests to ensure independence
before(:each) { GovukTechDocs::UniqueIdentifierGenerator.instance.reset }

it "Automatically assigns an ID to the heading" do
output = processor.render <<~MARKDOWN
# A heading
Expand Down
12 changes: 1 addition & 11 deletions spec/govuk_tech_docs/unique_identifier_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
RSpec.describe GovukTechDocs::UniqueIdentifierGenerator do
subject { described_class.instance }
before { subject.reset }
subject { described_class.new }

describe "#create" do
it "lower-cases the text" do
Expand Down Expand Up @@ -66,13 +65,4 @@
end
end
end

describe "#reset" do
it "clears the list of existing anchors" do
subject.create("An Anchor", 1)
expect(subject.anchors).to_not be_empty
subject.reset
expect(subject.anchors).to be_empty
end
end
end

0 comments on commit c63ea99

Please sign in to comment.