prawn_cocktail 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 1.9.3
4
5
  - rbx-19mode
data/README.md CHANGED
@@ -42,7 +42,7 @@ class InvoiceDocument < PrawnCocktail::Document
42
42
  {
43
43
  number: @invoice.id,
44
44
  amount: @invoice.amount,
45
- customer_name: @invoice.customer_name
45
+ customer: { name: @invoice.customer_name }
46
46
  }
47
47
  end
48
48
  end
@@ -54,7 +54,7 @@ The document has `render` and `render_file(name)` methods, just like `Prawn::Doc
54
54
 
55
55
  The filename is not required. Other libraries like `PrawnCocktailRails` may make use of it.
56
56
 
57
- The `data` value is passed to the template as an `OpenStruct`.
57
+ The `data` value is passed to the template as a recursive `OpenStruct`-like object.
58
58
 
59
59
  If the data becomes complex, you are advised to extract one or many builder classes like `InvoiceDocument::Data` and call those from `data`.
60
60
 
@@ -72,13 +72,15 @@ content do |data|
72
72
  move_down 10
73
73
  text "Amount: #{data.amount}"
74
74
  move_down 10
75
- text data.customer_name
75
+ text data.customer.name
76
76
  end
77
77
  ```
78
78
 
79
79
  The `meta` declaration is optional. It takes a hash which will be passed to `Prawn::Document.new`. This is where you specify `page_size`, `page_layout` and such.
80
80
 
81
- The `content` block will be passed the data from the document as an `OpenStruct`, and will be rendered in the context of a `Prawn::Document` instance.
81
+ The `content` block will be passed the data from the document as a recursive `OpenStruct`-type object (actually a `RecursiveClosedStruct` utility class).
82
+
83
+ The `content` block will be rendered in the context of a `Prawn::Document` instance.
82
84
 
83
85
  ### Helpers
84
86
 
@@ -1,7 +1,5 @@
1
- require "prawn"
2
1
  require "active_support/core_ext/class/attribute"
3
2
  require "active_support/inflector"
4
-
5
3
  require_relative "renderer"
6
4
 
7
5
  module PrawnCocktail
@@ -33,7 +31,7 @@ module PrawnCocktail
33
31
  private
34
32
 
35
33
  def renderer
36
- @renderer ||= Renderer.new(template_name, data, self.class.initializers)
34
+ @renderer ||= Renderer.new(template_name, data, initializers)
37
35
  end
38
36
 
39
37
  def template_name
@@ -44,5 +42,9 @@ module PrawnCocktail
44
42
  # Override in your subclass.
45
43
  {}
46
44
  end
45
+
46
+ def initializers
47
+ self.class.initializers
48
+ end
47
49
  end
48
50
  end
@@ -1,4 +1,6 @@
1
+ require "prawn"
1
2
  require_relative "template"
3
+ require_relative "utils/recursive_closed_struct"
2
4
 
3
5
  module PrawnCocktail
4
6
  class Renderer
@@ -37,11 +39,15 @@ module PrawnCocktail
37
39
  end
38
40
 
39
41
  def prawn_document
40
- @doc ||= Prawn::Document.new(@prawn_document_options || {})
42
+ @doc ||= Prawn::Document.new(prawn_document_options)
43
+ end
44
+
45
+ def prawn_document_options
46
+ @prawn_document_options || {}
41
47
  end
42
48
 
43
49
  def data_object
44
- OpenStruct.new(@data)
50
+ RecursiveClosedStruct.new(@data)
45
51
  end
46
52
  end
47
53
  end
@@ -0,0 +1,22 @@
1
+ class RecursiveClosedStruct
2
+ def initialize(hash)
3
+ @hash = hash
4
+ end
5
+
6
+ def method_missing(name, *)
7
+ value = fetch(name)
8
+ if value.is_a?(Hash)
9
+ self.class.new(value)
10
+ else
11
+ value
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def fetch(name)
18
+ @hash.fetch(name) do
19
+ raise NoMethodError.new("undefined method `#{name}' for #{self}")
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module PrawnCocktail
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -15,7 +15,7 @@ class TestDocument < PrawnCocktail::Document
15
15
  private
16
16
 
17
17
  def data
18
- { status: @status }
18
+ { nested: { status: @status } }
19
19
  end
20
20
  end
21
21
 
@@ -2,5 +2,5 @@ meta page_size: "A4"
2
2
 
3
3
  content do |data|
4
4
  text "Sub test document"
5
- status_line data.status
5
+ status_line data.nested.status
6
6
  end
@@ -2,5 +2,5 @@ meta page_size: "A4"
2
2
 
3
3
  content do |data|
4
4
  text "Test document"
5
- status_line data.status
5
+ status_line data.nested.status
6
6
  end
@@ -1,5 +1,4 @@
1
- require "minitest/autorun"
2
- require "minitest/pride"
1
+ require_relative "spec_helper"
3
2
  require "pdf/inspector"
4
3
 
5
4
  require "prawn_cocktail"
@@ -14,11 +13,14 @@ describe PrawnCocktail do
14
13
 
15
14
  describe "#render" do
16
15
  it "has the right contents" do
17
- assert_document_has_the_right_contents
16
+ assert_equal(
17
+ [ "Init works.", "Test document", "Status: success" ],
18
+ parse_strings(data)
19
+ )
18
20
  end
19
21
 
20
22
  it "has the right geometry" do
21
- assert_document_has_the_right_geometry
23
+ assert_equal expected_geometry("A4"), parse_geometry(data)
22
24
  end
23
25
  end
24
26
 
@@ -29,11 +31,14 @@ describe PrawnCocktail do
29
31
  end
30
32
 
31
33
  it "has the right contents" do
32
- assert_document_has_the_right_contents
34
+ assert_equal(
35
+ [ "Init works.", "Test document", "Status: success" ],
36
+ parse_strings(data)
37
+ )
33
38
  end
34
39
 
35
40
  it "has the right geometry" do
36
- assert_document_has_the_right_geometry
41
+ assert_equal expected_geometry("A4"), parse_geometry(data)
37
42
  end
38
43
  end
39
44
 
@@ -51,20 +56,6 @@ describe PrawnCocktail do
51
56
  end
52
57
  end
53
58
 
54
- def assert_document_has_the_right_contents
55
- assert_equal(
56
- [ "Init works.", "Test document", "Status: success" ],
57
- parse_strings(data)
58
- )
59
- end
60
-
61
- def assert_document_has_the_right_geometry
62
- assert_equal(
63
- parse_geometry(data),
64
- expected_geometry("A4")
65
- )
66
- end
67
-
68
59
  def parse_strings(pdf_data)
69
60
  PDF::Inspector::Text.analyze(pdf_data).strings
70
61
  end
@@ -0,0 +1,24 @@
1
+ require_relative "spec_helper"
2
+
3
+ require_relative "../lib/prawn_cocktail/utils/recursive_closed_struct"
4
+
5
+ describe RecursiveClosedStruct do
6
+ it "provides readers from a hash" do
7
+ subject = RecursiveClosedStruct.new(key: "value")
8
+ assert_equal "value", subject.key
9
+ end
10
+
11
+ it "raises when there's no such key" do
12
+ subject = RecursiveClosedStruct.new(key: "value")
13
+ assert_raises(NoMethodError) do
14
+ subject.other_key
15
+ end
16
+ end
17
+
18
+ it "recurses through hashes" do
19
+ subject = RecursiveClosedStruct.new({
20
+ one: { two: { three: "four" } }
21
+ })
22
+ assert_equal "four", subject.one.two.three
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
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.6.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-24 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prawn
16
- requirement: &70112015079480 !ruby/object:Gem::Requirement
16
+ requirement: &70266162371220 !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: *70112015079480
24
+ version_requirements: *70266162371220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70112015095380 !ruby/object:Gem::Requirement
27
+ requirement: &70266162370800 !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: *70112015095380
35
+ version_requirements: *70266162370800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70112015094900 !ruby/object:Gem::Requirement
38
+ requirement: &70266162370380 !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: *70112015094900
46
+ version_requirements: *70266162370380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pdf-inspector
49
- requirement: &70112015094400 !ruby/object:Gem::Requirement
49
+ requirement: &70266162369920 !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: *70112015094400
57
+ version_requirements: *70266162369920
58
58
  description:
59
59
  email:
60
60
  - henrik@barsoom.se
@@ -72,12 +72,15 @@ files:
72
72
  - lib/prawn_cocktail/document.rb
73
73
  - lib/prawn_cocktail/renderer.rb
74
74
  - lib/prawn_cocktail/template.rb
75
+ - lib/prawn_cocktail/utils/recursive_closed_struct.rb
75
76
  - lib/prawn_cocktail/version.rb
76
77
  - prawn_cocktail.gemspec
77
78
  - spec/fixtures/document.rb
78
79
  - spec/fixtures/sub_test_document.pdf.rb
79
80
  - spec/fixtures/test_document.pdf.rb
80
81
  - spec/integration_spec.rb
82
+ - spec/recursive_closed_struct_spec.rb
83
+ - spec/spec_helper.rb
81
84
  homepage: ''
82
85
  licenses: []
83
86
  post_install_message:
@@ -107,4 +110,6 @@ test_files:
107
110
  - spec/fixtures/sub_test_document.pdf.rb
108
111
  - spec/fixtures/test_document.pdf.rb
109
112
  - spec/integration_spec.rb
113
+ - spec/recursive_closed_struct_spec.rb
114
+ - spec/spec_helper.rb
110
115
  has_rdoc: