# class EimXML::Element

### Constants

NEST

### Attributes

attributes[R]
contents[R]
name[R]

### Public Class Methods

new(name, attributes={}) { |self| ... } click to toggle source

       # File lib/eim_xml.rb
    65 def initialize(name, attributes={})
    66         @name = name.to_sym
    67         @attributes = Hash.new
    68         @contents = Array.new
    69
    70         attributes.each do |k, v|
    71                 @attributes[k.to_sym] = v
    72         end
    73
    74         yield(self) if block_given?
    75 end

### Public Instance Methods

<<(v)
Alias for: add
==(xml) click to toggle source

        # File lib/eim_xml.rb
    127 def ==(xml)
    128         return false unless xml.is_a?(Element)
    129         @name==xml.name && @attributes==xml.attributes && @contents==xml.contents
    130 end

=~(obj, attr=nil)
Alias for: match
[](key) click to toggle source

        # File lib/eim_xml.rb
    137 def [](key)
    138         if key.is_a?(Fixnum)
    139                 @contents[key]
    140         else
    141                 @attributes[key.to_sym]
    142         end
    143 end

[]=(key, value)
Alias for: add_attribute
add(v) click to toggle source

       # File lib/eim_xml.rb
    82 def add(v)
    83         case v
    84         when nil
    85         when Array
    86                 v.each{|i| self.add(i)}
    87         else
    88                 @contents << v
    89         end
    90         self
    91 end

Also aliased as: <<
add_attribute(key, value) click to toggle source

        # File lib/eim_xml.rb
    132 def add_attribute(key, value)
    133         @attributes[key.to_sym] = value
    134 end

Also aliased as: []=
del_attribute(key) click to toggle source

        # File lib/eim_xml.rb
    145 def del_attribute(key)
    146         @attributes.delete(key.to_sym)
    147 end

find(obj, dst=Element.new(:found)) click to toggle source

        # File lib/eim_xml.rb
    195 def find(obj, dst=Element.new(:found))
    196         return find(Element.new(obj, dst)) if dst.is_a?(Hash)
    197
    198         dst << self if match(obj)
    199         @contents.each do |i|
    200                 case
    201                 when i.is_a?(Element)
    202                         i.find(obj, dst)
    203                 when obj.is_a?(Module) && i.is_a?(obj)
    204                         dst << i
    205                 end
    206         end
    207         dst
    208 end

has?(obj, attr=nil) click to toggle source

        # File lib/eim_xml.rb
    181 def has?(obj, attr=nil)
    182         return has?(Element.new(obj, attr)) if attr
    183
    184         @contents.any? do |i|
    185                 if i.is_a?(Element)
    186                         i.match(obj) || i.has?(obj)
    187                 else
    188                         obj.is_a?(Module) && i.is_a?(obj)
    189                 end
    190         end
    191 end

Also aliased as: has_element?, include?
has_element?(obj, attr=nil)
Alias for: has?
include?(obj, attr=nil)
Alias for: has?
inspect(out = "")
Alias for: to_s
match(obj, attr=nil) click to toggle source

        # File lib/eim_xml.rb
    153 def match(obj, attr=nil)
    154         return match(Element.new(obj, attr)) if attr
    155         return obj=~@name.to_s if obj.is_a?(Regexp)
    156         return @name==obj if obj.is_a?(Symbol)
    157         return is_a?(obj) if obj.is_a?(Module)
    158
    159         raise ArgumentError unless obj.is_a?(Element)
    160
    161         return false unless @name==obj.name
    162
    163         obj.attributes.all? do |k, v|
    164                 (v.nil? && !@attributes.include?(k)) ||
    165                         (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]]))
    166         end and obj.contents.all? do |i|
    167                 case i
    168                 when Element
    169                         has_element?(i)
    170                 when String
    171                         pcstring_contents.include?(PCString.new(i))
    172                 when PCString
    173                         pcstring_contents.include?(i)
    174                 when Regexp
    175                         @contents.any?{|c| c.is_a?(String) and i=~c}
    176                 end
    177         end
    178 end

Also aliased as: =~
name_and_attributes(out="") click to toggle source

        # File lib/eim_xml.rb
     94 def name_and_attributes(out="")
     95         out << "#{@name}"
     96         @attributes.each do |k, v|
     97                 next unless v
     98                 out << " #{k}='#{PCString===v ? v : PCString.encode(v.to_s)}'"
     99         end
    100 end

pcstring_contents() click to toggle source

        # File lib/eim_xml.rb
    149 def pcstring_contents
    150         @contents.select{|c| c.is_a?(String)||c.is_a?(PCString)}.map{|c| c.is_a?(String) ? PCString.new(c) : c}
    151 end

to_s(out = "")
Also aliased as: inspect
Alias for: write_to
write_to(out = "") click to toggle source

        # File lib/eim_xml.rb
    102 def write_to(out = "")
    103         out << "<"
    104         name_and_attributes(out)
    105
    106         if @contents.empty?
    107                 out << " />"
    108         else
    109                 out << ">"
    110                 @contents.each do |c|
    111                         case c
    112                         when Element
    113                                 c.write_to(out)
    114                         when PCString
    115                                 out << c.to_s
    116                         else
    117                                 out << PCString.encode(c.to_s)
    118                         end
    119                 end
    120                 out << "</#{@name}>"
    121         end
    122         out
    123 end

Also aliased as: to_s

### Protected Instance Methods

name=(new_name) click to toggle source

       # File lib/eim_xml.rb
    77 def name=(new_name)
    78         @name = new_name.to_sym
    79 end