module AwesomeXML::ClassMethods

Attributes

context[R]
nodes[R]
public_nodes[R]

Public Instance Methods

constant_node(name, value, options = {}) click to toggle source

Defines a method on your class returning a constant.

# File lib/awesome_xml/class_methods.rb, line 31
def constant_node(name, value, options = {})
  attr_reader name.to_sym
  define_method("parse_#{name}".to_sym) do
    instance_variable_set("@#{name}", value)
  end
  register(name, options[:private])
end
method_node(name) click to toggle source

Does not actually define a method, but registers the node name in the ‘@nodes` attribute.

# File lib/awesome_xml/class_methods.rb, line 41
def method_node(name)
  define_method("parse_#{name}".to_sym) {}
  register(name, false)
end
node(name, type, options = {}, &block) click to toggle source

Defines a method on your class returning a parsed value

# File lib/awesome_xml/class_methods.rb, line 47
def node(name, type, options = {}, &block)
  attr_reader name.to_sym
  options[:local_context] = @local_context
  xpath = NodeXPath.new(name, options).xpath
  define_method("parse_#{name}".to_sym) do
    evaluate_args = [xpath, AwesomeXML::Type.for(type, self.class.name), options]
    instance_variable_set(
      "@#{name}",
      evaluate_nodes(*evaluate_args, &block)
    )
  end
  register(name, options[:private])
end
parse(xml) click to toggle source

Takes in a string representing an XML document. Initializes an instance of the class the module was included in and calls ‘#parse` on it. See there for more info.

# File lib/awesome_xml/class_methods.rb, line 11
def parse(xml)
  new(xml).parse
end
parsing_type?() click to toggle source
# File lib/awesome_xml/class_methods.rb, line 73
def parsing_type?
  false
end
set_context(xpath) click to toggle source

Takes in a string representing an XPath and assigns it to the class variable ‘@@context`. This sets the current context node for all nodes defined below it in the class this module is included in.

# File lib/awesome_xml/class_methods.rb, line 18
def set_context(xpath)
  @context ||= xpath
end
with_context(xpath) { || ... } click to toggle source

Works just like ‘set_context`, but sets the current context node only for nodes defined inside the block passed to this method.

# File lib/awesome_xml/class_methods.rb, line 24
def with_context(xpath, &block)
  @local_context = xpath
  yield
  @local_context = nil
end

Private Instance Methods

register(node_name, privateness) click to toggle source
# File lib/awesome_xml/class_methods.rb, line 79
def register(node_name, privateness)
  @nodes ||= []
  @nodes << node_name.to_sym
  @public_nodes ||= []
  @public_nodes << node_name.to_sym unless privateness
end