Module: AwesomeXML::ClassMethods

Defined in:
lib/awesome_xml/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/awesome_xml/class_methods.rb', line 6

def context
  @context
end

#nodesObject (readonly)

Returns an array of symbols containing all method names defined by node builder methods in your class.



63
64
65
# File 'lib/awesome_xml/class_methods.rb', line 63

def nodes
  @nodes
end

#public_nodesObject (readonly)

Returns an array of symbols containing all method names defined by node builder methods in your class. Does not list nodes built with option ‘:private`.



69
70
71
# File 'lib/awesome_xml/class_methods.rb', line 69

def public_nodes
  @public_nodes
end

Instance Method Details

#constant_node(name, value, options = {}) ⇒ Object

Defines a method on your class returning a constant.



31
32
33
34
35
36
37
# 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) ⇒ Object

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



41
42
43
44
# 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) ⇒ Object

Defines a method on your class returning a parsed value



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object

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.



11
12
13
# File 'lib/awesome_xml/class_methods.rb', line 11

def parse(xml)
  new(xml).parse
end

#parsing_type?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/awesome_xml/class_methods.rb', line 73

def parsing_type?
  false
end

#set_context(xpath) ⇒ Object

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.



18
19
20
# File 'lib/awesome_xml/class_methods.rb', line 18

def set_context(xpath)
  @context ||= xpath
end

#with_context(xpath, &block) ⇒ Object

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



24
25
26
27
28
# File 'lib/awesome_xml/class_methods.rb', line 24

def with_context(xpath, &block)
  @local_context = xpath
  yield
  @local_context = nil
end