File: libxml.rb

package info (click to toggle)
ruby-mkrf 0.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,688 kB
  • sloc: ansic: 12,494; ruby: 6,998; sh: 790; yacc: 374; makefile: 57; cpp: 10
file content (107 lines) | stat: -rw-r--r-- 1,876 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# $Id: libxml.rb,v 1.1 2006/04/17 13:30:22 roscopeco Exp $ 
# Please see the LICENSE file for copyright and distribution information 
require 'xml/libxml_so'

class XML::Node::Set
  def empty? #:nodoc:
  	self.length <= 0
  end
  
  def first #:nodoc:
  	self.each { |n| return n }
  end
end

class XML::Document
  include Enumerable

  # maybe, maybe not...
  def each(&blk) #:nodoc:
    find('//*').each(&blk)
  end
end

class XML::Node::Set 
  # inefficient, but maybe can find a way to cache the
  # ary and dump on change?
  def [](i, count = nil) #:nodoc:
    if count
      to_a[i,count]
    else
      to_a[i]
    end
  end

  def to_s #:nodoc:
    to_a.to_s
  end
end

module XML::SiblingEnum #:nodoc:all
  private 
  
  # Iterates nodes and attributes
  def siblings(node, &blk)
    if n = node
      loop do
        blk.call(n)
        break unless n = n.next
      end
    end 
  end
end

class XML::Node
  include XML::SiblingEnum
  include Enumerable
  include Comparable
  
  # maybe these don't belong on all nodes...
  def each_child(&blk) #:nodoc:
    siblings(child, &blk)   
  end

  def each_attr(&blk) #:nodoc:
    siblings(properties, &blk)
  end

  # all siblings INCLUDING self
  def each_sibling(&blk) #:nodoc:
    siblings(self, &blk)
  end
  
  # I guess this is what you'd expect?
  alias :each :each_child

  def to_a #:nodoc:
    inject([]) { |ary,n| ary << n }
  end
  
  def <=>(other) #:nodoc:
    to_s <=> other.to_s
  end  
end

class XML::Attr 
  include XML::SiblingEnum
  include Enumerable

  def each_sibling(&blk) #:nodoc:
    siblings(self,&blk)
  end
  
  alias :each_attr :each_sibling
  alias :each :each_sibling
  
  def to_h #:nodoc:
    inject({}) do |h,a| h[a.name] = a.value end
  end

  def to_a #:nodoc:
    inject([]) do |ary,a| ary << [a.name, a.value] end
  end
  
  def to_s #:nodoc:
    "#{name} = #{value}"
  end
end