Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it compatible with Ruby's BasicObject #21

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ end
template.render
```

### Hanami

Follow these steps:

1. Add `tilt-jbuilder` gem to your project.
1. Generate a template (eg: `apps/api/templates/events/index.json.jbuilder`)
1. Profit

## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
Expand Down
11 changes: 6 additions & 5 deletions lib/tilt/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def initialize_engine
def prepare; end

def evaluate(scope, locals, &block)
scope ||= Object.new
scope ||= BasicObject.new
::Tilt::Jbuilder.encode(scope) do |json|
context = scope.instance_eval { binding }
context = scope.instance_eval { ::Kernel.binding }
set_locals(locals, scope, context)
if data.kind_of?(Proc)
return data.call(::Tilt::Jbuilder.new(scope))
Expand All @@ -103,9 +103,10 @@ def evaluate(scope, locals, &block)
private
def set_locals(locals, scope, context)
view_path = options[:view_path]
scope.send(:instance_variable_set, '@_jbuilder_view_path', view_path)
scope.send(:instance_variable_set, '@_jbuilder_locals', locals)
scope.send(:instance_variable_set, '@_tilt_data', data)
scope.instance_exec(binding) {
@_jbuilder_view_path = view_path
@_jbuilder_locals = locals
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change? Is this for BasicObject compatibility or an unrelated changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's related because BasicObject doesn't respond to #send.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi-related: I removed the line that was setting data because it wasn't clear to me where data was previously instantiated, and because the tests were still passing without it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you feel like you have a good reason not to would you mind adding data back in? Removing it seems unrelated to this PR and I'd rather not risk introducing a bug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anthonator I see your concern and totally agree with it.

But if add that data back as third line, it raises a NoMethodError. Which leads us to the initial issue: I don't understand where is defined. 😉

set_locals = locals.keys.map { |k| "#{k} = @_jbuilder_locals[#{k.inspect}]" }.join("\n")
eval set_locals, context
end
Expand Down
5 changes: 5 additions & 0 deletions spec/tilt-jbuilder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"{\"author\":\"Anthony\"}".should == template.render(Object.new, :name => 'Anthony')
end

it "should pass basic object locals" do
template = Tilt::JbuilderTemplate.new { "json.author name" }
"{\"author\":\"Anthony\"}".should == template.render(BasicObject.new, :name => 'Anthony')
end

it "should evaluate in an object scope" do
template = Tilt::JbuilderTemplate.new { "json.author @name" }
scope = Object.new
Expand Down