rexle 0.9.47 → 0.9.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/rexle.rb +36 -8
  2. metadata +3 -3
data/lib/rexle.rb CHANGED
@@ -10,6 +10,10 @@ require 'cgi'
10
10
  include REXML
11
11
 
12
12
  # modifications:
13
+ # 20-Oct-2012: feature: added Rexle::Element#texts which is the equivalent
14
+ # of REXML::Element#texts
15
+ # feature: Rexle::Element#add_text is now the equivalent of
16
+ # REXML::Element#add_text
13
17
  # 10-Sep-2012: bug fix: Removed code from method pretty_print in order to
14
18
  # get the XML displayed properly
15
19
  # 23-Aug-2012: feature: implemented xpath function contains()
@@ -37,8 +41,8 @@ include REXML
37
41
  module XMLhelper
38
42
 
39
43
  def doc_print(children)
40
-
41
- body = (children.empty? or children.is_an_empty_string? ) ? '' : scan_print(children).join
44
+
45
+ body = (children.nil? or children.empty? or children.is_an_empty_string? ) ? '' : scan_print(children).join
42
46
  a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
43
47
  "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name]
44
48
  end
@@ -118,6 +122,10 @@ class Rexle
118
122
 
119
123
  attr_reader :prefixes
120
124
 
125
+ def self.version()
126
+ '0.9.xx'
127
+ end
128
+
121
129
  def initialize(x=nil)
122
130
  super()
123
131
 
@@ -213,6 +221,7 @@ class Rexle
213
221
  procs = {
214
222
  Array: proc {|x| block_given? ? x : x.flatten.uniq },
215
223
  String: proc {|x| x},
224
+ Hash: proc {|x| x},
216
225
  TrueClass: proc{|x| x},
217
226
  FalseClass: proc{|x| x},
218
227
  :"Rexle::Element" => proc {|x| [x]}
@@ -235,7 +244,6 @@ class Rexle
235
244
 
236
245
  def query_xpath(raw_xpath_value, rlist=[], &blk)
237
246
 
238
- #puts 'raw_xpath_value: ' + raw_xpath_value.inspect
239
247
  #remove any pre'fixes
240
248
  #@rexle.prefixes.each {|x| xpath_value.sub!(x + ':','') }
241
249
  flag_func = false
@@ -277,6 +285,7 @@ class Rexle
277
285
 
278
286
  attribute = xpath_value[/^(attribute::|@)(.*)/,2]
279
287
 
288
+ return @attributes if attribute == '*'
280
289
  return [@attributes[attribute.to_sym]] if attribute and @attributes and @attributes.has_key?(attribute.to_sym)
281
290
  s = a_path.shift
282
291
  end
@@ -341,7 +350,9 @@ class Rexle
341
350
  rtn_element = filter(x, i+1, attr_search){|e| r = e.xpath(a_path.join('/') + raw_condition.to_s + remaining_path, &blk); (r || e) }
342
351
  next if rtn_element.nil? or (rtn_element.is_a? Array and rtn_element.empty?)
343
352
 
344
- if rtn_element.is_a? Array then
353
+ if rtn_element.is_a? Hash then
354
+ rtn_element
355
+ elsif rtn_element.is_a? Array then
345
356
  rtn_element
346
357
  elsif (rtn_element.is_a? String) || (rtn_element.is_a?(Array) and not(rtn_element[0].is_a? String))
347
358
  rtn_element
@@ -411,7 +422,16 @@ class Rexle
411
422
  @attributes.merge! h
412
423
  end
413
424
 
414
- def add_text(s) @value = s; self end
425
+ def add_text(s)
426
+ if @child_elements.length < 1 then
427
+ @value = s;
428
+ else
429
+ if @child_elements.last.is_a? Rexle::Element then
430
+ self.add s
431
+ end
432
+ end
433
+ self
434
+ end
415
435
 
416
436
  def attribute(key)
417
437
  key = key.to_sym if key.is_a? String
@@ -421,7 +441,7 @@ class Rexle
421
441
  def attributes() @attributes end
422
442
 
423
443
  def children()
424
-
444
+ return unless @value
425
445
  r = (@value.empty? ? [] : [@value]) + @child_elements
426
446
  def r.is_an_empty_string?()
427
447
  self.length == 1 and self.first == ''
@@ -459,7 +479,9 @@ class Rexle
459
479
  end
460
480
 
461
481
  def doc_root() @rexle.root end
462
- def each(&blk) @child_elements.each(&blk) end
482
+ def each(&blk)
483
+ @child_elements.each(&blk) #unless @child_elements.empty?
484
+ end
463
485
  def has_elements?() !self.elements.empty? end
464
486
 
465
487
  def insert_after(node) insert(node, 1) end
@@ -485,6 +507,10 @@ class Rexle
485
507
 
486
508
  result
487
509
  end
510
+
511
+ def texts()
512
+ [@value] + @child_elements.select {|x| x.is_a? String}
513
+ end
488
514
 
489
515
  def value=(raw_s)
490
516
 
@@ -715,9 +741,11 @@ class Rexle
715
741
  def xml(options={})
716
742
  o = {pretty: false, declaration: true}.merge(options)
717
743
  msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
744
+
718
745
  r = ''
719
746
  r = "<?xml version='1.0' encoding='UTF-8'?>\n" if o[:declaration] == true
720
747
  r << method(msg).call(self.root.children)
748
+
721
749
  r
722
750
  end
723
751
 
@@ -774,4 +802,4 @@ class Rexle
774
802
  [node.name, node.text.to_s, attributes, *children]
775
803
  end
776
804
 
777
- end
805
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rexle
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.47
5
+ version: 0.9.61
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-10-07 00:00:00 Z
13
+ date: 2012-10-21 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rexleparser
@@ -92,6 +92,6 @@ rubyforge_project:
92
92
  rubygems_version: 1.8.23
93
93
  signing_key:
94
94
  specification_version: 3
95
- summary: rexle
95
+ summary: Rexle is a simple XML parser written purely in Ruby
96
96
  test_files: []
97
97