blob: a13396bb714266af6f91dc05979397a603f199e5 (
plain)
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
|
# XSD4R - WSDL named element collection.
# Copyright (C) 2002, 2003, 2005 NAKAMURA, Hiroshi <[email protected]>.
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
module XSD
class NamedElements
include Enumerable
def initialize
@elements = []
@cache = {}
end
def dup
o = NamedElements.new
o.elements = @elements.dup
o
end
def freeze
super
@elements.freeze
self
end
def empty?
size == 0
end
def size
@elements.size
end
def [](idx)
if idx.is_a?(Numeric)
@elements[idx]
else
@cache[idx] ||= @elements.find { |item| item.name == idx }
end
end
def find_name(name)
@elements.find { |item| item.name.name == name }
end
def keys
collect { |element| element.name }
end
def each
@elements.each do |element|
yield(element)
end
end
def <<(rhs)
@elements << rhs
self
end
def delete(rhs)
@elements.delete(rhs)
end
def +(rhs)
o = NamedElements.new
o.elements = @elements + rhs.elements
o
end
def concat(rhs)
@elements.concat(rhs.elements)
self
end
Empty = NamedElements.new.freeze
protected
def elements=(rhs)
@elements = rhs
end
def elements
@elements
end
end
end
|