From 38ad27857f875abcc4204efde53fadc165c53a00 Mon Sep 17 00:00:00 2001 From: Romaric Date: Mon, 21 Nov 2022 16:10:10 +0000 Subject: [PATCH] Stop UniqueIdentifierGenerator from being a Singleton It no longer needs to be as its lifecycle is now fully tied to the Redcarpet renderer --- lib/govuk_tech_docs/tech_docs_html_renderer.rb | 4 ++-- lib/govuk_tech_docs/unique_identifier_generator.rb | 10 +--------- spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb | 3 --- .../unique_identifier_generator_spec.rb | 12 +----------- 4 files changed, 4 insertions(+), 25 deletions(-) diff --git a/lib/govuk_tech_docs/tech_docs_html_renderer.rb b/lib/govuk_tech_docs/tech_docs_html_renderer.rb index 35fa653a..3eef19e3 100644 --- a/lib/govuk_tech_docs/tech_docs_html_renderer.rb +++ b/lib/govuk_tech_docs/tech_docs_html_renderer.rb @@ -12,7 +12,7 @@ def initialize(options = {}) end def preprocess(document) - UniqueIdentifierGenerator.instance.reset + @unique_identifier_generator = UniqueIdentifierGenerator.new document end @@ -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) %(#{text}\n) end diff --git a/lib/govuk_tech_docs/unique_identifier_generator.rb b/lib/govuk_tech_docs/unique_identifier_generator.rb index 62b932c5..6006fcb7 100644 --- a/lib/govuk_tech_docs/unique_identifier_generator.rb +++ b/lib/govuk_tech_docs/unique_identifier_generator.rb @@ -1,15 +1,11 @@ -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) @@ -28,10 +24,6 @@ def create(id, level) anchor end - def reset - @anchors = [] - end - private def prefixed_by_parent(anchor, level) diff --git a/spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb b/spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb index 5ed75d39..fcba3f8f 100644 --- a/spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb +++ b/spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb @@ -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 diff --git a/spec/govuk_tech_docs/unique_identifier_generator_spec.rb b/spec/govuk_tech_docs/unique_identifier_generator_spec.rb index 074ff1a0..6c79f851 100644 --- a/spec/govuk_tech_docs/unique_identifier_generator_spec.rb +++ b/spec/govuk_tech_docs/unique_identifier_generator_spec.rb @@ -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 @@ -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