Module: AwesomeXML::ClassMethods
- Defined in:
- lib/awesome_xml/class_methods.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#nodes ⇒ Object
readonly
Returns an array of symbols containing all method names defined by node builder methods in your class.
-
#public_nodes ⇒ Object
readonly
Returns an array of symbols containing all method names defined by node builder methods in your class.
Instance Method Summary collapse
-
#constant_node(name, value, options = {}) ⇒ Object
Defines a method on your class returning a constant.
-
#method_node(name) ⇒ Object
Does not actually define a method, but registers the node name in the ‘@nodes` attribute.
-
#node(name, type, options = {}, &block) ⇒ Object
Defines a method on your class returning a parsed value.
-
#parse(xml) ⇒ Object
Takes in a string representing an XML document.
- #parsing_type? ⇒ Boolean
-
#set_context(xpath) ⇒ Object
Takes in a string representing an XPath and assigns it to the class variable ‘@@context`.
-
#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.
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/awesome_xml/class_methods.rb', line 6 def context @context end |
#nodes ⇒ Object (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_nodes ⇒ Object (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, = {}) attr_reader name.to_sym define_method("parse_#{name}".to_sym) do instance_variable_set("@#{name}", value) end register(name, [: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, = {}, &block) attr_reader name.to_sym [:local_context] = @local_context xpath = NodeXPath.new(name, ).xpath define_method("parse_#{name}".to_sym) do evaluate_args = [xpath, AwesomeXML::Type.for(type, self.class.name), ] instance_variable_set( "@#{name}", evaluate_nodes(*evaluate_args, &block) ) end register(name, [: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
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 |