Skip to content

Instantly share code, notes, and snippets.

@activefx
Created April 4, 2015 04:55
Show Gist options
  • Save activefx/cb5da877014d40b4702d to your computer and use it in GitHub Desktop.
Save activefx/cb5da877014d40b4702d to your computer and use it in GitHub Desktop.
module Nokoquery
module DSL
module Item
def item(name, *args, **options, &block)
modulizer = Nokoquery::Modulizer.new(module_type: 'Item', name: name, klass: self)
method_module = modulizer.module_for
mod_name = modulizer.module_name
method_module.class_variable_set(:"@@_#{mod_name}_#{name}_block_capture", block)
method_module.class_variable_set(:"@@_#{mod_name}_#{name}_options", options)
line_no = __LINE__; method_defs = %{
def #{name}
options = Nokoquery::Options::Item.new(@@_#{mod_name}_#{name}_options)
node = if #{args}.first || options.selector_specified?
send(options.selector_type, options.selector { #{args}.first })
else
self
end
options.before_operations.each { |op| node = op.call(node) }
node = if @@_#{mod_name}_#{name}_block_capture
if @@_#{mod_name}_#{name}_block_capture.arity == 1
@@_#{mod_name}_#{name}_block_capture.call(node)
else
node.instance_eval(&@@_#{mod_name}_#{name}_block_capture)
end
else
node
end
options.after_operations.each { |op| node = op.call(node) }
if options.keep?(node)
node = node.is_a?(Nokogiri::XML::Node) ? Nokoquery::Node.new(node, self, self.options) : node
block_given? ? (yield node) : node
else
nil
end
end
}
method_module.module_eval method_defs, __FILE__, line_no
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment