Class: EimXML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/eim_xml.rb

Direct Known Subclasses

XHTML::Base

Constant Summary collapse

NEST =
' '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) {|_self| ... } ⇒ Element

Returns a new instance of Element.

Yields:

  • (_self)

Yield Parameters:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/eim_xml.rb', line 63

def initialize(name, attributes = {})
  @name = name.to_sym
  @attributes = {}
  @contents = []

  attributes.each do |k, v|
    @attributes[k.to_sym] = v
  end

  yield(self) if block_given?
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



59
60
61
# File 'lib/eim_xml.rb', line 59

def attributes
  @attributes
end

#contentsObject (readonly)

Returns the value of attribute contents.



59
60
61
# File 'lib/eim_xml.rb', line 59

def contents
  @contents
end

#nameObject

Returns the value of attribute name.



59
60
61
# File 'lib/eim_xml.rb', line 59

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



127
128
129
130
131
# File 'lib/eim_xml.rb', line 127

def ==(other)
  return false unless other.is_a?(Element)

  @name == other.name && @attributes == other.attributes && @contents == other.contents
end

#[](key) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/eim_xml.rb', line 138

def [](key)
  if key.is_a?(Integer)
    @contents[key]
  else
    @attributes[key.to_sym]
  end
end

#add(content) ⇒ Object Also known as: <<



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/eim_xml.rb', line 80

def add(content)
  case content
  when nil
    # nothing to do
  when Array
    content.each { |i| add(i) }
  else
    @contents << content
  end
  self
end

#add_attribute(key, value) ⇒ Object Also known as: []=



133
134
135
# File 'lib/eim_xml.rb', line 133

def add_attribute(key, value)
  @attributes[key.to_sym] = value
end

#del_attribute(key) ⇒ Object



146
147
148
# File 'lib/eim_xml.rb', line 146

def del_attribute(key)
  @attributes.delete(key.to_sym)
end

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



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/eim_xml.rb', line 196

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|
    if i.is_a?(Element)
      i.find(obj, dst)
    elsif 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?

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
# File 'lib/eim_xml.rb', line 182

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: =~

Raises:

  • (ArgumentError)


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

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



93
94
95
96
97
98
99
100
# File 'lib/eim_xml.rb', line 93

def name_and_attributes(out = '')
  out << @name.to_s
  @attributes.each do |k, v|
    next unless v

    out << " #{k}='#{v.is_a?(PCString) ? v : PCString.encode(v.to_s)}'"
  end
end

#pcstring_contentsObject



150
151
152
# File 'lib/eim_xml.rb', line 150

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