Class: REXML::Object::Base
- Inherits:
-
Object
- Object
- REXML::Object::Base
- Defined in:
- lib/voruby/misc/rexml_ext.rb
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#xml_element_name ⇒ Object
readonly
Returns the value of attribute xml_element_name.
Class Method Summary collapse
Instance Method Summary collapse
- #find_namespace_by_href(href) ⇒ Object
- #get_attribute(name, ns = nil) ⇒ Object
- #get_element(name, ns = nil) ⇒ Object
- #get_elements(name, ns = nil) ⇒ Object
-
#initialize(defn = nil) ⇒ Base
constructor
A new instance of Base.
- #set_attribute(name, value, ns = nil) ⇒ Object
- #set_element(name, value, ns) ⇒ Object
- #set_elements(name, values, ns = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(defn = nil) ⇒ Base
Returns a new instance of Base.
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/voruby/misc/rexml_ext.rb', line 134 def initialize(defn=nil) @node = case defn when Document then defn.root when Element then defn when Hash then Element.new(self.xml_element_name) when NilClass then Element.new(self.xml_element_name) else Document.new(defn).root end initialize_members(defn) if defn.is_a?(Hash) end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
128 129 130 |
# File 'lib/voruby/misc/rexml_ext.rb', line 128 def node @node end |
#xml_element_name ⇒ Object (readonly)
Returns the value of attribute xml_element_name.
128 129 130 |
# File 'lib/voruby/misc/rexml_ext.rb', line 128 def xml_element_name @xml_element_name end |
Class Method Details
.xml_type ⇒ Object
132 |
# File 'lib/voruby/misc/rexml_ext.rb', line 132 def self.xml_type; self.class.to_s.split('::').last end |
.xsd_namespace ⇒ Object
131 |
# File 'lib/voruby/misc/rexml_ext.rb', line 131 def self.xsd_namespace; 'http://www.w3.org/2001/XMLSchema' end |
.xsi_namespace ⇒ Object
130 |
# File 'lib/voruby/misc/rexml_ext.rb', line 130 def self.xsi_namespace; 'http://www.w3.org/2001/XMLSchema-instance' end |
Instance Method Details
#find_namespace_by_href(href) ⇒ Object
149 150 151 |
# File 'lib/voruby/misc/rexml_ext.rb', line 149 def find_namespace_by_href(href) self.node.namespaces.find{ |prefix, uri| uri == href } end |
#get_attribute(name, ns = nil) ⇒ Object
153 154 155 156 |
# File 'lib/voruby/misc/rexml_ext.rb', line 153 def get_attribute(name, ns=nil) attribute = self.node.attributes.get_attribute_ns(ns, name) attribute ? attribute.value : nil end |
#get_element(name, ns = nil) ⇒ Object
162 163 164 |
# File 'lib/voruby/misc/rexml_ext.rb', line 162 def get_element(name, ns=nil) XPath.first(self.node, *element_xpath_for(name, ns)) end |
#get_elements(name, ns = nil) ⇒ Object
183 184 185 |
# File 'lib/voruby/misc/rexml_ext.rb', line 183 def get_elements(name, ns=nil) ReflectiveElementList.new(self.node, *element_xpath_for(name, ns)) end |
#set_attribute(name, value, ns = nil) ⇒ Object
158 159 160 |
# File 'lib/voruby/misc/rexml_ext.rb', line 158 def set_attribute(name, value, ns=nil) self.node.attributes[attribute_xpath_for(name, ns)] = value.to_s end |
#set_element(name, value, ns) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/voruby/misc/rexml_ext.rb', line 166 def set_element(name, value, ns) el = get_element(name, ns) # The element doesn't exist... if !el el = REXML::Element.new(name) @node << el # so insert it end case value when String then el.text = value.to_s when REXML::Element then value.name = name; el.replace_with(value) end el.add_namespace(ns) if !find_namespace_by_href(ns) end |
#set_elements(name, values, ns = nil) ⇒ Object
187 188 189 |
# File 'lib/voruby/misc/rexml_ext.rb', line 187 def set_elements(name, values, ns=nil) get_elements(name, ns).replace(values) end |
#to_s ⇒ Object
191 192 193 194 195 196 |
# File 'lib/voruby/misc/rexml_ext.rb', line 191 def to_s formatter = Formatters::Default.new xml = '' formatter.write(self.node, xml) xml end |