prawn_cocktail 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,17 @@ require_relative "renderer"
6
6
 
7
7
  module PrawnCocktail
8
8
  class Document
9
+ class_attribute :doc_initializers
10
+ self.doc_initializers ||= []
11
+
12
+ def self.initialize_document(&block)
13
+ self.doc_initializers << block
14
+ end
15
+
16
+ def self.helper(mod)
17
+ initialize_document { extend mod }
18
+ end
19
+
9
20
  def render
10
21
  renderer.render_data
11
22
  end
@@ -16,27 +27,22 @@ module PrawnCocktail
16
27
 
17
28
  def filename
18
29
  # Override in your subclass.
30
+ nil
19
31
  end
20
32
 
21
33
  private
22
34
 
23
35
  def renderer
24
- @renderer ||= Renderer.new(template, data, self.class.doc_initializers)
36
+ @renderer ||= Renderer.new(template_name, data, self.class.doc_initializers)
25
37
  end
26
38
 
27
- def template
39
+ def template_name
28
40
  self.class.name.underscore
29
41
  end
30
42
 
31
- class_attribute :doc_initializers
32
- self.doc_initializers ||= []
33
-
34
- def self.initialize_document(&block)
35
- self.doc_initializers << block
36
- end
37
-
38
- def self.helper(mod)
39
- initialize_document { extend mod }
43
+ def data
44
+ # Override in your subclass.
45
+ {}
40
46
  end
41
47
  end
42
48
  end
@@ -1,7 +1,9 @@
1
+ require_relative "template"
2
+
1
3
  module PrawnCocktail
2
4
  class Renderer
3
- def initialize(template, data, doc_initializers)
4
- @template = template
5
+ def initialize(template_name, data, doc_initializers)
6
+ @template_name = template_name
5
7
  @data = data
6
8
  @doc_initializers = doc_initializers
7
9
  end
@@ -12,46 +14,34 @@ module PrawnCocktail
12
14
 
13
15
  def content(&block)
14
16
  @doc_initializers.each do |proc|
15
- doc.instance_eval(&proc)
17
+ prawn_document.instance_eval(&proc)
16
18
  end
17
19
 
18
- doc.instance_exec(data_object, &block)
20
+ prawn_document.instance_exec(data_object, &block)
19
21
  end
20
22
 
21
23
  def render_data
22
- render
23
- doc.render
24
+ apply_template
25
+ prawn_document.render
24
26
  end
25
27
 
26
28
  def render_file(file)
27
- render
28
- doc.render_file(file)
29
+ apply_template
30
+ prawn_document.render_file(file)
29
31
  end
30
32
 
31
33
  private
32
34
 
33
- def render
34
- instance_eval(read_template, template_path)
35
+ def apply_template
36
+ Template.new(@template_name).apply(self)
35
37
  end
36
38
 
37
- def doc
39
+ def prawn_document
38
40
  @doc ||= Prawn::Document.new(@prawn_document_options || {})
39
41
  end
40
42
 
41
43
  def data_object
42
44
  OpenStruct.new(@data)
43
45
  end
44
-
45
- def read_template
46
- File.read(template_path)
47
- end
48
-
49
- def template_path
50
- File.join(template_root, "#{@template}.pdf.rb")
51
- end
52
-
53
- def template_root
54
- PrawnCocktail.template_root
55
- end
56
46
  end
57
47
  end
@@ -0,0 +1,25 @@
1
+ module PrawnCocktail
2
+ class Template
3
+ def initialize(name)
4
+ @name = name
5
+ end
6
+
7
+ def apply(context)
8
+ context.instance_eval(read_template, template_path)
9
+ end
10
+
11
+ private
12
+
13
+ def read_template
14
+ File.read(template_path)
15
+ end
16
+
17
+ def template_path
18
+ File.join(template_root, "#{@name}.pdf.rb")
19
+ end
20
+
21
+ def template_root
22
+ PrawnCocktail.template_root
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module PrawnCocktail
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -8,28 +8,44 @@ PrawnCocktail.template_root = "spec/fixtures"
8
8
  require_relative "fixtures/document"
9
9
 
10
10
  describe PrawnCocktail do
11
+ let(:data) do
12
+ TestDocument.new("success").render
13
+ end
14
+
11
15
  describe "#render" do
12
- it "works" do
13
- data = TestDocument.new("success").render
14
- assert_test_document data
16
+ it "has the right contents" do
17
+ assert_document_has_the_right_contents
18
+ end
19
+
20
+ it "has the right geometry" do
21
+ assert_document_has_the_right_geometry
15
22
  end
16
23
  end
17
24
 
18
25
  describe "#render_file" do
19
- it "works" do
26
+ let(:data) do
20
27
  TestDocument.new("success").render_file("/tmp/test_document.pdf")
21
- data = File.read("/tmp/test_document.pdf")
22
- assert_test_document data
28
+ File.read("/tmp/test_document.pdf")
29
+ end
30
+
31
+ it "has the right contents" do
32
+ assert_document_has_the_right_contents
33
+ end
34
+
35
+ it "has the right geometry" do
36
+ assert_document_has_the_right_geometry
23
37
  end
24
38
  end
25
39
  end
26
40
 
27
- def assert_test_document(data)
41
+ def assert_document_has_the_right_contents
28
42
  assert_equal(
29
43
  [ "Init works.", "Test document", "Status: success" ],
30
44
  parse_strings(data)
31
45
  )
46
+ end
32
47
 
48
+ def assert_document_has_the_right_geometry
33
49
  assert_equal(
34
50
  parse_geometry(data),
35
51
  expected_geometry("A4")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn_cocktail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prawn
16
- requirement: &70216788921060 !ruby/object:Gem::Requirement
16
+ requirement: &70204700961000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70216788921060
24
+ version_requirements: *70204700961000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70216788920580 !ruby/object:Gem::Requirement
27
+ requirement: &70204700960520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70216788920580
35
+ version_requirements: *70204700960520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70216776430360 !ruby/object:Gem::Requirement
38
+ requirement: &70204700774940 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70216776430360
46
+ version_requirements: *70204700774940
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pdf-inspector
49
- requirement: &70216776429880 !ruby/object:Gem::Requirement
49
+ requirement: &70204700774460 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70216776429880
57
+ version_requirements: *70204700774460
58
58
  description:
59
59
  email:
60
60
  - henrik@barsoom.se
@@ -71,6 +71,7 @@ files:
71
71
  - lib/prawn_cocktail/configuration.rb
72
72
  - lib/prawn_cocktail/document.rb
73
73
  - lib/prawn_cocktail/renderer.rb
74
+ - lib/prawn_cocktail/template.rb
74
75
  - lib/prawn_cocktail/version.rb
75
76
  - prawn_cocktail.gemspec
76
77
  - spec/fixtures/document.rb