phlex-pdf 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e265d912e998c60ecf6d2d9cd8d68bbd183769525b13e60c02b3a9c82af99699
4
- data.tar.gz: a7688fbcf405741dba49938da25cb91d2c9ac7fc70a34a67379ec2724b75b23f
3
+ metadata.gz: 670aa939ab2505d3783c38b9070adbce93e1ddf67c041c726efd7b4efed5296f
4
+ data.tar.gz: c1e31b9053d69c43c0fac102637fd2d63be0ef9e7f2002e33da8e2419462b90e
5
5
  SHA512:
6
- metadata.gz: d5986c26c44ebba40a95a9c254bf3e492d11e41d41b8b8e0dcb01745efe101739fcdc69fc1b62138113ae4479beda3e3dad115745b62f80dd2a0e69e056ed287
7
- data.tar.gz: 932a86b037f7a2258e6fd037bf0ebae5834f95d415cd96851838e65ea62ac9c77b255355b26da549a69cb151332c87392b5d49cc6d5a6327e6c4e657d15d9795
6
+ metadata.gz: e815d9f10d1f527d51c250021060354dedbe7ba09153aa7ecd043b02a6e7bb9eba93932f30b2519c3649f4d689961fd6987fe93665b69617d69408e2f2c67cb6
7
+ data.tar.gz: 3f2283f3b9a6965a34cf08ff7b169b425e493de5f40591e2ed45e09602004953dc00a8a0e639b413f241dd503a693847582a8e41ced0454bdcf52ee149cff22b
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Phlex::Pdf
1
+ # Phlex::PDF
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/phlex/pdf`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Phlex PDF lets you compose PDF files with components in pure Ruby. It's a thin layer that sits on the shoulder of giants, [Prawn](https://github.com/prawnpdf/prawn), that encourages a component-first approach to building PDF documents.
6
4
 
7
5
  ## Installation
8
6
 
@@ -16,6 +14,8 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
14
 
17
15
  ## Usage
18
16
 
17
+ `Phlex::PDF` is a thin wrapper around `Prawn::View`, so you'll want to become familiar with [PrawnPDF](http://prawnpdf.org/), particularly the [PrawnPDF Manual](https://prawnpdf.org/manual.pdf).
18
+
19
19
  ```ruby
20
20
  require "phlex/pdf"
21
21
 
@@ -46,7 +46,11 @@ class NoticeComponent < ApplicationComponent
46
46
  end
47
47
  end
48
48
 
49
- NoticeComponent.pdf.render_file "poof.pdf"
49
+ # Render it to a file
50
+ NoticeComponent.render_file "hello.pdf"
51
+
52
+ # Or to a string
53
+ NoticeComponent.render
50
54
  ```
51
55
 
52
56
  ## Development
@@ -57,7 +61,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
57
61
 
58
62
  ## Contributing
59
63
 
60
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/phlex-pdf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/phlex-pdf/blob/main/CODE_OF_CONDUCT.md).
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/phlex-ruby/phlex-pdf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/phlex-ruby/phlex-pdf/blob/main/CODE_OF_CONDUCT.md).
61
65
 
62
66
  ## License
63
67
 
@@ -65,4 +69,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
65
69
 
66
70
  ## Code of Conduct
67
71
 
68
- Everyone interacting in the Phlex::Pdf project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/phlex-pdf/blob/main/CODE_OF_CONDUCT.md).
72
+ Everyone interacting in the Phlex::PDF project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/phlex-ruby/phlex-pdf/blob/main/CODE_OF_CONDUCT.md).
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Pdf
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
data/lib/phlex/pdf.rb CHANGED
@@ -12,19 +12,82 @@ module Phlex
12
12
 
13
13
  def call(document, &block)
14
14
  @document = document
15
- before_template if respond_to?(:before_template)
16
- view_template(&block)
17
- after_template if respond_to?(:after_template)
15
+ around_template do
16
+ if block_given?
17
+ view_template do
18
+ yield_content(&block)
19
+ end
20
+ else
21
+ view_template
22
+ end
23
+ end
18
24
  end
19
25
 
20
- def render(component, &block)
21
- component.call(@document, &block)
26
+ # @abstract Override this method to hook in around a template render. You can do things before and after calling `super` to render the template. You should always call `super` so that callbacks can be added at different layers of the inheritance tree.
27
+ # @return [nil]
28
+ def around_template
29
+ before_template
30
+ yield
31
+ after_template
22
32
  nil
23
33
  end
24
34
 
25
- def self.document(document = Prawn::Document.new)
26
- new.call(document)
27
- document
35
+ # @abstract Override this method to hook in right before a template is rendered. Please remember to call `super` so that callbacks can be added at different layers of the inheritance tree.
36
+ # @return [nil]
37
+ def before_template
38
+ nil
39
+ end
40
+
41
+ # @abstract Override this method to hook in right after a template is rendered. Please remember to call `super` so that callbacks can be added at different layers of the inheritance tree.
42
+ # @return [nil]
43
+ def after_template
44
+ nil
45
+ end
46
+
47
+ def yield_content(&block)
48
+ return unless block_given?
49
+
50
+ if block.arity.zero?
51
+ # This handles lambdas and ->
52
+ yield
53
+ else
54
+ # This handles Proc and proc
55
+ yield self
56
+ end
57
+ end
58
+
59
+ def render(renderable, &block)
60
+ case renderable
61
+ when Phlex::PDF
62
+ renderable.call(@document, &block)
63
+ when String
64
+ @document.text renderable
65
+ when Class
66
+ render(renderable.new, &block) if renderable < Phlex::PDF
67
+ when Proc, Method
68
+ yield_content(&renderable)
69
+ when Enumerable
70
+ renderable.each { |r| render(r, &block) }
71
+ else
72
+ raise ArgumentError, "You can't render a #{renderable.inspect}."
73
+ end
74
+
75
+ nil
76
+ end
77
+
78
+ class << self
79
+ def document(document = Prawn::Document.new)
80
+ new.call(document)
81
+ document
82
+ end
83
+
84
+ def render(...)
85
+ document.render(...)
86
+ end
87
+
88
+ def render_file(...)
89
+ document.render_file(...)
90
+ end
28
91
  end
29
92
  end
30
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-24 00:00:00.000000000 Z
11
+ date: 2024-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn