Module: ExistDB::Dom::Mapper::ClassMethods
- Defined in:
- lib/existdb/dom/mapper.rb
Instance Method Summary collapse
- #attribute(name, type = String, options = {}) ⇒ Object
- #attributes ⇒ Object
- #element(name, type = String, options = {}, &block) ⇒ Object
- #elements ⇒ Object
- #find(*options) ⇒ Object
- #find_by_xquery(resource, query) ⇒ Object
- #get_attribute(tag_name, dom) ⇒ Object
- #get_element(tag_name, dom) ⇒ Object
- #get_elements(tag_name, klass, dom) ⇒ Object
- #is_dom_mapper? ⇒ Boolean
- #parse(resource, options = {}) ⇒ Object
- #set_attribute(dom, tag_name, value) ⇒ Object
- #set_element(dom, tag_name, value) ⇒ Object
- #tag(new_tag_name) ⇒ Object
- #tag_name ⇒ Object
Instance Method Details
#attribute(name, type = String, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/existdb/dom/mapper.rb', line 45 def attribute(name, type = String, = {}) a = Attribute.new(name, type, ) @attributes ||= Hash.new @attributes[a.tag] = a self.class_eval %{ def #{a.method_name} get_attribute(#{a.tag.inspect}) end def #{a.method_name}=(value) set_attribute(#{a.tag.inspect}, value) end } end |
#attributes ⇒ Object
60 61 62 |
# File 'lib/existdb/dom/mapper.rb', line 60 def attributes @attributes.values end |
#element(name, type = String, options = {}, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/existdb/dom/mapper.rb', line 64 def element(name, type = String, = {}, &block) e = Element.new(name, type, ) @elements ||= Hash.new @elements[e.tag] = e if (type.respond_to?(:is_dom_mapper?) and type.is_dom_mapper?) or block_given? then # Nested elements if block_given? then # Anonymous Mapper Class klass = self.class_eval %{ @@#{e.method_name}_klass = Class.new do include ExistDB::Dom::Mapper end } klass.instance_eval &block else self.class_eval %{ @@#{e.method_name}_klass = #{type} } end self.class_eval %{ def #{e.method_name} get_elements(#{e.tag.inspect}, @@#{e.method_name}_klass) end } else # No nested elements self.class_eval %{ def #{e.method_name} get_element(#{e.tag.inspect}) end def #{e.method_name}=(value) set_element(#{e.tag.inspect}, value) end } end end |
#elements ⇒ Object
100 101 102 |
# File 'lib/existdb/dom/mapper.rb', line 100 def elements @elements.values end |
#find(*options) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/existdb/dom/mapper.rb', line 191 def find(*) xql = XQLFactory::XQLFactory.new(*) if xql.node_xpath.nil? then xql.node_xpath = "//#{tag_name}" end if xql.doc.is_a?(Resource::Xml) then xql.doc.xquery(xql.xquery).map{ |res| new(res) } elsif xql.doc.is_a?(ResourceSet) then xql.doc.join.xquery(xql.xquery).map{ |res| new(res) } elsif xql.doc.is_a?(String) and Embedded.instance.running? Embedded.instance.xquery_service.query(xql.xquery).map{ |res| new(res) } else nil end end |
#find_by_xquery(resource, query) ⇒ Object
209 210 211 212 |
# File 'lib/existdb/dom/mapper.rb', line 209 def find_by_xquery(resource, query) nodes = resource.xquery(query) nodes.map{ |node| new(node) } end |
#get_attribute(tag_name, dom) ⇒ Object
121 122 123 124 125 |
# File 'lib/existdb/dom/mapper.rb', line 121 def get_attribute(tag_name, dom) a = @attributes[tag_name] value = dom.getAttributes.getNamedItem(tag_name).getValue rescue nil a.type_cast( value ) end |
#get_element(tag_name, dom) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/existdb/dom/mapper.rb', line 104 def get_element(tag_name, dom) e = @elements[tag_name] child = getFirstChildByTagName(dom, tag_name) return e.type_cast( child.getNodeValue ) if child.respond_to?(:getNodeValue) return nil end |
#get_elements(tag_name, klass, dom) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/existdb/dom/mapper.rb', line 111 def get_elements(tag_name, klass, dom) children = dom.getElementsByTagName(tag_name) size = children.getLength if size == 1 then klass.new(children.item(0)) elsif size > 1 then 0..size.map{ |i| klass.new(children.item(i)) } end end |
#is_dom_mapper? ⇒ Boolean
214 215 216 |
# File 'lib/existdb/dom/mapper.rb', line 214 def is_dom_mapper? true end |
#parse(resource, options = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/existdb/dom/mapper.rb', line 175 def parse(resource, = {}) if [:single] then return new(resource) else xpath = [:xpath] xpath ||= "//#{[:tag]}" if [:tag] xpath ||= "//#{[:name]}" if [:name] xpath ||= "//#{tag_name}" resource_set = resource.xquery(xpath) return resource_set.map{ |res| new(res) } end end |
#set_attribute(dom, tag_name, value) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/existdb/dom/mapper.rb', line 151 def set_attribute(dom, tag_name, value) attr = dom.getAttributes.getNamedItem(tag_name) new_attr = org.exist.dom.AttrImpl.new( org.exist.dom.QName.new( tag_name.to_s.to_java_string ), value.to_s.to_java_string ) ExistDB::Embedded.instance.transaction do |transaction| if attr then new_attr.setOwnerDocument( dom.getOwnerDocument ) dom.updateChild(transaction, attr, new_attr) else dom.appendChild(new_attr) end end return value end |
#set_element(dom, tag_name, value) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/existdb/dom/mapper.rb', line 127 def set_element(dom, tag_name, value) parent = dom node = getFirstChildByTagName(dom, tag_name) if node.nil? then doc = parent.getOwnerDocument node = doc.createElement( tag_name.to_s ) parent.appendChild(node) end child = node.getChildNodes.select{ |child| child.getNodeType == org.w3c.dom.Node.TEXT_NODE }.first text = org.exist.dom.TextImpl.new( value.to_s.to_java_string ) if child then ExistDB::Embedded.instance.transaction do |transaction| text.setOwnerDocument( node.getOwnerDocument ) node.updateChild(transaction, child, text) end else node.appendChild(text) end return value end |
#tag(new_tag_name) ⇒ Object
167 168 169 |
# File 'lib/existdb/dom/mapper.rb', line 167 def tag(new_tag_name) @tag_name = new_tag_name.to_s unless new_tag_name.nil? || new_tag_name.to_s.empty? end |
#tag_name ⇒ Object
171 172 173 |
# File 'lib/existdb/dom/mapper.rb', line 171 def tag_name @tag_name ||= to_s.split('::').last.downcase end |