core_ex 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/NEWS +39 -1
  2. data/SPEC.dyn.yml +6 -6
  3. data/SPEC.gemspec +13 -0
  4. data/SPEC.yml +3 -3
  5. data/lib/core_ex/dependencies_ext/constant_load_path.rb +23 -0
  6. data/lib/core_ex/embedded_tests.rb +29 -23
  7. data/lib/core_ex/enumerable.rb +10 -18
  8. data/lib/core_ex/exception.rb +24 -21
  9. data/lib/core_ex/file_utils.rb +51 -0
  10. data/lib/core_ex/module/attr_once.rb +41 -0
  11. data/lib/core_ex/module/import.rb +28 -0
  12. data/lib/core_ex/module/mix_in_with_args.rb +267 -0
  13. data/lib/core_ex/object/instance_eval_with_args.rb +56 -0
  14. data/lib/core_ex/object/singleton_class.rb +78 -0
  15. data/lib/core_ex/object/the_first_time.rb +32 -0
  16. data/lib/core_ex/pathname.rb +268 -164
  17. data/lib/core_ex/proc.rb +77 -0
  18. data/lib/core_ex/rakefile_base.rf +93 -51
  19. data/lib/core_ex/require.rb +43 -384
  20. data/lib/core_ex/string.rb +52 -41
  21. data/lib/core_ex/time.rb +26 -41
  22. data/lib/core_ex/try_dup.rb +68 -0
  23. data/lib/core_ex/yaml.rb +103 -100
  24. data/lib/core_ex.rb +246 -35
  25. data/lib/{core_ex/dtime.rb → d_time.rb} +36 -22
  26. data/lib/{core_ex/dumpable_proc.rb → dumpable_proc.rb} +1 -2
  27. data/lib/{core_ex/pathlist.rb → path_list.rb} +111 -63
  28. data/lib/{core_ex/temp_path.rb → temp_path.rb} +55 -41
  29. data/lib/{core_ex/test/unit/ui/yaml/testrunner.rb → test/unit/u_i/yaml/test_runner.rb} +7 -10
  30. data/lib/{core_ex/version.rb → version.rb} +4 -7
  31. data/lib/yaml_extension.rb +78 -0
  32. data/test/check-core_ex.yml +6 -8
  33. data/test/check-pkg-core_ex.yml +3 -6
  34. data/test/sanity/multiple-requires.yml +41 -17
  35. data/test/sanity/single-requires.yml +36 -20
  36. data/test/sanity-suite.yml +5 -7
  37. data/test/test-unit-setup.rb +11 -3
  38. data/test/unit-suite.yml +8 -9
  39. metadata +35 -13
  40. data/lib/core_ex/attr_once.rb +0 -36
  41. data/lib/core_ex/fileutils.rb +0 -44
@@ -0,0 +1,56 @@
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: instance_eval_with_args.rb 355 2005-09-15 23:18:43Z ertai $
5
+
6
+
7
+ module CoreEx
8
+
9
+ module Object
10
+
11
+ module InstanceEvalWithArgs
12
+
13
+ @@seed ||= 0
14
+
15
+ def instance_eval_with_args ( *args, &block )
16
+ raise ArgumentError, "no block given" if block.nil?
17
+ method_name = :"instance_eval_temporary_method#{object_id}.#{@@seed}"
18
+ @@seed += 1
19
+ define_singleton_method(method_name, &block)
20
+ begin
21
+ result = __send__(method_name, *args)
22
+ ensure
23
+ undef_singleton_method(method_name)
24
+ end
25
+ result
26
+ end
27
+
28
+ end # module InstanceEvalWithArgs
29
+
30
+ end # module Object
31
+
32
+ end # module CoreEx
33
+
34
+
35
+ test_section __FILE__ do
36
+
37
+ class TestInstanceEvalWithArgs < ::Test::Unit::TestCase
38
+
39
+ def setup
40
+ end
41
+
42
+ def teardown
43
+ end
44
+
45
+ def test_instance_eval
46
+ assert_nothing_raised do
47
+ @res = "foo".instance_eval_with_args(42) do |x|
48
+ x + 1
49
+ end
50
+ end
51
+ assert_equal 43, @res
52
+ end
53
+
54
+ end # class TestInstanceEvalWithArgs
55
+
56
+ end
@@ -0,0 +1,78 @@
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: singleton_class.rb 332 2005-09-04 14:01:01Z ertai $
5
+
6
+
7
+ module CoreEx
8
+
9
+ module Object
10
+
11
+ module SingletonClass
12
+
13
+ def singleton_class
14
+ class << self
15
+ self
16
+ end
17
+ end
18
+
19
+ def define_singleton_method ( method_name, *a, &b )
20
+ singleton_class.send(:define_method, method_name, *a, &b)
21
+ end
22
+ private :define_singleton_method
23
+
24
+ def undef_singleton_method ( method_name )
25
+ singleton_class.send(:undef_method, method_name)
26
+ end
27
+ private :undef_singleton_method
28
+
29
+ def singleton_class_eval ( *args, &block )
30
+ if block.nil? or args.empty?
31
+ singleton_class.class_eval(*args, &block)
32
+ else
33
+ singleton_class.instance_eval(*args, &block)
34
+ end
35
+ end
36
+
37
+ end # module SingletonClass
38
+
39
+
40
+ test_section __FILE__ do
41
+
42
+ class TestSingletonClass < ::Test::Unit::TestCase
43
+
44
+ def setup
45
+ @string = 'foo'
46
+ end
47
+
48
+ def teardown
49
+ end
50
+
51
+ def test_singleton_class
52
+ assert_nothing_raised do
53
+ assert_kind_of Class, @string.singleton_class, 'not a class'
54
+ end
55
+ assert_equal @string.singleton_class.object_id,
56
+ @string.singleton_class.object_id, 'not the same object id'
57
+ assert_equal ::String, @string.singleton_class.superclass, 'bad super class'
58
+ end
59
+
60
+ def test_define_and_undef_singleton_method
61
+ assert_nothing_raised do
62
+ @string.send(:define_singleton_method, :foo) { 42 }
63
+ end
64
+ assert_equal 42, @string.foo
65
+ assert_raise(NoMethodError) { @string.dup.foo }
66
+ assert_nothing_raised do
67
+ @string.send(:undef_singleton_method, :foo)
68
+ end
69
+ assert_raise(NoMethodError) { @string.foo }
70
+ end
71
+
72
+ end # class TestSingletonClass
73
+
74
+ end
75
+
76
+ end # module Object
77
+
78
+ end # module CoreEx
@@ -0,0 +1,32 @@
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: the_first_time.rb 332 2005-09-04 14:01:01Z ertai $
5
+
6
+
7
+ module CoreEx
8
+
9
+ module Object
10
+
11
+ module TheFirstTime
12
+
13
+ @@set = Set.new
14
+
15
+ def the_first_time ( &block )
16
+ id = block.source_file
17
+ return if @@set.include? id
18
+ @@set << id
19
+ block[]
20
+ end
21
+
22
+ alias_method :just_one_time, :the_first_time
23
+
24
+ end # module TheFirstTime
25
+
26
+ end # module Object
27
+
28
+ end # module CoreEx
29
+
30
+ test_section __FILE__ do
31
+
32
+ end
@@ -3,145 +3,211 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: pathname.rb 272 2005-06-02 23:47:29Z ertai $
6
+ # $Id: pathname.rb 351 2005-09-14 08:33:42Z ertai $
7
7
 
8
8
 
9
- require 'pathname'
10
- require 'core_ex/embedded_tests'
11
- require 'core_ex/string'
12
-
13
-
14
- class Pathname
15
-
16
- def ensure_mkdir
17
- (mkdir) rescue Errno::EEXIST
18
- end
19
-
20
- def ensure_mkpath
21
- (mkpath) rescue Errno::EEXIST
22
- end
23
-
24
- def extsplit ( aChar='.' )
25
- raise ArgumentError, "#{aChar} is not just a char" if aChar.size != 1
26
- aChar = Regexp.escape(aChar)
27
- to_s =~ /^(.*?)(#{aChar}[^#{aChar}]*)?$/
28
- [Pathname.new($1), $2 || '']
29
- end
30
-
31
- def cp ( aPath )
32
- FileUtils.cp self.to_s, aPath.to_s
33
- end
34
-
35
- def mv ( aPath )
36
- FileUtils.mv self.to_s, aPath.to_s
37
- end
38
-
39
- def cp_r ( aPath )
40
- FileUtils.cp_r self.to_s, aPath.to_s
41
- end
42
-
43
- def rm
44
- FileUtils.rm self.to_s
45
- end
46
-
47
- def rm_r
48
- FileUtils.rm_r self.to_s
49
- end
50
-
51
- def rm_rf
52
- FileUtils.rm_rf self.to_s
53
- end
54
-
55
- def rm_f
56
- FileUtils.rm_f self.to_s
57
- end
58
-
59
- # The current ruby's unlink implementation has a bug.
60
- def unlink()
61
- if FileTest.directory? @path and not FileTest.symlink? @path
62
- Dir.unlink @path
63
- else
64
- File.unlink @path
65
- end
66
- end
67
-
68
- # Allow this kind of things:
69
- #
70
- # root = Pathname.new('/tmp/test')
71
- # foo, bar = 'foo', 'bar'
72
- #
73
- # ...
74
- #
75
- # (root/foo/bar).each_line do |line|
76
- # ...
77
- # end
78
- alias :/ :+
79
-
80
- def to_io
81
- @open_mode ||= 'r'
82
- open(@open_mode)
83
- end
84
-
85
- def to_path
86
- self
87
- end
88
-
89
- attr_accessor :open_mode
90
-
91
- def require ( *a )
92
- if a.empty?
93
- RequireSystem.instance.require(self)
94
- else
95
- Kernel.__require__(*a)
96
- end
97
- end
98
-
99
- def load ( *a )
100
- if a.empty?
101
- RequireSystem.instance.load(self)
102
- else
103
- Kernel.__load__(*a)
104
- end
105
- end
106
-
107
- def expand_path_with ( directories, extensions=nil )
108
- ext = extname
109
- exts, dirs = [''], ['']
110
- unless extensions.nil?
111
- if ext.empty?
112
- exts = extensions
9
+
10
+
11
+ module CoreEx
12
+
13
+ module Pathname
14
+
15
+ setup do
16
+ have YamlExtension, :path
17
+ alias_method :split_no_args, :split
18
+ end
19
+
20
+ # Allow this kind of things:
21
+ #
22
+ # root = Pathname.new('/tmp/test')
23
+ # foo, bar = 'foo', 'bar'
24
+ #
25
+ # ...
26
+ #
27
+ # (root/foo/bar).each_line do |line|
28
+ # ...
29
+ # end
30
+ def / ( rhs )
31
+ self + rhs
32
+ end
33
+
34
+ def ensure_mkdir
35
+ (mkdir) rescue Errno::EEXIST
36
+ end
37
+
38
+ def ensure_mkpath
39
+ (mkpath) rescue Errno::EEXIST
40
+ end
41
+
42
+ def extsplit ( aChar='.' )
43
+ raise ArgumentError, "#{aChar} is not just a char" if aChar.size != 1
44
+ aChar = Regexp.escape(aChar)
45
+ to_s =~ /^(.*?)(#{aChar}[^#{aChar}]*)?$/
46
+ [::Pathname.new($1), $2 || '']
47
+ end
48
+
49
+ # Replace the file extension with +newext+. If there is no
50
+ # extenson on the string, append the new extension to the end. If
51
+ # the new extension is not given, or is the empty string, remove
52
+ # any existing extension.
53
+ #
54
+ # +ext+ is a user added method for the String class.
55
+ def ext(newext='')
56
+ str = self.to_s
57
+ return self if ['.', '..'].include? str
58
+ if newext != ''
59
+ newext = (newext =~ /^\./) ? newext : ("." + newext)
60
+ end
61
+ (str.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || str + newext).to_path
62
+ end
63
+
64
+ def split ( *args )
65
+ args.shift if args.first == '/'
66
+ split_no_args(*args)
67
+ end
68
+
69
+ def cp ( aPath )
70
+ ::FileUtils.cp self.to_s, aPath.to_s
71
+ end
72
+
73
+ def mv ( aPath )
74
+ ::FileUtils.mv self.to_s, aPath.to_s
75
+ end
76
+
77
+ def cp_r ( aPath )
78
+ ::FileUtils.cp_r self.to_s, aPath.to_s
79
+ end
80
+
81
+ def rm
82
+ ::FileUtils.rm self.to_s
83
+ end
84
+
85
+ def rm_r
86
+ ::FileUtils.rm_r self.to_s
87
+ end
88
+
89
+ def rm_rf
90
+ ::FileUtils.rm_rf self.to_s
91
+ end
92
+
93
+ def rm_f
94
+ ::FileUtils.rm_f self.to_s
95
+ end
96
+
97
+ # The current ruby's unlink implementation has a bug.
98
+ def unlink()
99
+ if FileTest.directory? @path and not FileTest.symlink? @path
100
+ Dir.unlink @path
113
101
  else
114
- unless extensions.include? ext
115
- raise ArgumentError, "bad extension `#{ext}' for #{self}"
102
+ File.unlink @path
103
+ end
104
+ end
105
+
106
+ def to_io
107
+ @open_mode ||= :r
108
+ open(@open_mode.to_s)
109
+ end
110
+
111
+ def to_path
112
+ self
113
+ end
114
+
115
+ def to_yaml_string
116
+ to_s
117
+ end
118
+
119
+ attr_accessor :open_mode
120
+
121
+ def split_with_load_path ( load_paths=Controllers.load_paths )
122
+ str = to_s
123
+ load_paths = load_paths.sort { |x, y| y.to_path.to_s.size <=> x.to_path.to_s.size }
124
+ load_paths.each do |load_path|
125
+ if str =~ /^#{Regexp.quote(load_path.to_path)}\/*(.*)/
126
+ return [load_path.to_path, Regexp.last_match[1].to_path]
116
127
  end
117
128
  end
129
+ return nil
130
+ end
131
+
132
+ def import!
133
+ base_path = expand_path.cleanpath.to_s.gsub(/\/+/, '/').to_path.split_with_load_path.last
134
+ Dependencies.depend_on to_s unless base_path.ext.to_s.camelize.constantize
135
+ end
136
+
137
+ def load_path!
138
+ string = expand_path.cleanpath.to_s
139
+ raise "bad path name `#{string}' need a directory" unless directory?
140
+ raise 'bad Controllers' unless Controllers.is_a? Dependencies::RootLoadingModule
141
+ path = string.to_path
142
+ return false if Controllers.load_paths.any? { |c| c.to_path == path }
143
+ Controllers.load_paths.unshift Dependencies::ConstantLoadPath.new(string)
144
+ $LOAD_PATH.unshift string
118
145
  end
119
-
120
- dirs = directories unless absolute?
121
-
122
- exts.each do |ext|
123
- dirs.each do |dir|
124
- dir = dir.to_path
125
- file = dir + "#{self}#{ext}"
126
- return file.expand_path.cleanpath if file.exist?
146
+
147
+ def expand_path_with ( directories, extensions=nil )
148
+ ext = extname
149
+ exts, dirs = [''], ['']
150
+ unless extensions.nil?
151
+ if ext.empty?
152
+ exts = extensions
153
+ else
154
+ unless extensions.include? ext
155
+ raise ArgumentError, "bad extension `#{ext}' for #{self}"
156
+ end
157
+ end
158
+ end
159
+
160
+ dirs = directories unless absolute?
161
+
162
+ exts.each do |ext|
163
+ dirs.each do |dir|
164
+ dir = dir.to_path
165
+ file = dir + "#{self}#{ext}"
166
+ return file.expand_path.cleanpath if file.exist?
167
+ end
127
168
  end
169
+ return nil
128
170
  end
129
- return nil
130
- end
131
171
 
132
- module ShortCut
172
+ module ShortCut
133
173
 
134
- # Allow to use this sort of constructions:
135
- #
136
- # `/path/to/a/file`.open do |f|
137
- # ...
138
- # end
139
- def ` ( path )
140
- Pathname.new(path)
174
+ # Allow to use this sort of constructions:
175
+ #
176
+ # `/path/to/a/file`.open do |f|
177
+ # ...
178
+ # end
179
+ def ` ( path )
180
+ ::Pathname.new(path)
181
+ end
141
182
  end
142
- end
143
183
 
144
- end # class Pathname
184
+ module ClassMethods
185
+
186
+ def yaml_load ( val )
187
+ new(val.to_s)
188
+ end
189
+
190
+ end # module ClassMethods
191
+
192
+ module Assertions
193
+
194
+ def assert_expand_path_with ( inp, ref, load_path=(@load_path || $LOAD_PATH) )
195
+ assert_nothing_raised do
196
+ @my = inp.expand_path_with(load_path, %w[ .rb .so .bundle .o .dll ] << '')
197
+ ref = ref.expand_path.cleanpath unless ref.nil?
198
+ assert_equal(ref, @my, inp)
199
+ end
200
+ unless ref.nil?
201
+ assert_kind_of(Pathname, @my)
202
+ assert(@my.exist?)
203
+ end
204
+ end
205
+
206
+ end # module Assertions
207
+
208
+ end # module Pathname
209
+
210
+ end # module CoreEx
145
211
 
146
212
 
147
213
 
@@ -150,51 +216,89 @@ end # class Pathname
150
216
  #
151
217
  test_section __FILE__ do
152
218
 
219
+ require 'tempfile'
220
+
221
+ module CoreEx
222
+
223
+ class PathnameExTest < Test::Unit::TestCase
224
+ include YamlExtension::Assertions
225
+ include CoreEx::Pathname::Assertions
226
+
227
+ def setup
228
+ @test = __FILE__.to_path.dirname.parent.parent + 'test'
229
+ @res = @test + 'resources'
230
+ @req = @res + 'require'
231
+ @s = 'test_require'
232
+ @s_dne = 'test_require_dne'
233
+ @s_so = 'test_require_so.so'
234
+ @s_rb = 'test_require_rb.rb'
235
+ @p = @s.to_path
236
+ @p_dne = @s_dne.to_path
237
+ @p_so = @s_so.to_path
238
+ @p_rb = @s_rb.to_path
239
+ @ls = [ @s, @s_dne, @s_so, @s_rb ]
240
+ @lp = [ @p, @p_dne, @p_rb ] # FIXME @p_so on mac
241
+ @l = @ls + @lp
242
+ @load_path = $LOAD_PATH
243
+ end
153
244
 
154
- require 'tempfile'
245
+ #
246
+ # Tests
247
+ #
248
+ def test_ensure_dir
249
+ p = nil
250
+ begin
251
+ name = Tempfile.new('pathname')
252
+ p = name.path.to_path
253
+ name.delete
254
+ assert(! p.directory?, 'no directory')
255
+ assert_nothing_raised { p.ensure_mkdir }
256
+ assert(p.directory?, 'directory')
257
+ assert_nothing_raised { p.ensure_mkdir }
258
+ assert(p.directory?, 'still directory')
259
+ ensure
260
+ p.rmdir unless p.nil?
261
+ end
262
+ end
155
263
 
264
+ include Pathname::ShortCut
156
265
 
157
- class PathnameExTest < Test::Unit::TestCase
266
+ def test_simple
267
+ assert_equal([`path.ext1`, '.ext2'], `path.ext1.ext2`.extsplit)
268
+ assert_equal([`path`, ''], `path`.extsplit)
269
+ assert_equal([`path`, '.'], `path.`.extsplit)
270
+ assert_equal([`path`, '-ext'], `path-ext`.extsplit('-'))
271
+ assert_equal([`path-ext1`, '-ext2'], `path-ext1-ext2`.extsplit('-'))
272
+ end
158
273
 
159
- #
160
- # Tests
161
- #
162
- def test_ensure_dir
163
- p = nil
164
- begin
165
- name = Tempfile.new('pathname')
166
- p = Pathname.new(name.path)
167
- name.delete
168
- assert(! p.directory?, 'no directory')
169
- assert_nothing_raised { p.ensure_mkdir }
170
- assert(p.directory?, 'directory')
171
- assert_nothing_raised { p.ensure_mkdir }
172
- assert(p.directory?, 'still directory')
173
- ensure
174
- p.rmdir unless p.nil?
175
- end
176
- end
274
+ def test_slash
275
+ assert_equal(`path/to/a/file`, `path`/'to'/'a'/'file')
276
+ path, to, a, file = `/path`, 'to', 'a', 'file'
277
+ assert_equal(`/path/to/a/file`, path/to/a/file)
278
+ end
177
279
 
178
- include Pathname::ShortCut
280
+ def test_1_expand_path_with
281
+ @lp.each { |x| assert_expand_path_with(x, nil) }
282
+ end
179
283
 
180
- def test_simple
181
- assert_equal([`path.ext1`, '.ext2'], `path.ext1.ext2`.extsplit)
182
- assert_equal([`path`, ''], `path`.extsplit)
183
- assert_equal([`path`, '.'], `path.`.extsplit)
184
- assert_equal([`path`, '-ext'], `path-ext`.extsplit('-'))
185
- assert_equal([`path-ext1`, '-ext2'], `path-ext1-ext2`.extsplit('-'))
186
- end
284
+ def test_2_expand_path_with
285
+ @load_path << @req
286
+ @lp.delete(@p_dne)
287
+ @lp.each { |x| assert_expand_path_with(x, @req + x) }
288
+ @lp.each { |x| assert_expand_path_with(@req + x, @req + x) }
289
+ assert_expand_path_with(@p_dne, nil)
290
+ end
187
291
 
188
- def test_slash
189
- assert_equal(`path/to/a/file`, `path`/'to'/'a'/'file')
190
- path, to, a, file = `/path`, 'to', 'a', 'file'
191
- assert_equal(`/path/to/a/file`, path/to/a/file)
192
- end
292
+ def test_yaml
293
+ str = 'foo/bar/baz.rb'
294
+ assert_yaml_load "--- !path #{str}", ::Pathname, str.to_path
295
+ assert_equal('baz.rb', @val.basename.to_s)
296
+ assert_equal(str, @val.to_s)
297
+ assert_yaml_dump str.to_path, @ref
298
+ end
193
299
 
194
- # def test_expand_path_with
195
- # See core_ex/require RequireSystemTest#test_*_search
196
- # end
300
+ end # class PathnameExTest
197
301
 
198
- end # class PathnameExTest
302
+ end # module CoreEx
199
303
 
200
304
  end