File: attr.rb

package info (click to toggle)
ruby-libxml 2.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,024 kB
  • ctags: 2,251
  • sloc: xml: 9,628; ansic: 8,409; ruby: 7,806; makefile: 3
file content (123 lines) | stat: -rw-r--r-- 2,671 bytes parent folder | download | duplicates (7)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# encoding: UTF-8

module LibXML
  module XML
    class Attr 
      include Enumerable

      # call-seq:
      #    attr.child? -> (true|false)
      #
      # Returns whether this attribute has child attributes.
      #
      def child?
        not self.children.nil?
      end

      # call-seq:
      #    attr.doc? -> (true|false)
      #
      # Determine whether this attribute is associated with an
      # XML::Document.
      def doc?
        not self.doc.nil?
      end

      # call-seq:
      #    attr.last? -> (true|false)
      #
      # Determine whether this is the last attribute.
      def last?
        self.last.nil?
      end

      # call-seq:
      #    attr.next? -> (true|false)
      #
      # Determine whether there is a next attribute.
      def next?
        not self.next.nil?
      end

      # call-seq:
      #    attr.ns? -> (true|false)
      #
      # Determine whether this attribute has an associated
      # namespace.
      def ns?
        not self.ns.nil?
      end

      # call-seq:
      #   attr.namespacess -> XML::Namespaces
      #
      # Returns this node's XML::Namespaces object,
      # which is used to access the namespaces
      # associated with this node.
      def namespaces
        @namespaces ||= XML::Namespaces.new(self)
      end
      
      #
      # call-seq:
      #    attr.parent? -> (true|false)
      #
      # Determine whether this attribute has a parent.
      def parent?
        not self.parent.nil?
      end

      # call-seq:
      #    attr.prev? -> (true|false)
      #
      # Determine whether there is a previous attribute.
      def prev?
        not self.prev.nil?
      end

      # Returns this node's type name
      def node_type_name
        if node_type == Node::ATTRIBUTE_NODE
          'attribute'
        else
          raise(UnknownType, "Unknown node type: %n", node.node_type);
        end
      end

      # Iterates nodes and attributes
      def siblings(node, &blk)
        if n = node
          loop do
            blk.call(n)
            break unless n = n.next
          end
        end
      end

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

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