Module: HappyMapper::ClassMethods
- Defined in:
- lib/happymapper.rb
Instance Method Summary collapse
- #after_parse(&block) ⇒ Object
- #after_parse_callbacks ⇒ Object
- #attribute(name, type, options = {}) ⇒ Object
- #attributes ⇒ Object
- #content(name) ⇒ Object
- #element(name, type, options = {}) ⇒ Object
- #elements ⇒ Object
- #has_many(name, type, options = {}) ⇒ Object
- #has_one(name, type, options = {}) ⇒ Object
-
#namespace(namespace = nil) ⇒ Object
Specify a namespace if a node and all its children are all namespaced elements.
- #parse(xml, options = {}) ⇒ Object
- #raw_content(name) ⇒ Object
- #register_namespace(namespace, ns) ⇒ Object
- #tag(new_tag_name) ⇒ Object
- #tag_name ⇒ Object
Instance Method Details
#after_parse(&block) ⇒ Object
53 54 55 |
# File 'lib/happymapper.rb', line 53 def after_parse(&block) after_parse_callbacks.push(block) end |
#after_parse_callbacks ⇒ Object
49 50 51 |
# File 'lib/happymapper.rb', line 49 def after_parse_callbacks @after_parse_callbacks ||= [] end |
#attribute(name, type, options = {}) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/happymapper.rb', line 21 def attribute(name, type, ={}) attribute = Attribute.new(name, type, ) @attributes[to_s] ||= [] @attributes[to_s] << attribute attr_accessor attribute.method_name.intern end |
#attributes ⇒ Object
28 29 30 |
# File 'lib/happymapper.rb', line 28 def attributes @attributes[to_s] || [] end |
#content(name) ⇒ Object
39 40 41 42 |
# File 'lib/happymapper.rb', line 39 def content(name) @content = name attr_accessor name end |
#element(name, type, options = {}) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/happymapper.rb', line 32 def element(name, type, ={}) element = Element.new(name, type, ) @elements[to_s] ||= [] @elements[to_s] << element attr_accessor element.method_name.intern end |
#elements ⇒ Object
57 58 59 |
# File 'lib/happymapper.rb', line 57 def elements @elements[to_s] || [] end |
#has_many(name, type, options = {}) ⇒ Object
65 66 67 |
# File 'lib/happymapper.rb', line 65 def has_many(name, type, ={}) element name, type, {:single => false}.merge() end |
#has_one(name, type, options = {}) ⇒ Object
61 62 63 |
# File 'lib/happymapper.rb', line 61 def has_one(name, type, ={}) element name, type, {:single => true}.merge() end |
#namespace(namespace = nil) ⇒ Object
Specify a namespace if a node and all its children are all namespaced elements. This is simpler than passing the :namespace option to each defined element.
72 73 74 75 |
# File 'lib/happymapper.rb', line 72 def namespace(namespace = nil) @namespace = namespace if namespace @namespace end |
#parse(xml, options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/happymapper.rb', line 89 def parse(xml, = {}) if xml.is_a?(XML::Node) node = xml else if xml.is_a?(XML::Document) node = xml.root else node = XML::Parser.string(xml).parse.root end root = node.name == tag_name end namespace = @namespace || (node.namespaces && node.namespaces.default) namespace = "#{DEFAULT_NS}:#{namespace}" if namespace xpath = root ? '/' : './/' xpath += "#{DEFAULT_NS}:" if namespace xpath += tag_name nodes = node.find(xpath, Array(namespace)) collection = nodes.collect do |n| obj = new attributes.each do |attr| obj.send("#{attr.method_name}=", attr.from_xml_node(n, namespace)) end elements.each do |elem| obj.send("#{elem.method_name}=", elem.from_xml_node(n, namespace)) end obj.send("#{@content}=", n.content) if @content obj.send("#{@raw_content}=", n.inner_xml) if @raw_content obj.class.after_parse_callbacks.each { |callback| callback.call(obj) } obj end # per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354 nodes = nil if [:single] || root collection.first else collection end end |
#raw_content(name) ⇒ Object
44 45 46 47 |
# File 'lib/happymapper.rb', line 44 def raw_content(name) @raw_content = name attr_accessor name end |
#register_namespace(namespace, ns) ⇒ Object
77 78 79 |
# File 'lib/happymapper.rb', line 77 def register_namespace(namespace, ns) @registered_namespaces.merge!(namespace => ns) end |
#tag(new_tag_name) ⇒ Object
81 82 83 |
# File 'lib/happymapper.rb', line 81 def tag(new_tag_name) @tag_name = new_tag_name.to_s end |
#tag_name ⇒ Object
85 86 87 |
# File 'lib/happymapper.rb', line 85 def tag_name @tag_name ||= to_s.split('::')[-1].downcase end |