Class: EimXML::Element
- Inherits:
-
Object
- Object
- EimXML::Element
- Defined in:
- lib/eim_xml.rb
Direct Known Subclasses
Constant Summary collapse
- NEST =
" "
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #==(xml) ⇒ Object
- #[](key) ⇒ Object
- #add(v) ⇒ Object (also: #<<)
- #add_attribute(key, value) ⇒ Object (also: #[]=)
- #del_attribute(key) ⇒ Object
- #find(obj, dst = Element.new(:found)) ⇒ Object
- #has?(obj, attr = nil) ⇒ Boolean (also: #has_element?, #include?)
-
#initialize(name, attributes = {}) {|_self| ... } ⇒ Element
constructor
A new instance of Element.
- #match(obj, attr = nil) ⇒ Object (also: #=~)
- #name_and_attributes(out = "") ⇒ Object
- #pcstring_contents ⇒ Object
- #write_to(out = "") ⇒ Object (also: #to_s)
Constructor Details
#initialize(name, attributes = {}) {|_self| ... } ⇒ Element
Returns a new instance of Element.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eim_xml.rb', line 71 def initialize(name, attributes={}) @name = name.to_sym @attributes = Hash.new @contents = Array.new attributes.each do |k, v| @attributes[k.to_sym] = v end yield(self) if block_given? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
67 68 69 |
# File 'lib/eim_xml.rb', line 67 def attributes @attributes end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
67 68 69 |
# File 'lib/eim_xml.rb', line 67 def contents @contents end |
#name ⇒ Object
Returns the value of attribute name.
67 68 69 |
# File 'lib/eim_xml.rb', line 67 def name @name end |
Instance Method Details
#==(xml) ⇒ Object
133 134 135 136 |
# File 'lib/eim_xml.rb', line 133 def ==(xml) return false unless xml.is_a?(Element) @name==xml.name && @attributes==xml.attributes && @contents==xml.contents end |
#[](key) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/eim_xml.rb', line 143 def [](key) if key.is_a?(Fixnum) @contents[key] else @attributes[key.to_sym] end end |
#add(v) ⇒ Object Also known as: <<
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/eim_xml.rb', line 88 def add(v) case v when nil when Array v.each{|i| self.add(i)} else @contents << v end self end |
#add_attribute(key, value) ⇒ Object Also known as: []=
138 139 140 |
# File 'lib/eim_xml.rb', line 138 def add_attribute(key, value) @attributes[key.to_sym] = value end |
#del_attribute(key) ⇒ Object
151 152 153 |
# File 'lib/eim_xml.rb', line 151 def del_attribute(key) @attributes.delete(key.to_sym) end |
#find(obj, dst = Element.new(:found)) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/eim_xml.rb', line 201 def find(obj, dst=Element.new(:found)) return find(Element.new(obj, dst)) if dst.is_a?(Hash) dst << self if match(obj) @contents.each do |i| case when i.is_a?(Element) i.find(obj, dst) when obj.is_a?(Module) && i.is_a?(obj) dst << i end end dst end |
#has?(obj, attr = nil) ⇒ Boolean Also known as: has_element?, include?
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/eim_xml.rb', line 187 def has?(obj, attr=nil) return has?(Element.new(obj, attr)) if attr @contents.any? do |i| if i.is_a?(Element) i.match(obj) || i.has?(obj) else obj.is_a?(Module) && i.is_a?(obj) end end end |
#match(obj, attr = nil) ⇒ Object Also known as: =~
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/eim_xml.rb', line 159 def match(obj, attr=nil) return match(Element.new(obj, attr)) if attr return obj=~@name.to_s if obj.is_a?(Regexp) return @name==obj if obj.is_a?(Symbol) return is_a?(obj) if obj.is_a?(Module) raise ArgumentError unless obj.is_a?(Element) return false unless @name==obj.name obj.attributes.all? do |k, v| (v.nil? && !@attributes.include?(k)) || (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]])) end and obj.contents.all? do |i| case i when Element has_element?(i) when String pcstring_contents.include?(PCString.new(i)) when PCString pcstring_contents.include?(i) when Regexp @contents.any?{|c| c.is_a?(String) and i=~c} end end end |
#name_and_attributes(out = "") ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/eim_xml.rb', line 100 def name_and_attributes(out="") out << "#{@name}" @attributes.each do |k, v| next unless v out << " #{k}='#{PCString===v ? v : PCString.encode(v.to_s)}'" end end |
#pcstring_contents ⇒ Object
155 156 157 |
# File 'lib/eim_xml.rb', line 155 def pcstring_contents @contents.select{|c| c.is_a?(String)||c.is_a?(PCString)}.map{|c| c.is_a?(String) ? PCString.new(c) : c} end |
#write_to(out = "") ⇒ Object Also known as: to_s
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/eim_xml.rb', line 108 def write_to(out = "") out << "<" name_and_attributes(out) if @contents.empty? out << " />" else out << ">" @contents.each do |c| case c when Element c.write_to(out) when PCString out << c.to_s else out << PCString.encode(c.to_s) end end out << "</#{@name}>" end out end |