Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/branches/1_5/lib/xsd/namedelements.rb

Revision 1917, 1.9 kB (checked in by nahi, 1 year ago)
  • added support for attributeGroup.
  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1 # XSD4R - WSDL named element collection.
2 # Copyright (C) 2000-2007  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
4 # This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
5 # redistribute it and/or modify it under the same terms of Ruby's license;
6 # either the dual license version in 2003, or any later version.
7
8
9 module XSD
10
11
12 class NamedElements
13   include Enumerable
14
15   def initialize
16     @elements = []
17     @cache = {}
18   end
19
20   def dup
21     o = NamedElements.new
22     o.elements = @elements.dup
23     o
24   end
25
26   def freeze
27     super
28     @elements.freeze
29     self
30   end
31
32   def empty?
33     size == 0
34   end
35
36   def size
37     @elements.size
38   end
39
40   def [](idx)
41     if idx.is_a?(Numeric)
42       @elements[idx]
43     else
44       @cache[idx] ||= @elements.find { |item| item.name == idx }
45     end
46   end
47
48   def find_name(name)
49     @elements.find { |item| item.name.name == name }
50   end
51
52   def keys
53     collect { |element| element.name }
54   end
55
56   def each
57     @elements.each do |element|
58       yield(element)
59     end
60   end
61
62   def <<(rhs)
63     @elements << rhs
64     self
65   end
66  
67   def delete(rhs)
68     rv = @elements.delete(rhs)
69     @cache.clear
70     rv
71   end
72
73   def +(rhs)
74     o = NamedElements.new
75     o.elements = @elements + rhs.elements
76     @cache.clear
77     o
78   end
79
80   def concat(rhs)
81     @elements.concat(rhs.elements)
82     @cache.clear
83     self
84   end
85
86   def uniq
87     o = NamedElements.new
88     o.elements = uniq_elements
89     o
90   end
91
92   def uniq!
93     @elements.replace(uniq_elements)
94     @cache.clear
95   end
96
97   def find_all
98     o = NamedElements.new
99     each do |ele|
100       o << ele if yield(ele)
101     end
102     o
103   end
104
105   Empty = NamedElements.new.freeze
106
107 protected
108
109   def elements=(rhs)
110     @elements = rhs
111   end
112
113   def elements
114     @elements
115   end
116
117 private
118
119   def uniq_elements
120     dict = {}
121     elements = []
122     @elements.each do |ele|
123       unless dict.key?(ele.name)
124         dict[ele.name] = ele
125         elements << ele
126       end
127     end
128     elements
129   end
130 end
131
132 end
Note: See TracBrowser for help on using the browser.