ruby_ex 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/AUTHORS +51 -0
  2. data/ChangeLog +1763 -0
  3. data/NEWS +3 -0
  4. data/README +1 -0
  5. data/Rakefile +8 -0
  6. data/SPEC.dyn.yml +10 -0
  7. data/SPEC.gem.yml +269 -0
  8. data/SPEC.yml +36 -0
  9. data/src/abstract.rb +253 -0
  10. data/src/abstract_node.rb +85 -0
  11. data/src/algorithms.rb +12 -0
  12. data/src/algorithms/simulated_annealing.rb +142 -0
  13. data/src/ask.rb +100 -0
  14. data/src/attributed_class.rb +303 -0
  15. data/src/cache.rb +350 -0
  16. data/src/checkout.rb +12 -0
  17. data/src/choose.rb +271 -0
  18. data/src/commands.rb +20 -0
  19. data/src/commands/command.rb +492 -0
  20. data/src/commands/datas.rb +16 -0
  21. data/src/commands/datas/composite.rb +31 -0
  22. data/src/commands/datas/data.rb +65 -0
  23. data/src/commands/datas/factory.rb +69 -0
  24. data/src/commands/datas/temp.rb +26 -0
  25. data/src/commands/factory.rb +67 -0
  26. data/src/commands/helpers.rb +81 -0
  27. data/src/commands/pipe.rb +66 -0
  28. data/src/commands/runners.rb +16 -0
  29. data/src/commands/runners/exec.rb +50 -0
  30. data/src/commands/runners/fork.rb +130 -0
  31. data/src/commands/runners/runner.rb +140 -0
  32. data/src/commands/runners/system.rb +57 -0
  33. data/src/commands/seq.rb +32 -0
  34. data/src/config_file.rb +95 -0
  35. data/src/const_regexp.rb +57 -0
  36. data/src/daemon.rb +135 -0
  37. data/src/diff.rb +665 -0
  38. data/src/dlogger.rb +62 -0
  39. data/src/drb/drb_observable.rb +95 -0
  40. data/src/drb/drb_observable_pool.rb +27 -0
  41. data/src/drb/drb_service.rb +44 -0
  42. data/src/drb/drb_undumped_attributes.rb +56 -0
  43. data/src/drb/drb_undumped_indexed_object.rb +55 -0
  44. data/src/drb/insecure_protected_methods.rb +101 -0
  45. data/src/drb_ex.rb +12 -0
  46. data/src/dumpable_proc.rb +57 -0
  47. data/src/filetype.rb +229 -0
  48. data/src/generate_id.rb +44 -0
  49. data/src/histogram.rb +222 -0
  50. data/src/hookable.rb +283 -0
  51. data/src/hooker.rb +54 -0
  52. data/src/indexed_node.rb +65 -0
  53. data/src/io_marshal.rb +99 -0
  54. data/src/ioo.rb +193 -0
  55. data/src/labeled_node.rb +62 -0
  56. data/src/logger_observer.rb +24 -0
  57. data/src/md5sum.rb +70 -0
  58. data/src/module/autoload_tree.rb +65 -0
  59. data/src/module/hierarchy.rb +334 -0
  60. data/src/module/instance_method_visibility.rb +71 -0
  61. data/src/node.rb +81 -0
  62. data/src/object_monitor.rb +143 -0
  63. data/src/object_monitor_activity.rb +34 -0
  64. data/src/observable.rb +138 -0
  65. data/src/observable_pool.rb +291 -0
  66. data/src/orderedhash.rb +252 -0
  67. data/src/pp_hierarchy.rb +30 -0
  68. data/src/random_generators.rb +29 -0
  69. data/src/random_generators/random_generator.rb +33 -0
  70. data/src/random_generators/ruby.rb +25 -0
  71. data/src/ruby_ex.rb +124 -0
  72. data/src/safe_eval.rb +346 -0
  73. data/src/sendmail.rb +214 -0
  74. data/src/service_manager.rb +122 -0
  75. data/src/shuffle.rb +30 -0
  76. data/src/spring.rb +134 -0
  77. data/src/spring_set.rb +134 -0
  78. data/src/symtbl.rb +108 -0
  79. data/src/synflow.rb +474 -0
  80. data/src/thread_mutex.rb +11 -0
  81. data/src/timeout_ex.rb +79 -0
  82. data/src/trace.rb +26 -0
  83. data/src/uri/druby.rb +78 -0
  84. data/src/uri/file.rb +63 -0
  85. data/src/uri/ftp_ex.rb +36 -0
  86. data/src/uri/http_ex.rb +41 -0
  87. data/src/uri/pgsql.rb +136 -0
  88. data/src/uri/ssh.rb +87 -0
  89. data/src/uri/svn.rb +113 -0
  90. data/src/uri_ex.rb +71 -0
  91. data/src/verbose_object.rb +70 -0
  92. data/src/yaml/basenode_ext.rb +63 -0
  93. data/src/yaml/chop_header.rb +24 -0
  94. data/src/yaml/transform.rb +450 -0
  95. data/src/yaml/yregexpath.rb +76 -0
  96. data/test/algorithms/simulated_annealing_test.rb +102 -0
  97. data/test/check-pkg-ruby_ex.yml +15 -0
  98. data/test/check-ruby_ex.yml +12 -0
  99. data/test/resources/autoload_tree/A.rb +11 -0
  100. data/test/resources/autoload_tree/B.rb +10 -0
  101. data/test/resources/autoload_tree/foo/C.rb +18 -0
  102. data/test/resources/foo.txt +6 -0
  103. data/test/sanity-suite.yml +12 -0
  104. data/test/sanity/multiple-requires.yml +20 -0
  105. data/test/sanity/single-requires.yml +24 -0
  106. data/test/test-unit-setup.rb +6 -0
  107. data/test/unit-suite.yml +14 -0
  108. metadata +269 -0
@@ -0,0 +1,291 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: observable_pool.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+
9
+ require 'ruby_ex'
10
+ require 'observable'
11
+ require 'thread'
12
+
13
+
14
+ module ObservablePool
15
+
16
+ class Observer
17
+ include Observable
18
+
19
+ attr_reader :observable
20
+
21
+ def initialize(observable, mutex)
22
+ @observable = observable
23
+ @observable.add_observer(self)
24
+ @mutex = mutex
25
+ end
26
+
27
+ def update(*args, &block)
28
+ @mutex.synchronize do
29
+ changed
30
+ notify_observers(@observable, *args, &block)
31
+ end
32
+ end
33
+
34
+ def method_missing(meth, *args, &block)
35
+ @observable.send(meth, *args, &block)
36
+ end
37
+
38
+ end # class Observer
39
+
40
+ def add_observable(observable)
41
+ unless observable.is_a?(Observable)
42
+ raise(ArgumentError, "observable object must be Observale")
43
+ end
44
+ @observable_peer_mutex = Mutex.new unless defined? @observable_peer_mutex
45
+ @observable_peers = {} unless defined? @observable_peers
46
+ sync_observer = Observer.new(observable, @observable_peer_mutex)
47
+ @observable_peers[observable] = sync_observer
48
+ @global_observer_peers = [] unless defined? @global_observer_peers
49
+ @global_observer_peers.each do |observer|
50
+ sync_observer.add_observer(observer)
51
+ end
52
+ sync_observer
53
+ end
54
+
55
+ def add_observables(*observables)
56
+ observables.each { |observable| add_observable(observable) }
57
+ end
58
+
59
+ def delete_observable(observable)
60
+ @observable_peers.delete(observable) if defined? @observable_peers
61
+ end
62
+
63
+ def delete_observables
64
+ @observable_peers.clear if defined? @observable_peers
65
+ end
66
+
67
+ def observable(observable)
68
+ @observable_peers[observable] if defined? @observable_peers
69
+ end
70
+
71
+ def observable?(observable)
72
+ @observable_peers.has_key?(observable) if defined? @observable_peers
73
+ end
74
+
75
+ def list_observables
76
+ if defined? @observable_peers
77
+ @observable_peers.keys
78
+ else
79
+ []
80
+ end
81
+ end
82
+
83
+ def list_observables_observed(observer)
84
+ observables = []
85
+ if defined? @observable_peers
86
+ @observable_peers.each do |observable, obsv|
87
+ if obsv.list_observers.include?(observer)
88
+ observables << observable
89
+ end
90
+ end
91
+ end
92
+ observables
93
+ end
94
+
95
+ def count_observables
96
+ if defined? @observable_peers
97
+ @observable_peers.size
98
+ else
99
+ 0
100
+ end
101
+ end
102
+
103
+ def each_observables(&block)
104
+ if defined? @observable_peers
105
+ @observable_peers.each do |observable, observer|
106
+ block[observable, observer]
107
+ end
108
+ end
109
+ end
110
+
111
+ def list_all_observers
112
+ observers = []
113
+ if defined? @observable_peers
114
+ @observable_peers.each do |observable, observer|
115
+ observers += observer.list_observers
116
+ end
117
+ end
118
+ observers.uniq
119
+ end
120
+
121
+ def count_all_observers
122
+ list_observers.size
123
+ end
124
+
125
+ def add_global_observer(observer)
126
+ if defined? @observable_peers
127
+ @observable_peers.each do |observable, obsv|
128
+ obsv.add_observer(observer) unless observable.observer?(observer)
129
+ end
130
+ end
131
+ @global_observer_peers = [] unless defined? @global_observer_peers
132
+ @global_observer_peers << observer
133
+ end
134
+
135
+ def add_global_observers(*observers)
136
+ observers.each { |observer| add_global_observer(observer) }
137
+ end
138
+
139
+ def delete_global_observer(observer)
140
+ if defined? @observable_peers
141
+ @observable_peers.each do |observable, obsv|
142
+ obsv.delete_observer(observer)
143
+ end
144
+ end
145
+ @global_observer_peers.delete(observer) if defined? @global_observer_peers
146
+ end
147
+
148
+ def delete_global_observers
149
+ if defined? @global_observer_peers
150
+ @global_observer_peers.dup.each do |global_observer|
151
+ delete_global_observer(global_observer)
152
+ end
153
+ end
154
+ end
155
+
156
+ def list_global_observers
157
+ if defined? @global_observer_peers
158
+ @global_observer_peers.dup
159
+ else
160
+ []
161
+ end
162
+ end
163
+
164
+ def count_global_observers
165
+ if defined? @global_observer_peers
166
+ @globale.observer_peers.size
167
+ else
168
+ 0
169
+ end
170
+ end
171
+
172
+ def global_observer?(observer)
173
+ if defined? @global_observer_peers
174
+ @global_observer_peers.include?(observer)
175
+ else
176
+ false
177
+ end
178
+ end
179
+
180
+ end # class ObservablePool
181
+
182
+
183
+ test_section __FILE__ do
184
+
185
+
186
+ class ObservablePoolTest < Test::Unit::TestCase
187
+
188
+ class Notifier
189
+ include Observable
190
+
191
+ end
192
+
193
+ class NotifierPool
194
+ include ObservablePool
195
+
196
+ end
197
+
198
+ class Obsv
199
+
200
+ attr_reader :last_msg
201
+
202
+ def initialize
203
+ @last_msg = nil
204
+ end
205
+
206
+ def update(*args, &block)
207
+ @last_msg = args
208
+ end
209
+
210
+ end
211
+
212
+ def test_simple
213
+ n1 = Notifier.new
214
+ n2 = Notifier.new
215
+ o1 = Obsv.new
216
+ o2 = Obsv.new
217
+ o3 = Obsv.new
218
+ op = NotifierPool.new
219
+ assert_equal(0, op.count_observables)
220
+ assert_equal([], op.list_observables)
221
+ op.add_observables(n1)
222
+ op.add_observable(n2)
223
+ op.observable(n2).add_observers(o1, o3)
224
+ assert_equal(2, op.count_observables)
225
+ [ n1, n2 ].each { |x| assert(op.list_observables.include?(x)) }
226
+ assert_equal(1, n1.count_observers)
227
+ assert_equal([], n1.list_observers[0].list_observers)
228
+ assert_equal(1, n2.count_observers)
229
+ assert_equal(2, n2.list_observers[0].count_observers)
230
+ [o1, o3].each do |x|
231
+ assert(n2.list_observers[0].list_observers.include?(x),
232
+ "#{x} not observer")
233
+ end
234
+ op.observable(n1).add_observers(o2, o3)
235
+ assert_equal(2, op.observable(n1).count_observers)
236
+ [o1, o2, o3].each do |x|
237
+ assert(op.list_all_observers.include?(x), "#{x} not observer")
238
+ end
239
+ n2.changed
240
+ n2.notify_observers('msg2')
241
+ assert_equal([ n2, 'msg2'], o1.last_msg)
242
+ assert_nil(o2.last_msg)
243
+ assert_equal([ n2, 'msg2'], o3.last_msg)
244
+ n1.changed
245
+ n1.notify_observers('msg1')
246
+ assert_equal([n2, 'msg2'], o1.last_msg)
247
+ assert_equal([n1, 'msg1'], o2.last_msg)
248
+ assert_equal([n1, 'msg1'], o3.last_msg)
249
+ [n1, n2].each do |x|
250
+ assert(op.list_observables_observed(o3).include?(x), "#{x} not observer")
251
+ end
252
+ end
253
+
254
+ def test_global_observer
255
+ n1 = Notifier.new
256
+ n2 = Notifier.new
257
+ n3 = Notifier.new
258
+ o1 = Obsv.new
259
+ o2 = Obsv.new
260
+ o3 = Obsv.new
261
+ op = NotifierPool.new
262
+ assert_equal(0, op.count_global_observers)
263
+ assert_equal([], op.list_global_observers)
264
+ op.add_observables(n1)
265
+ op.add_observable(n2)
266
+ op.observable(n1).add_observers(o1)
267
+ op.observable(n2).add_observers(o2)
268
+ op.add_global_observers(o3)
269
+ [o1, o3].each do |x|
270
+ assert(n1.list_observers[0].list_observers.include?(x),
271
+ "#{x} not observer")
272
+ end
273
+ [o2, o3].each do |x|
274
+ assert(n2.list_observers[0].list_observers.include?(x),
275
+ "#{x} not observer")
276
+ end
277
+ assert_equal([o3], op.list_global_observers)
278
+ op.add_observable(n3)
279
+ assert([o3], n3.list_observers[0].list_observers)
280
+ op.delete_global_observers
281
+ assert_equal([], op.list_global_observers)
282
+ assert([], n3.list_observers[0].list_observers)
283
+ assert([o2], n2.list_observers[0].list_observers)
284
+ assert([o1], n1.list_observers[0].list_observers)
285
+ end
286
+
287
+ end # class ObservablePoolTest
288
+
289
+
290
+ end
291
+
@@ -0,0 +1,252 @@
1
+ # AUTHOR
2
+ # jan molic /mig/at/1984/dot/cz/
3
+ # updated by: Nicolas Pouillard <ertai@lrde.epita.fr>
4
+ #
5
+ # DESCRIPTION
6
+ # Override hash to make it creation order preserved.
7
+ # This will be obsolete - it is better to use special OrderedHash class
8
+ # than overriding.
9
+ # Public domain.
10
+ #
11
+ # THANKS
12
+ # Andrew Johnson for his suggestions and fixes of Hash[],
13
+ # merge, to_a, inspect and shift
14
+ #
15
+ # USAGE
16
+ # just require this file, examples are at the end
17
+ # you can try to run this file (ruby orderedhash.rb)
18
+ #
19
+
20
+ # $LastChangedBy: ertai $
21
+ # $Id: orderedhash.rb 266 2005-06-01 14:27:18Z ertai $
22
+
23
+ require 'ruby_ex'
24
+
25
+ module OrderedHash_ext
26
+
27
+ def store ( a, b )
28
+ @order.push a unless has_key? a
29
+ super a,b
30
+ end
31
+
32
+ def []= ( a, b )
33
+ @order.push a unless has_key? a
34
+ super a,b
35
+ end
36
+
37
+ def == ( hsh2 )
38
+ return false if !hsh2.respond_to? :order or @order != hsh2.order
39
+ super hsh2
40
+ end
41
+
42
+ def clear
43
+ @order = []
44
+ super
45
+ end
46
+
47
+ def delete ( key )
48
+ @order.delete key
49
+ super
50
+ end
51
+
52
+ def each_key
53
+ @order.each { |k| yield k }
54
+ self
55
+ end
56
+
57
+ def each_value
58
+ @order.each { |k| yield self[k] }
59
+ self
60
+ end
61
+
62
+ def each
63
+ @order.each { |k| yield(k, self[k]) }
64
+ self
65
+ end
66
+
67
+ alias :each_pair :each
68
+
69
+ def delete_if
70
+ @order.clone.each do |k|
71
+ delete k if yield(k, self[k])
72
+ end
73
+ self
74
+ end
75
+
76
+ def values
77
+ ary = []
78
+ @order.each { |k| ary << self[k] }
79
+ ary
80
+ end
81
+
82
+ def keys
83
+ @order
84
+ end
85
+
86
+ def invert
87
+ hsh2 = self.class.new
88
+ @order.each { |k| hsh2[self[k]] = k }
89
+ hsh2
90
+ end
91
+
92
+ def reject ( &block )
93
+ self.dup.delete_if(&block)
94
+ end
95
+
96
+ def reject! ( &block )
97
+ hsh2 = reject(&block)
98
+ self == hsh2 ? nil : hsh2
99
+ end
100
+
101
+ def replace ( hsh2 )
102
+ @order = hsh2.keys
103
+ super hsh2
104
+ end
105
+
106
+ def shift
107
+ key = @order.first
108
+ key ? [key,delete(key)] : super
109
+ end
110
+
111
+ def to_a
112
+ ary = []
113
+ each { |k,v| ary << [k,v] }
114
+ ary
115
+ end
116
+
117
+ def to_s
118
+ to_a.to_s
119
+ end
120
+
121
+ def update ( hsh2 )
122
+ hsh2.each { |k,v| self[k] = v }
123
+ self
124
+ end
125
+
126
+ def merge! ( hsh2 )
127
+ hsh2.each { |k,v| self[k] = v }
128
+ self
129
+ end
130
+
131
+ def merge ( hsh2 )
132
+ self.dup.update(hsh2)
133
+ end
134
+
135
+ def select
136
+ ary = []
137
+ each { |k,v| ary << [k,v] if yield k,v }
138
+ ary
139
+ end
140
+
141
+ def ordered?
142
+ !@order.nil?
143
+ end
144
+
145
+ end # module OrderedHash_ext
146
+
147
+
148
+ class OHash < Hash
149
+
150
+ attr_accessor :order
151
+
152
+ alias :unordered_keys :keys
153
+
154
+ include OrderedHash_ext
155
+
156
+ def initialize ( *a )
157
+ @order = unordered_keys
158
+ super
159
+ end
160
+
161
+ def self.[] ( *args )
162
+ hsh = OHash.new
163
+ if Hash === args[0]
164
+ hsh.replace args[0]
165
+ elsif (args.size % 2) != 0
166
+ raise ArgumentError, "odd number of elements for Hash"
167
+ else
168
+ hsh[args.shift] = args.shift while args.size > 0
169
+ end
170
+ hsh
171
+ end
172
+
173
+ end # class OHash
174
+
175
+
176
+ class Hash
177
+
178
+ def ordered?
179
+ false
180
+ end
181
+
182
+ end # class Hash
183
+
184
+
185
+ test_section __FILE__ do
186
+ class OrderedHashTest < Test::Unit::TestCase
187
+
188
+ def check_ordered ( h, ref )
189
+ h.store(4,3)
190
+ h.store(2,3)
191
+ assert_equal(h.to_a, ref)
192
+ end
193
+
194
+ def test_ordered
195
+ check_ordered(OHash.new, [[4, 3], [2, 3]])
196
+ end
197
+
198
+ def test_unorderd
199
+ check_ordered(Hash.new, [[2, 3], [4, 3]])
200
+ end
201
+
202
+ def test_simply
203
+ # You can do simply
204
+ hsh = OHash.new
205
+ hsh['z'] = 1
206
+ hsh['a'] = 2
207
+ hsh['c'] = 3
208
+ assert_equal(hsh.keys, ['z','a','c'])
209
+ end
210
+
211
+ def test_preserve
212
+ # or using OrderedHash[] method
213
+ hsh = OHash['z', 1, 'a', 2, 'c', 3]
214
+ hsh2 = OHash['a', 2, 'z', 1, 'c', 3]
215
+ assert_equal(hsh.keys, ['z','a','c'])
216
+ assert_not_equal(hsh, hsh2)
217
+ end
218
+
219
+ def test_dont_preserve
220
+ # but this don't preserve order
221
+ hsh = OHash['z'=>1, 'a'=>2, 'c'=>3]
222
+ hsh2 = OHash['a'=>2, 'z'=>1, 'c'=>3]
223
+ assert_equal(hsh, hsh2)
224
+ end
225
+
226
+ def test_unorderd_empty
227
+ check_ordered({}, [[2, 3], [4, 3]])
228
+ end
229
+
230
+ def test_little_ordered
231
+ a = (0..11).to_a
232
+ h = OHash[*a]
233
+ assert_equal(h.to_s, a.to_s)
234
+ end
235
+
236
+ def test_big_ordered
237
+ a = (0..501).to_a
238
+ h = OHash[*a]
239
+ assert_equal(h.to_s, a.to_s)
240
+ end
241
+
242
+ def test_eql
243
+ assert_nothing_raised { OHash.new == {} }
244
+ assert_not_equal OHash.new, {}
245
+ assert_equal OHash.new, OHash.new
246
+ assert_equal OHash[1,2,3,4], OHash[1,2,3,4]
247
+ assert_not_equal OHash[1,2,3,4], OHash[3,4,1,2]
248
+ end
249
+
250
+ end # OrderedHashTest
251
+
252
+ end