-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
Default custom XPath function handler #464
Conversation
Hi! I'm not 100% opposed to having a default object that we evaluate our context against, but I don't like this API. I would rather that people just do # Class eval form
Nokogiri::XML::XPathFunctions.class_eval do
def regex node_set, regexp
node_set.find_all { |n| n['whatever'] =~ /#{regexp}/ }
end
end
# Module Extend
Nokogiri::XML::XPathFunctions.extend(Module.new {
def regex node_set, regexp
node_set.find_all { |n| n['whatever'] =~ /#{regexp}/ }
end
})
# Module Extend + Super
Nokogiri::XML::XPathFunctions.extend(Module.new {
def regex node_set, regexp
super(node_set, regexp + "hi mom!")
end
}) I think this would simplify the implementation and allow people to use OO constructs more easily. The other concern I have with this is performance. If we have a default object that we eval against, we're going to be paying I'm luke warm on including this feature. If there are no performance issues, it's probably fine, but I'd like to hear from @flavorjones too. |
Thanks for the feedback. For what it's worth, I used a contained, anonymously-classed handler for two reasons. One reason was so I could clear out all the existing methods (other than Both of those goals are still achievable using The first version of this code existed as a |
First, let me say that I am totally on board with improving the API around declaring custom xpath handlers. Regarding performance: libxml2 is smart enough to not invoke our Which means that we don't have to worry about I'd also like to explicitly note that there's no reason we can't be backwards-compatible with this API: if the user chooses to implement his own class and pass an instance of it in as the last argument to In summary:
|
That's all good to know (especially the performance bit). Thanks. It simplifies the implementation quite a bit. I'm happy to replace swap out my API for @tenderlove's. I'd like to clarify a couple things before I do:
Opinion(s)? |
I've created an easy-to-decorate handler for custom XPath functions, and created code so that Node will use it in the absence of another handler:
A custom handler can still be passed to
Node#xpath()
to achieve the old behavior. To override the baked-in handler without defining a new handler or any new functions, passnil
.This functionality would allow contributors to build up mixable, shareable libraries of XPath functions without requiring large changes to consumer code.