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.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/eim_xml.rb', line 65 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.
61 62 63 |
# File 'lib/eim_xml.rb', line 61 def attributes @attributes end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
61 62 63 |
# File 'lib/eim_xml.rb', line 61 def contents @contents end |
#name ⇒ Object
Returns the value of attribute name.
61 62 63 |
# File 'lib/eim_xml.rb', line 61 def name @name end |
Instance Method Details
#==(xml) ⇒ Object
127 128 129 130 |
# File 'lib/eim_xml.rb', line 127 def ==(xml) return false unless xml.is_a?(Element) @name==xml.name && @attributes==xml.attributes && @contents==xml.contents end |
#[](key) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/eim_xml.rb', line 137 def [](key) if key.is_a?(Fixnum) @contents[key] else @attributes[key.to_sym] end end |
#add(v) ⇒ Object Also known as: <<
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/eim_xml.rb', line 82 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: []=
132 133 134 |
# File 'lib/eim_xml.rb', line 132 def add_attribute(key, value) @attributes[key.to_sym] = value end |
#del_attribute(key) ⇒ Object
145 146 147 |
# File 'lib/eim_xml.rb', line 145 def del_attribute(key) @attributes.delete(key.to_sym) end |
#find(obj, dst = Element.new(:found)) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/eim_xml.rb', line 195 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?
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/eim_xml.rb', line 181 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: =~
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/eim_xml.rb', line 153 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
94 95 96 97 98 99 100 |
# File 'lib/eim_xml.rb', line 94 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
149 150 151 |
# File 'lib/eim_xml.rb', line 149 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/eim_xml.rb', line 102 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 |