Skip to content

Commit

Permalink
Support 0-argument lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Apr 18, 2023
1 parent 5e0ca1f commit 9a968d4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.1
3.2.2
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Unreleased

- `SGML#render` can now render lambdas with zero or one arguments. Previously, it could only render lambdas with one argument or procs with any number of arguments.
6 changes: 5 additions & 1 deletion lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ def render(renderable, &block)
when Enumerable
renderable.each { |r| render(r, &block) }
when Proc
yield_content(&renderable)
if renderable.arity == 0
yield_content_with_no_args(&renderable)
else
yield_content(&renderable)
end
else
raise ArgumentError, "You can't render a #{renderable}."
end
Expand Down
30 changes: 27 additions & 3 deletions test/phlex/view/renderable/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,38 @@ def template(&block)
end
end

with "proc" do
with "0-argument lambda" do
view do
define_method :template do
render proc { h1 { "Hi" } }
render -> { h1 { "Hi" } }
end
end

it "renders the other view" do
it "renders the lambda" do
expect(output).to be == "<h1>Hi</h1>"
end
end

with "1-argument lambda" do
view do
define_method :template do
render -> (_view) { h1 { "Hi" } }
end
end

it "renders the lambda" do
expect(output).to be == "<h1>Hi</h1>"
end
end

with "multi-argument proc" do
view do
define_method :template do
render proc { |_a, _b, _c| h1 { "Hi" } }
end
end

it "renders the lambda" do
expect(output).to be == "<h1>Hi</h1>"
end
end
Expand Down

0 comments on commit 9a968d4

Please sign in to comment.