Skip to content

Commit 9a93376

Browse files
committed
Introduce PrawnHtml::Instance class to preserve the context
1 parent 0261e60 commit 9a93376

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

lib/prawn-html.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ module PrawnHtml
156156
}.freeze
157157

158158
def append_html(pdf, html)
159-
pdf_wrapper = PdfWrapper.new(pdf)
160-
renderer = DocumentRenderer.new(pdf_wrapper)
161-
html_parser = PrawnHtml::HtmlParser.new(renderer)
162-
html_parser.process(html)
159+
PrawnHtml::Instance.new(pdf).append(html: html)
163160
end
164161

165162
module_function :append_html
@@ -179,3 +176,4 @@ def append_html(pdf, html)
179176
require 'prawn_html/pdf_wrapper'
180177
require 'prawn_html/document_renderer'
181178
require 'prawn_html/html_parser'
179+
require 'prawn_html/instance'

lib/prawn_html/instance.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module PrawnHtml
4+
class Instance
5+
attr_reader :html_parser, :pdf_wrapper, :renderer
6+
7+
def initialize(pdf)
8+
@pdf_wrapper = PrawnHtml::PdfWrapper.new(pdf)
9+
@renderer = PrawnHtml::DocumentRenderer.new(@pdf_wrapper)
10+
@html_parser = PrawnHtml::HtmlParser.new(@renderer)
11+
end
12+
13+
def append(html:)
14+
html_parser.process(html)
15+
end
16+
end
17+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe PrawnHtml::Instance do
4+
describe 'initialize' do
5+
subject(:instance) { described_class.new(pdf) }
6+
7+
let(:pdf) { instance_double(Prawn::Document) }
8+
9+
before do
10+
allow(PrawnHtml::PdfWrapper).to receive(:new)
11+
allow(PrawnHtml::DocumentRenderer).to receive(:new)
12+
allow(PrawnHtml::HtmlParser).to receive(:new)
13+
end
14+
15+
it 'initializes the required entities', :aggregate_failures do
16+
instance
17+
expect(PrawnHtml::PdfWrapper).to have_received(:new)
18+
expect(PrawnHtml::DocumentRenderer).to have_received(:new)
19+
expect(PrawnHtml::HtmlParser).to have_received(:new)
20+
end
21+
end
22+
23+
describe '#append' do
24+
subject(:append) { described_class.new(pdf).append(html: html) }
25+
26+
let(:html) { '<h1>Some HTML</h1>' }
27+
let(:html_parser) { instance_double(PrawnHtml::HtmlParser, process: nil) }
28+
let(:pdf) { instance_double(Prawn::Document) }
29+
30+
before do
31+
allow(PrawnHtml::HtmlParser).to receive(:new).and_return(html_parser)
32+
end
33+
34+
it 'sends a process message to the html parser' do
35+
append
36+
expect(html_parser).to have_received(:process).with(html)
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)