-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to call transform's method from the given block
- Loading branch information
1 parent
f61daf0
commit 0ec7ab2
Showing
4 changed files
with
146 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
# Provides a context for tree transformations to run in. The context allows | ||
# accessing each of the bindings in the bindings hash as local method. | ||
# | ||
# Example: | ||
# Example: | ||
# | ||
# ctx = Context.new(:a => :b) | ||
# ctx.instance_eval do | ||
# ctx.instance_eval do | ||
# a # => :b | ||
# end | ||
# | ||
# @api private | ||
class Parslet::Context | ||
include Parslet | ||
|
||
def initialize(bindings) | ||
def initialize(bindings, transform = nil) | ||
@__transform = transform if transform | ||
bindings.each do |key, value| | ||
singleton_class.send(:define_method, key) { value } | ||
instance_variable_set("@#{key}", value) | ||
end | ||
end | ||
end | ||
|
||
def respond_to_missing?(method, include_private) | ||
super || @__transform&.respond_to?(method, true) || false | ||
end | ||
|
||
if RUBY_VERSION >= '3' | ||
def method_missing(method, *args, **kwargs, &block) | ||
if @__transform&.respond_to?(method, true) | ||
@__transform.__send__(method, *args, **kwargs, &block) | ||
else | ||
super | ||
end | ||
end | ||
else | ||
def method_missing(method, *args, &block) | ||
if @__transform&.respond_to?(method, true) | ||
@__transform.__send__(method, *args, &block) | ||
else | ||
super | ||
end | ||
end | ||
|
||
ruby2_keywords :method_missing | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.