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
data/src/hookable.rb ADDED
@@ -0,0 +1,283 @@
1
+ # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: hookable.rb 266 2005-06-01 14:27:18Z ertai $
5
+
6
+ require 'ruby_ex'
7
+ require 'hooker'
8
+
9
+
10
+ module Hookable
11
+
12
+ def self.included ( aClass )
13
+ super
14
+
15
+ aClass.module_eval do
16
+
17
+ def self.inheritable_attributes
18
+ if @inheritable_attributes.nil? or @inheritable_attributes.empty?
19
+ @inheritable_attributes = HashWithRecursiveCloneDup.new
20
+ end
21
+ @inheritable_attributes
22
+ end
23
+
24
+
25
+ class_inheritable_array :hooks
26
+ class_inheritable_array :hookers
27
+ inheritable_attributes[:hookers] = []
28
+ inheritable_attributes[:hooks] = []
29
+
30
+
31
+ def hook_trigger ( name, *args )
32
+ self.class.hook_trigger(name, *args)
33
+ return unless defined? @hookers
34
+ @hookers.each do |hooker|
35
+ @@hook_trigger_internal[hooker, name, *args]
36
+ end
37
+ end
38
+
39
+
40
+ def self.hook_trigger ( name, *args )
41
+ raise ArgumentError, "Unknown hook #{name}" unless hooks.include? name
42
+ hookers.each do |hooker|
43
+ @@hook_trigger_internal[hooker, name, *args]
44
+ end
45
+ end
46
+
47
+
48
+ def self.hooker_subscribe ( hooker )
49
+ hookers << hooker
50
+ end
51
+
52
+
53
+ def hooker_subscribe ( hooker )
54
+ @hookers ||= []
55
+ @hookers << hooker
56
+ end
57
+
58
+
59
+ def self.subscribe_hook ( *hook_names, &block )
60
+ hooker_subscribe ProcHooker.new(*hook_names, &block)
61
+ end
62
+
63
+
64
+ def subscribe_hook ( *hook_names, &block )
65
+ hooker_subscribe ProcHooker.new(*hook_names, &block)
66
+ end
67
+
68
+
69
+ def self.hook_declare ( name, *groups )
70
+ hooks << name
71
+ end
72
+
73
+
74
+ @@hook_trigger_internal = lambda do |hooker, name, *args|
75
+ if hooker.respond_to?(name)
76
+ hooker.send(name, *args)
77
+ else
78
+ if hooker.respond_to? :default_hook and not hooker.default_hook.nil?
79
+ hooker.send(hooker.default_hook, name, *args)
80
+ elsif hooker.respond_to?(:method_missing)
81
+ hooker.send(name, *args)
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
90
+
91
+ class ProcHooker < Proc
92
+ include Hooker
93
+ def initialize ( *hook_names, &block )
94
+ hook_method :call, *hook_names
95
+ super(&block)
96
+ end
97
+ end
98
+
99
+
100
+ class HashWithRecursiveCloneDup < Hash
101
+
102
+ def initialize_copy ( aHash )
103
+ aHash.each { |k, v| self[k] = v.dup }
104
+ end
105
+
106
+ end # class HashWithRecursiveCloneDup
107
+
108
+ end # module Hookable
109
+
110
+
111
+
112
+ test_section __FILE__ do
113
+
114
+ class HookableHookerTest < Test::Unit::TestCase
115
+
116
+ class HookerTest
117
+ include Hooker
118
+
119
+ attr_reader :stack
120
+
121
+ def log ( *args )
122
+ @stack ||= []
123
+ @stack << args
124
+ end
125
+
126
+ def method_missing ( *a )
127
+ log(:method_missing, *a)
128
+ end
129
+
130
+ def hook1 ( *a )
131
+ log(:hook1, *a)
132
+ end
133
+
134
+ def reset
135
+ @stack.clear if @stack
136
+ end
137
+ end
138
+
139
+
140
+ STATIC_HOOKER_TEST = HookerTest.new
141
+
142
+
143
+ class HookableTest
144
+ include Hookable
145
+
146
+ hook_declare :hook1
147
+
148
+ hook_declare :hook2, :group1
149
+
150
+ def run
151
+ run1
152
+ run2
153
+ end
154
+
155
+ def run1
156
+ hook_trigger :hook1
157
+ end
158
+
159
+ def run2
160
+ hook_trigger :hook2, 42
161
+ hook_trigger :hook3 rescue nil
162
+ end
163
+
164
+ def << ( hooker )
165
+ hooker_subscribe hooker
166
+ end
167
+
168
+ hooker_subscribe STATIC_HOOKER_TEST
169
+
170
+ def self.ref
171
+ [
172
+ [[:hook1], [:method_missing, :hook2, 42]],
173
+ [[:hook1], [:default, :hook2, 42]],
174
+ [[:hook_1_and_2], [:hook_1_and_2, 42]],
175
+ [[:hook1], [:method_missing, :default_by_method_missing, :hook2, 42]],
176
+ [[:hook1], [:hook2, 42]],
177
+ [[:hook1], [:method_missing, :hook2_by_dynamic_hook_method, 42]]
178
+ ]
179
+ end
180
+
181
+ end
182
+
183
+
184
+
185
+ class SubHookableTest < HookableTest
186
+ hook_declare :hook3
187
+
188
+ def self.ref
189
+ [
190
+ [[:hook1], [:method_missing, :hook2, 42], [:method_missing, :hook3]],
191
+ [[:hook1], [:default, :hook2, 42], [:default, :hook3]],
192
+ [[:hook_1_and_2], [:hook_1_and_2, 42], [:method_missing, :hook3]],
193
+ [[:hook1], [:method_missing, :default_by_method_missing, :hook2, 42],
194
+ [:method_missing, :default_by_method_missing, :hook3]],
195
+ [[:hook1], [:hook2, 42], [:default, :hook3]],
196
+ [[:hook1], [:method_missing, :hook2_by_dynamic_hook_method, 42],
197
+ [:method_missing, :hook3]]
198
+ ]
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+ class HookerTest1 < HookerTest
206
+ def default ( *a )
207
+ log(:default, *a)
208
+ end
209
+ hook_default_method :default
210
+ end
211
+
212
+
213
+
214
+ class HookerTest2 < HookerTest
215
+ def hook_1_and_2 ( *a )
216
+ log(:hook_1_and_2, *a)
217
+ end
218
+ hook_method :hook_1_and_2, :hook1, :hook2
219
+ end
220
+
221
+
222
+
223
+ class HookerTest3 < HookerTest
224
+ hook_default_method :default_by_method_missing
225
+ end
226
+
227
+
228
+
229
+ class HookerTest4 < HookerTest
230
+ end
231
+
232
+
233
+
234
+ class HookerTest21 < HookerTest1
235
+ def hook2 ( *a )
236
+ log(:hook2, *a)
237
+ end
238
+ end
239
+
240
+
241
+
242
+ def test_hookable
243
+ hookable_checker HookableTest.new
244
+ end
245
+
246
+
247
+
248
+ def test_sub_hookable
249
+ hookable_checker SubHookableTest.new
250
+ end
251
+
252
+
253
+
254
+ def hookable_checker ( support )
255
+ hookers = []
256
+ hookers << HookerTest1.new
257
+ hookers << HookerTest2.new
258
+ hookers << HookerTest3.new
259
+ hookers << HookerTest21.new
260
+ hooker4 = HookerTest4.new
261
+ hookers << hooker4
262
+ hookers.each do |hooker|
263
+ support << hooker
264
+ end
265
+ STATIC_HOOKER_TEST.reset
266
+ hookers.unshift STATIC_HOOKER_TEST
267
+ hooker4.hook_method(:hook2_by_dynamic_hook_method, :hook2)
268
+ @a = []
269
+ support.subscribe_hook(:hook2) do |*a|
270
+ assert_nothing_raised do
271
+ @a << [:proc_hooker_call, :hook2, *a]
272
+ end
273
+ end
274
+ support.run
275
+ hookers.zip(support.class.ref).each do |hooker, ref|
276
+ assert_equal(ref, hooker.stack)
277
+ end
278
+ assert_equal([[:proc_hooker_call, :hook2, 42]], @a)
279
+ end
280
+
281
+ end
282
+
283
+ end
data/src/hooker.rb ADDED
@@ -0,0 +1,54 @@
1
+ # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: hooker.rb 233 2005-05-17 08:35:52Z ertai $
5
+
6
+ require 'rubygems'
7
+ require_gem 'activesupport'
8
+ require 'active_support/class_inheritable_attributes'
9
+
10
+
11
+
12
+ module Hooker
13
+
14
+ def self.included ( aClass )
15
+ super
16
+
17
+ aClass.class_eval do
18
+
19
+ class_inheritable_accessor :default_hook
20
+
21
+
22
+ def self.hook_default_method ( method )
23
+ raise unless method.is_a? Symbol
24
+ self.default_hook = method
25
+ end
26
+
27
+
28
+ def hook_method ( *a )
29
+ @@generic_hook_method[method(:instance_eval), *a]
30
+ end
31
+
32
+
33
+ def self.hook_method ( *a )
34
+ @@generic_hook_method[method(:module_eval), *a]
35
+ end
36
+
37
+
38
+ @@generic_hook_method = lambda do |eval_meth, method, *hook_names|
39
+ raise unless method.is_a? Symbol
40
+ hook_names.each do |hook_name|
41
+ raise unless hook_name.is_a? Symbol
42
+ eval_meth[%Q{
43
+ def #{hook_name} ( *a, &b )
44
+ #{method}(*a, &b)
45
+ end
46
+ }]
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end # module Hooker
@@ -0,0 +1,65 @@
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: polrop $
6
+ # $Id: node.rb 171 2005-03-29 09:12:47Z polrop $
7
+
8
+
9
+ require 'ruby_ex'
10
+ require 'abstract_node'
11
+
12
+
13
+ class IndexedNode < AbstractNode
14
+ include Concrete
15
+
16
+ def initialize(data=nil, *sub_nodes)
17
+ @sub_nodes = sub_nodes
18
+ super
19
+ end
20
+
21
+ def <<(sub_node)
22
+ check_sub_node_type(sub_node)
23
+ @sub_nodes << sub_node
24
+ end
25
+
26
+ end # class IndexedNode
27
+
28
+
29
+ test_section __FILE__ do
30
+
31
+ class IndexedNodeTest < Test::Unit::TestCase
32
+
33
+ def test_simple
34
+ s11 = IndexedNode.new(11)
35
+ s12 = IndexedNode.new(12)
36
+ s = IndexedNode.new(1, s11, s12)
37
+ assert_equal(1, s.data)
38
+ assert_equal(s11, s.sub_nodes[0])
39
+ assert_equal(s12, s.sub_nodes[1])
40
+ end
41
+
42
+ def test_crochet_equal
43
+ s = IndexedNode.new(1)
44
+ assert_nothing_raised { s[0] = IndexedNode.new(10) }
45
+ assert_raises(TypeError) { s[1] = nil }
46
+ end
47
+
48
+ def test_merge
49
+ s = IndexedNode.new(1)
50
+ s11 = IndexedNode.new(11)
51
+ s12 = IndexedNode.new(12)
52
+ s.merge!({ 0 => s11, 1 => s12 })
53
+ end
54
+
55
+ def test_chevron
56
+ s = IndexedNode.new(1)
57
+ assert_equal(0, s.size)
58
+ s << IndexedNode.new(2)
59
+ assert_equal(1, s.size)
60
+ end
61
+
62
+ end # class IndexedNodeTest
63
+
64
+ end
65
+
data/src/io_marshal.rb ADDED
@@ -0,0 +1,99 @@
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: io_marshal.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+
9
+ require 'ruby_ex'
10
+
11
+ class IO
12
+
13
+ def dump(*objs)
14
+ objs.each do |obj|
15
+ data = Marshal.dump(obj)
16
+ write(data.size.to_s)
17
+ write("\0")
18
+ write(data)
19
+ end
20
+ end
21
+
22
+ def load(num_objs=nil, non_block=false)
23
+ objs = []
24
+ n = num_objs
25
+ begin
26
+ return nil if eof? and non_block
27
+ objs << load_one_object(non_block)
28
+ unless n.nil?
29
+ n -= 1
30
+ break if n <= 0
31
+ end
32
+ end until eof?
33
+ objs
34
+ end
35
+
36
+ def load_one_object(non_block=false)
37
+ size = ''
38
+ while (c = read(1)) != "\0" do
39
+ if c.nil?
40
+ if non_block
41
+ return nil
42
+ else
43
+ sleep(0.1)
44
+ redo
45
+ end
46
+ end
47
+ size += c
48
+ end
49
+ size = size.to_i
50
+ data = read(size)
51
+ Marshal.load(data)
52
+ end
53
+
54
+ end # class IO
55
+
56
+
57
+ #
58
+ # Unit test suite
59
+ #
60
+ test_section __FILE__ do
61
+
62
+
63
+ class IOMarshalTest < Test::Unit::TestCase
64
+
65
+ #
66
+ # Tests
67
+ #
68
+ def test_simple
69
+ obj1 = 12
70
+ obj2 = 'hello'
71
+ obj3 = { 'a' => 12, 'b' => 42 }
72
+ rd, wr = IO.pipe
73
+ if pid = fork # father
74
+ wr.close
75
+ o1, o2 = rd.load(2)
76
+ assert_equal(obj1, o1)
77
+ assert_equal(obj2, o2)
78
+ o3 = rd.load
79
+ assert_equal([obj3], o3)
80
+ assert_equal(nil, rd.load(4, true))
81
+ assert_equal(nil, rd.load_one_object(true))
82
+ Process.waitpid(pid)
83
+ rd.close
84
+ else # son
85
+ rd.close
86
+ sleep(0.5)
87
+ wr.dump(obj1)
88
+ wr.dump(obj2, obj3)
89
+ wr.close
90
+ exit
91
+ end
92
+ end
93
+
94
+
95
+ end # class IOMarshalTest
96
+
97
+
98
+ end
99
+