rexle 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rexle.rb +40 -18
  2. metadata +2 -2
data/lib/rexle.rb CHANGED
@@ -62,7 +62,10 @@ class Rexle
62
62
 
63
63
  return scan_match(self, element_name, attr_search, condition, rlist) if xpath_value[0,2] == '//'
64
64
 
65
- return_elements = @child_lookup.map.with_index.select {|x| x[0][0] == element_name or element_name == '*'}
65
+ return_elements = @child_lookup.map.with_index.select {|x|
66
+
67
+ x[0][0] == element_name or element_name == '*'
68
+ }
66
69
 
67
70
  if return_elements.length > 0 then
68
71
 
@@ -71,15 +74,17 @@ class Rexle
71
74
  else
72
75
 
73
76
  return_elements.map.with_index do |x,i|
74
- rtn_element = filter(x, i+1, attr_search){|e| r = e.xpath(a.join('/'), rlist); r || e }
77
+ rtn_element = filter(x, i+1, attr_search){|e| r = e.xpath(a.join('/'), rlist); r || e }
78
+ next unless rtn_element
79
+
75
80
  if rtn_element.is_a? Array then
76
81
  rlist = rtn_element.flatten
77
82
  elsif (rtn_element.is_a? String) || not(rtn_element[0].is_a? String)
78
83
  rlist << rtn_element
79
84
  end
80
85
  end
86
+
81
87
  end
82
-
83
88
  else
84
89
 
85
90
  # strip off the 1st element from the XPath
@@ -108,6 +113,16 @@ class Rexle
108
113
 
109
114
  private
110
115
 
116
+ def scan_print(nodes)
117
+ out = []
118
+ nodes.each do |x|
119
+ out << "<%s>" % x.name
120
+ out << scan_print(x.children)
121
+ out << "</%s>" % x.name
122
+ end
123
+ out
124
+ end
125
+
111
126
  def format_attributes(condition)
112
127
  raw_items = condition[1..-1].scan(/[\w]+=\'[^\']+\'|and|\d+/)
113
128
 
@@ -147,25 +162,25 @@ class Rexle
147
162
 
148
163
  x = raw_element
149
164
  e = @child_elements[x.last]
165
+ h = x[0][1] # <-- fetch the attributes
150
166
 
151
- if attr_search then
152
- h = x[0][1] # <-- fetch the attributes
153
- if block_given? then
154
- blk.call(e)
155
- else
156
- if attr_search.is_a? Fixnum then
157
- e if i == attr_search
158
- else
159
- e if h and eval(attr_search)
160
- end
161
- end
162
- else
167
+ if not attr_search or \
168
+ (attr_search and ((attr_search.is_a?(Fixnum) and \
169
+ i == attr_search) or (h and eval(attr_search)))) then
163
170
  block_given? ? blk.call(e) : e
164
171
  end
172
+
165
173
  end
166
174
 
167
175
  end # -- end of element --
168
176
 
177
+ def root() @doc end
178
+
179
+ def xml()
180
+ body = scan_print(self.root.children).join
181
+ "<%s>%s</%s>" % [self.root.name, body, self.root.name]
182
+ end
183
+
169
184
  private
170
185
 
171
186
  def scan_element(a)
@@ -211,9 +226,6 @@ class Rexle
211
226
  r = scan_element(a)
212
227
  element.add_element(r) if r
213
228
 
214
- # capture siblings
215
- a.slice!(0, name.length + 3) if a[0, name.length + 3].join == "</%s>" % name
216
-
217
229
  (r = scan_element(a); element.add_element r if r) until (a[0, name.length + 3].join == "</%s>" % [name]) or a.length < 2
218
230
  else
219
231
  #check for its end tag
@@ -228,6 +240,16 @@ class Rexle
228
240
  end
229
241
  end
230
242
 
243
+ def scan_print(nodes)
244
+ out = []
245
+ nodes.each do |x|
246
+ out << "<%s>" % x.name
247
+ out << scan_print(x.children)
248
+ out << "</%s>" % x.name
249
+ end
250
+ out
251
+ end
252
+
231
253
  def count(path) @doc.xpath(path).flatten.compact.length end
232
254
 
233
255
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rexle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-11-05 00:00:00 +00:00
12
+ date: 2010-11-07 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15