Skip to content

Commit

Permalink
Add Proc, Lambda, Method, proc, lambda, and -> rendering support
Browse files Browse the repository at this point in the history
  • Loading branch information
bradgessler committed Feb 25, 2024
1 parent d734e5d commit ff35c46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/phlex/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ def after_template

def yield_content(&block)
return unless block_given?
yield self

if block.arity.zero?
# This handles lambdas and ->
yield
else
# This handles Proc and proc
yield self
end
end

def render(renderable, &block)
Expand All @@ -57,6 +64,8 @@ def render(renderable, &block)
@document.text renderable
when Class
render(renderable.new, &block) if renderable < Phlex::PDF
when Proc, Method
yield_content(&renderable)
when Enumerable
renderable.each { |r| render(r, &block) }
else
Expand Down
11 changes: 11 additions & 0 deletions spec/phlex/pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ def view_template
WarningComponent.new("Danger!"),
WarningComponent.new("Don't Panic!")
]

render Proc.new { text "Rendered from Proc.new", color: "00FF00" }
render proc { text "Rendered from proc", color: "00FFFF" }
render lambda { text "Rendered from lambda", color: "FFFF00" }
render -> { text "Rendered from ->", color: "99FF00" }
render method(:calm)

render BodyComponent do |body|
body.greet "Brad"
end
render FooterComponent.new
end

def calm
text "Rendered from Method", color: "F0F0F0"
end
end

RSpec.describe Phlex::PDF do
Expand Down

0 comments on commit ff35c46

Please sign in to comment.