class Object

Public Instance Methods

RPath(graph = nil, adapter = nil, &block) click to toggle source

Constructs an RPath expression and optionally evaluates it on a graph.

@overload RPath

Constructs an RPath expression
@example Construct an expression
  exp = RPath { foo.bar }
@example Construct an expression beginning with an uppercase letter
  exp = RPath { |root| root.Users.alice }
@yieldparam [RPath::Root] root
  The {RPath::Root} of the RPath expression. You should almost
  always omit this yield paramter. Use it only to avoid an exception if the
  first letter of your expression is uppercase. See the example above.
@return [RPath::Expression]
@see file:README.md

@overload RPath(graph, adapter = nil)

Constructs an RPath expression, evaluates it, and returns the result
@example Construct and expression and evaluate it on an XML document
  RPath.use :nokogiri
  xml = Nokogiri::XML('<foo bar="baz"/>')
  RPath(xml) { foo['bar'] } # => "baz"
@example Construct an expression and evaluate it with a custom adapter
  RPath(graph, CustomAdapter.new) { foo.bar }
@example Construct an expression and evaluate it with a custom adapter that has been registered
  RPath(graph, :custom) { foo.bar }
@example Construct an expression and evaluate it, letting RPath infer the adapter
  RPath(graph) { foo.bar }
@param [Object] graph
  The graph on which to evaluate the expression.
@param [RPath::Adapter, Symbol, nil] adapter
  The adapter with which to evaluate the expression. If the adapter has been
  registered with {RPath.use}, its id (a symbol) may be given as a 
  shortcut. If +nil+, RPath attempts to infer the adapter by calling
  {RPath::Adapter#adapts?} on registered adapters.
@yieldparam [RPath::Root] root
  The {RPath::Root} of the RPath expression. You should almost
  always omit this yield parameter. Use it only to avoid an exception if the
  first letter of your expression is uppercase. See the example above.
@return [Object]
@see file:README.md
@see RPath.use
# File lib/rpath.rb, line 93
def RPath(graph = nil, adapter = nil, &block)
  exp = RPath::Root.new

  if block_given?
    exp = block.arity > 0 ? block.call(exp) : exp.instance_eval(&block)
  end

  graph ? exp.eval(graph, adapter) : exp
end