Class | EimXML::Element
In: | lib/eim_xml.rb
Parent: | Object

### Methods

<< == =~ [] []= add add_attribute del_attribute find has? has_element? include? inspect match name= name_and_attributes new pcstring_contents to_s write_to

### Constants

NEST | = | " "

### Attributes

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

### Public Class methods

new(name, attributes={}) {|self| ...}

[Source]

        # File lib/eim_xml.rb, line 65
    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)

[Source]

         # File lib/eim_xml.rb, line 127
    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)

[Source]

         # File lib/eim_xml.rb, line 137
    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)

[Source]

        # File lib/eim_xml.rb, line 82
    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

add_attribute(key, value)

[Source]

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

del_attribute(key)

[Source]

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

find(obj, dst=Element.new(:found))

[Source]

         # File lib/eim_xml.rb, line 195
    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)

[Source]

         # File lib/eim_xml.rb, line 181
    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

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)

[Source]

         # File lib/eim_xml.rb, line 153
    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

name_and_attributes(out="")

[Source]

         # File lib/eim_xml.rb, line 94
     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()

[Source]

         # File lib/eim_xml.rb, line 149
    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 = "")

Alias for write_to

write_to(out = "")

[Source]

         # File lib/eim_xml.rb, line 102
    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

### Protected Instance methods

name=(new_name)

[Source]

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

[Validate]