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
data/lib/core_ex/yaml.rb CHANGED
@@ -1,54 +1,18 @@
1
1
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
2
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
3
  # License:: Gnu General Public License.
4
- # Revision:: $Id: yaml.rb 300 2005-06-23 20:45:47Z ertai $
5
-
6
- require 'yaml'
7
- require 'core_ex'
8
-
9
-
10
- class Class
11
-
12
- def yaml_extension ( theYamlType=nil )
13
- case theYamlType
14
- when NilClass
15
- theYamlType = "!#{self.name.downcase}"
16
- when String
17
- theYamlType = theYamlType
18
- when Symbol
19
- theYamlType = "!#{theYamlType}"
20
- else
21
- raise ArgumentError, "Expect a symbol or a string not: #{theYamlType}"
22
- end
4
+ # Revision:: $Id: yaml.rb 342 2005-09-08 01:12:48Z ertai $
23
5
 
24
- class_eval do
25
- undef to_yaml_type
26
- undef to_yaml
27
- def to_yaml ( opts={} )
28
- YAML::quick_emit(self.object_id, opts) do |out|
29
- out << "#{to_yaml_type} #{to_yaml_string}"
30
- end
31
- end
32
- end
33
6
 
34
- class_eval %{
35
- def to_yaml_type
36
- '#{theYamlType}'
37
- end
38
- }
39
-
40
- theClass = self
41
- YAML.add_builtin_type(theYamlType) do |type, val|
42
- theClass.yaml_load val
43
- end
44
- end
45
-
46
- end # class Class
7
+ module CoreEx
47
8
 
9
+ module Yaml
10
+ end # module Yaml
48
11
 
12
+ end # module CoreEx
49
13
 
50
14
  class Regexp
51
- yaml_extension :re
15
+ have YamlExtension, :re
52
16
 
53
17
  def to_yaml_string
54
18
  source
@@ -78,7 +42,7 @@ end # class Regexp
78
42
 
79
43
 
80
44
  class Range
81
- yaml_extension
45
+ have YamlExtension
82
46
 
83
47
  def to_yaml_string
84
48
  to_s
@@ -94,48 +58,77 @@ end # class Range
94
58
 
95
59
 
96
60
 
97
- class Proc
98
- yaml_extension
61
+ class Array
99
62
 
100
- def self.yaml_load ( val )
101
- DumpableProc.new(val.to_s)
63
+ alias_method :old_to_yaml, :to_yaml
64
+
65
+ def to_yaml ( opts={} )
66
+ em = opts[:Emitter]
67
+ if (opts[:Inline] or (em and em.options[:Inline])) and not is_complex_yaml?
68
+ YAML::quick_emit(self.object_id, opts) do |out|
69
+ out << '['
70
+ first = true
71
+ each do |x|
72
+ if first
73
+ first = false
74
+ else
75
+ out << ', '
76
+ end
77
+ x.to_yaml(:Emitter => out)
78
+ end
79
+ out << ']'
80
+ end
81
+ else
82
+ old_to_yaml(opts)
83
+ end
102
84
  end
103
85
 
104
- def to_yaml_string
105
- to_s
86
+
87
+ undef_method :is_complex_yaml?
88
+
89
+ def is_complex_yaml?
90
+ any? { |x| x.is_complex_yaml? } or inspect.size > 80
106
91
  end
107
92
 
108
- end # class Proc
93
+ end # class Array
109
94
 
110
95
 
111
96
 
112
- class PathList
113
- yaml_extension :filelist
97
+ class Hash
114
98
 
115
- def self.yaml_load ( val )
116
- new(val)
117
- end
99
+ alias_method :old_to_yaml, :to_yaml
118
100
 
119
- undef to_yaml
120
101
  def to_yaml ( opts={} )
121
- to_a.to_yaml(opts)
102
+ em = opts[:Emitter]
103
+ if (opts[:Inline] or (em and em.options[:Inline])) and not is_complex_yaml?
104
+ YAML::quick_emit(self.object_id, opts) do |out|
105
+ out << '{'
106
+ first = true
107
+ each do |k, v|
108
+ if first
109
+ first = false
110
+ else
111
+ out << ', '
112
+ end
113
+ k.to_yaml(:Emitter => out)
114
+ out << ': '
115
+ v.to_yaml(:Emitter => out)
116
+ end
117
+ out << '}'
118
+ end
119
+ else
120
+ old_to_yaml(opts)
121
+ end
122
122
  end
123
123
 
124
- end # class PathList
125
-
126
124
 
127
- class Pathname
128
- yaml_extension :path
129
-
130
- def to_yaml_string
131
- to_s
132
- end
125
+ undef_method :is_complex_yaml?
133
126
 
134
- def self.yaml_load ( val )
135
- new(val.to_s)
127
+ def is_complex_yaml?
128
+ any? { |x| x.is_complex_yaml? } or inspect.size > 80
136
129
  end
137
130
 
138
- end # class Pathname
131
+ end # class Hash
139
132
 
140
133
 
141
134
 
@@ -148,19 +141,7 @@ end
148
141
  test_section __FILE__ do
149
142
 
150
143
  class YamlExTest < Test::Unit::TestCase
151
-
152
- def assert_yaml_load ( aString, aClass, anObject=nil )
153
- assert_nothing_raised { @val = YAML::load(aString) }
154
- assert_kind_of(aClass, @val)
155
- assert_equal(anObject, @val) unless anObject.nil?
156
- @ref = aString
157
- end
158
-
159
- def assert_yaml_dump ( anObject, aString )
160
- assert_nothing_raised { @str = anObject.to_yaml }
161
- assert_equal(aString, @str)
162
- end
163
-
144
+ include YamlExtension::Assertions
164
145
 
165
146
 
166
147
  def test_regexp
@@ -179,14 +160,6 @@ test_section __FILE__ do
179
160
  assert_match(@val, "af\nffb")
180
161
  end
181
162
 
182
- def test_pathname
183
- str = 'foo/bar/baz.rb'
184
- assert_yaml_load "--- !path #{str}", Pathname, str.to_path
185
- assert_equal('baz.rb', @val.basename.to_s)
186
- assert_equal(str, @val.to_s)
187
- assert_yaml_dump str.to_path, @ref
188
- end
189
-
190
163
  def test_range
191
164
  assert_yaml_load "--- !range 0..10", Range, 0..10
192
165
  assert_yaml_dump @val, @ref
@@ -194,21 +167,51 @@ test_section __FILE__ do
194
167
  assert_yaml_dump @val, @ref
195
168
  end
196
169
 
197
- def test_proc
198
- assert_yaml_load "--- !proc 3 + 7", Proc
199
- assert_yaml_dump @val, @ref
200
- assert_equal(10, @val[])
170
+ def test_ruby
171
+ assert_yaml_load "--- !ruby 2 + 4", Integer, 6
172
+ assert_yaml_load "--- !ruby '[2, 4]'", Array, [2, 4]
201
173
  end
202
174
 
203
- def test_pathlist
204
- ls = __FILE__.to_path.dirname + '*.rb'
205
- assert_yaml_load "--- !filelist #{ls}", PathList, PathList[ls]
206
- assert_yaml_dump @val, @val.to_a.to_yaml
175
+ def test_array
176
+ opts = { :Inline => true }
177
+ assert_yaml_load '--- [2, "4"]', Array, [2, '4']
178
+ assert_yaml_dump @val, @ref, opts
179
+ assert_yaml_load '--- [2, "4"]', Array, [2, '4']
180
+ assert_yaml_dump @val, "--- \n- 2\n- \"4\""
181
+ assert_yaml_load "--- [2, [!range 1..4]]", Array, [2, [1..4]]
182
+ assert_yaml_dump @val, "--- \n- 2\n- \n - !range 1..4", opts
183
+ arr = (0 .. 25).to_a
184
+ str = arr.join(', ')
185
+ str2 = arr.join("\n- ")
186
+ assert_yaml_load "--- [#{str}]", Array, arr
187
+ assert_yaml_dump @val, "--- \n- #{str2}", opts
188
+ assert_yaml_load '--- [2, {a: 4}]', Array, [2, {'a' => 4}]
189
+ assert_yaml_dump @val, @ref, opts
207
190
  end
208
191
 
209
- def test_ruby
210
- assert_yaml_load "--- !ruby 2 + 4", Integer, 6
211
- assert_yaml_load "--- !ruby '[2, 4]'", Array, [2, 4]
192
+ def test_hash
193
+ opts = { :Inline => true }
194
+ assert_yaml_load '--- {a: "4"}', Hash, { 'a' => '4' }
195
+ assert_yaml_dump @val, @ref, opts
196
+ assert_yaml_load '--- {a: "4"}', Hash, { 'a' => '4' }
197
+ assert_yaml_dump @val, "--- \na: \"4\""
198
+ assert_yaml_load "--- {a: [4]}", Hash, { 'a' => [4] }
199
+ assert_yaml_dump @val, @ref, opts
200
+ hash = Hash[*(0 .. 23).to_a]
201
+ arr = hash.map{|k,v| "#{k}: #{v}"}.sort
202
+ str = arr.join(', ')
203
+ str2 = arr.join('|')
204
+ assert_yaml_load "--- {#{str}}", Hash, hash
205
+ assert_yaml_dump @val, /\A--- \n(?:(?:#{str2})\n)*(?:#{str2})\Z/, opts
206
+ end
207
+
208
+ def test_no_inline
209
+ opts = { :Inline => false }
210
+ hash = Hash[*(0 .. 3).to_a]
211
+ tree = [hash.dup, hash.dup, hash.dup, hash.dup]
212
+ @no_inline = "--- \n- \n 0: 1\n 2: 3\n- \n 0: 1\n 2: 3\n- \n 0: 1\n 2: 3\n- \n 0: 1\n 2: 3"
213
+ assert_yaml_dump tree, @no_inline, opts
214
+ assert_yaml_dump tree, @no_inline
212
215
  end
213
216
 
214
217
  end # class YamlExTest
data/lib/core_ex.rb CHANGED
@@ -1,56 +1,267 @@
1
1
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
2
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
3
  # License:: Gnu General Public License.
4
- # Revision:: $Id: core_ex.rb 301 2005-06-23 20:57:05Z ertai $
4
+ # Revision:: $Id: core_ex.rb 352 2005-09-15 01:43:03Z ertai $
5
5
 
6
- require 'pathname'
6
+ unless defined? CORE_EX_LOADED and CORE_EX_LOADED
7
+ CORE_EX_LOADED = true
8
+ $LOADED_FEATURES << 'core_ex.rb' unless $LOADED_FEATURES.include? 'core_ex.rb'
7
9
 
8
- module CoreEx
9
- @@dir = Pathname.new(__FILE__).dirname.expand_path
10
- $: << @@dir.to_s
11
- $" << 'core_ex.rb' unless $".include? 'core_ex.rb'
10
+ require 'pathname'
11
+ require 'set'
12
+
13
+ CORE_EX_DIR = Pathname.new(__FILE__).dirname.expand_path.cleanpath
14
+
15
+ core_ex_long = (CORE_EX_DIR + 'core_ex.rb').to_s
16
+ $LOADED_FEATURES << core_ex_long unless $LOADED_FEATURES.include? core_ex_long
17
+
18
+ CORE_EX_VENDORS = [] unless defined? CORE_EX_VENDORS
19
+ CORE_EX_VENDORS << CORE_EX_DIR.parent + 'vendor'
12
20
 
13
- @@core_ex = @@dir + 'core_ex'
21
+ def core_ex_debug ( &block )
22
+ # STDERR.puts block[].inspect
23
+ end
14
24
 
15
- %w[ embedded_tests pathname require ].each do |aPath|
16
- require "core_ex/#{aPath}"
25
+ def core_ex_vendor_require ( dir_name, dir_version='*' )
26
+ if CORE_EX_VENDORS.any? { |dir| dir.exist? }
27
+ dir = Pathname.glob("{#{CORE_EX_VENDORS.join(',')}}/#{dir_name}#{dir_version}/{lib,}").first
28
+ if dir and dir.directory?
29
+ if defined? CoreEx::Pathname and Pathname.include? CoreEx::Pathname
30
+ dir.load_path!
31
+ else
32
+ $LOAD_PATH << dir.to_s
33
+ end
34
+ core_ex_debug { [:vendor, dir_name, dir_version] }
35
+ else
36
+ raise LoadError, "no vendor dir #{dir}"
37
+ end
38
+ else
39
+ core_ex_debug { :novendor }
40
+ end
17
41
  end
18
- RequireSystem.instance
19
- %w[ attr_once enumerable exception
20
- string fileutils pathlist yaml ].each do |aPath|
21
- (@@core_ex + aPath).require
42
+
43
+ def core_ex_register_vendor ( dir )
44
+ CORE_EX_VENDORS << dir unless CORE_EX_VENDORS.include? dir
22
45
  end
23
46
 
24
- def self.each ( &block )
25
- @@core_list ||= PathList[@@core_ex + '**/*.rb']
26
- @@core_list.each(&block)
47
+ begin
48
+ require 'rubygems'
49
+ def core_ex_gem_require ( gem_name, gem_version='> 0' )
50
+ require_gem gem_name, gem_version
51
+ core_ex_debug { [:require_gem, gem_name, gem_version] }
52
+ end
53
+ rescue LoadError => ex
54
+ def core_ex_gem_require ( *a, &b )
55
+ core_ex_debug { :nogem }
56
+ end
27
57
  end
28
58
 
29
- def self.require
30
- @@core_list ||= PathList[@@core_ex + '**/*.rb']
31
- @@core_list.require
59
+ def core_ex_require ( name, gem_name=name, dir_name=name, gem_version='> 0', dir_version='*' )
60
+ begin
61
+ core_ex_vendor_require(dir_name, dir_version)
62
+ rescue LoadError => ex
63
+ begin
64
+ core_ex_gem_require(gem_name, gem_version)
65
+ rescue LoadError
66
+ end
67
+ end
68
+ require name
32
69
  end
33
70
 
34
- def self.dir
35
- @@dir
71
+ verbose = $VERBOSE
72
+ $VERBOSE = false
73
+ core_ex_require 'active_support', 'activesupport', 'activesupport', '~> 1.1.1', '-1.1.1'
74
+
75
+ # <<< little active_support patch
76
+ module Inflector
77
+ def underscore(camel_cased_word)
78
+ camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z])/,'_\1').gsub(/(^|\/)_/, '\1').downcase
79
+ end
80
+ def constantize(camel_cased_word, start=Object)
81
+ camel_cased_word.split("::").inject(start) do |final_type, part|
82
+ return nil unless final_type.const_defined? part
83
+ final_type.const_get(part)
84
+ end
85
+ end
86
+ end # module Inflector
87
+ module Dependencies
88
+ class LoadingModule
89
+ def const_load!(name, file_name = nil)
90
+ file_name ||= 'application' if root? && name.to_s == 'ApplicationController'
91
+ path = self.path + [file_name || name]
92
+
93
+ load_paths.each do |load_path|
94
+ fs_path = load_path.filesystem_path(path)
95
+ next unless fs_path
96
+
97
+ case
98
+ when File.directory?(fs_path)
99
+ new_module = LoadingModule.new(self.root, self.path + [name])
100
+ self.const_set name, new_module
101
+ if self.root?
102
+ if Object.const_defined?(name)
103
+ msg = "Cannot load module #{name}: Object::#{name} is set to #{Object.const_get(name).inspect}"
104
+ raise NameError, msg
105
+ end
106
+ Object.const_set(name, new_module)
107
+ end
108
+ # <<<
109
+ module_fs_path = fs_path + '.rb'
110
+ self.root.load_file!(module_fs_path) if File.exist?(module_fs_path)
111
+ # >>>
112
+ break
113
+ when File.file?(fs_path)
114
+ self.root.load_file!(fs_path)
115
+
116
+ # Import the loaded constant from Object provided we are the root node.
117
+ self.const_set(name, Object.const_get(name)) if self.root? && Object.const_defined?(name)
118
+ break
119
+ end
120
+ end
121
+
122
+ self.const_defined?(name)
123
+ end
124
+ end # class LoadingModule
125
+ end # module Dependencies
126
+ # >>>
127
+
128
+ $VERBOSE = verbose
129
+
130
+ DEPENDENCIES_MECHANISM = :require unless defined? DEPENDENCIES_MECHANISM
131
+ Dependencies.mechanism = DEPENDENCIES_MECHANISM
132
+
133
+ if Object.const_defined? :Controllers
134
+ # Perhaps a rails environement
135
+ raise 'bad Controllers' unless Controllers.is_a? Dependencies::RootLoadingModule
136
+ Controllers.load_paths << Dependencies::ConstantLoadPath.new(CORE_EX_DIR.to_s)
137
+ else
138
+ Controllers = Dependencies::LoadingModule.root(CORE_EX_DIR.to_s)
139
+ end
140
+ Controllers::CoreEx.name # .import! is not yet defined
141
+ Controllers::Test::Unit::UI::Yaml.name
142
+
143
+ module CoreEx
144
+
145
+ module_function
146
+
147
+ def dir
148
+ CORE_EX_DIR
149
+ end
150
+
151
+ def core_ex
152
+ dir + 'core_ex'
36
153
  end
37
154
 
38
155
  end # module CoreEx
39
156
 
40
- core_ex = CoreEx.dir + 'core_ex'
41
- {
42
- :Time => 'time',
43
- :DTime => 'dtime',
44
- :DumpableProc => 'dumpable_proc',
45
- :Version => 'version',
46
- :Test => 'test/unit/ui/yaml/testrunner',
47
- :TempPath => 'temp_path'
48
- }.each do |k, v|
49
- k = [k] unless k.is_a? Enumerable
50
- v = [v] unless v.is_a? Enumerable
51
- k.each do |aConst|
52
- v.each do |aPath|
53
- Kernel.autoload(aConst, (core_ex + aPath).to_s)
157
+ class Object
158
+ include CoreEx::EmbeddedTests
159
+ end # class Object
160
+ if defined? EMBEDDED_TEST_MODE
161
+ embedded_test_mode EMBEDDED_TEST_MODE
162
+ end
163
+
164
+ class String
165
+ include CoreEx::String
166
+ end # class String
167
+
168
+ class Module
169
+ include CoreEx::Module::MixInWithArgs
170
+ include CoreEx::Module::AttrOnce
171
+ include CoreEx::Module::Import
172
+ end # class Module
173
+
174
+ class Object
175
+ include CoreEx::Object::TheFirstTime
176
+ include CoreEx::Object::SingletonClass
177
+ include CoreEx::Object::InstanceEvalWithArgs
178
+ end # class Object
179
+
180
+ class Proc
181
+ include CoreEx::Proc
182
+ end # class Proc
183
+
184
+ module Dependencies
185
+ class ConstantLoadPath
186
+ include CoreEx::DependenciesExt::ConstantLoadPath
187
+ end # class ConstantLoadPath
188
+ end # module Dependencies
189
+
190
+ class Pathname
191
+ include CoreEx::Pathname
192
+ end # class Pathname
193
+
194
+ $LOADED_FEATURES.dup.each do |feature|
195
+ if (path = Pathname.new(feature)).absolute?
196
+ load_path, base_path = path.split_with_load_path($LOAD_PATH)
197
+ unless load_path.nil?
198
+ new_feature = base_path.to_s
199
+ next if $LOADED_FEATURES.include? new_feature
200
+ $LOADED_FEATURES << new_feature
201
+ end
202
+ end
203
+ end
204
+
205
+ module Kernel
206
+
207
+ alias_method :require_without_unification, :require
208
+ def require ( file, *a )
209
+ if (path = Pathname.new(file)).absolute?
210
+ load_path, base_path = path.split_with_load_path
211
+ unless load_path.nil?
212
+ feature = base_path.to_s
213
+ return false if $LOADED_FEATURES.include? feature
214
+ $LOADED_FEATURES << feature
215
+ end
54
216
  end
217
+ require_without_unification(file, *a)
55
218
  end
219
+
220
+ end # module Kernel
221
+
222
+ module Enumerable
223
+ include CoreEx::Enumerable
224
+ end # module Enumerable
225
+
226
+ class Exception
227
+ include CoreEx::Exception
228
+ end # class Exception
229
+
230
+ require 'fileutils'
231
+ module FileUtils
232
+ include CoreEx::FileUtils::RemoveDir
233
+ end # module FileUtils
234
+
235
+ require 'time'
236
+ class Time
237
+ include CoreEx::Time
238
+ end # class Time
239
+
240
+ CoreEx::Yaml.import!
241
+ CoreEx::TryDup.import!
242
+
243
+
244
+ require 'pp'
245
+ silence_warnings { require 'active_support/breakpoint' }
246
+
247
+ test_section __FILE__ do
248
+
249
+ class TestCoreEx < ::Test::Unit::TestCase
250
+
251
+ def setup
252
+ end
253
+
254
+ def teardown
255
+ end
256
+
257
+ def test_simple
258
+ assert_kind_of Dependencies::LoadingModule, Controllers::CoreEx
259
+ assert_raise(NameError) { EmbeddedTests }
260
+ assert_kind_of Module, CoreEx::EmbeddedTests
261
+ end
262
+
263
+ end # class TestCoreEx
264
+
265
+ end
266
+
56
267
  end
@@ -1,16 +1,13 @@
1
- # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
- # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
- # License: Gnu General Public License.
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
+ # Revision:: $Id: d_time.rb 334 2005-09-04 14:29:40Z ertai $
4
5
 
5
- # $LastChangedBy: ertai $
6
- # $Id: dtime.rb 252 2005-05-31 23:41:42Z ertai $
7
6
 
8
7
 
9
- require 'core_ex'
10
- require 'core_ex/time'
11
-
12
8
 
13
9
  class DTime
10
+ have YamlExtension
14
11
 
15
12
  def initialize(delta)
16
13
  @delta = delta.to_f.abs
@@ -26,7 +23,16 @@ class DTime
26
23
  end
27
24
 
28
25
  def inspect
29
- "#@day days #@hour hours #@min mins #@sec secs".freeze
26
+ res = []
27
+ res << "#@day days" unless @day.zero?
28
+ res << "#@hour hours" unless @hour.zero?
29
+ res << "#@min mins" unless @min.zero?
30
+ res << "#@sec secs" if res.empty? or not @sec.zero?
31
+ res.join ' '
32
+ end
33
+
34
+ def to_yaml_string
35
+ inspect
30
36
  end
31
37
 
32
38
  def to_a
@@ -55,21 +61,22 @@ class DTime
55
61
  @delta.round
56
62
  end
57
63
 
58
- def to_yaml(opts={})
59
- ((@day != 0 ? "#@day days " : '') +
60
- (@hour != 0 ? "#@hour hours " : '') +
61
- (@min != 0 ? "#@min mins " : '') +
62
- "#@sec secs").to_yaml(opts)
63
- end
64
-
65
64
  def self.diff ( *args, &block )
66
65
  start = Time.now
67
66
  block[*args]
68
67
  return Time.now - start
69
68
  end
70
69
 
70
+ def == ( rhs )
71
+ @delta == rhs.to_f
72
+ end
73
+
74
+ def method_missing ( *a, &b )
75
+ @delta.send(*a, &b)
76
+ end
77
+
71
78
  attr_once :to_s, :inspect, :to_a, :to_i, :hash, :to_hash, :floor,
72
- :round, :to_yaml
79
+ :round
73
80
 
74
81
  end # class DTime
75
82
 
@@ -77,6 +84,7 @@ end # class DTime
77
84
  test_section __FILE__ do
78
85
 
79
86
  class DTimeTest < Test::Unit::TestCase
87
+ include YamlExtension::Assertions
80
88
 
81
89
  def test_simple
82
90
  d = DTime.new(260.33)
@@ -106,7 +114,7 @@ class DTimeTest < Test::Unit::TestCase
106
114
  def test_conversion
107
115
  d = DTime.new(260.75)
108
116
  assert_equal('260.75', d.to_s)
109
- assert_equal('0 days 0 hours 4 mins 20.75 secs', d.inspect)
117
+ assert_equal('4 mins 20.75 secs', d.inspect)
110
118
  assert_equal(260, d.to_f.floor)
111
119
  assert_equal(260, d.to_i)
112
120
  assert_equal(260, d.floor)
@@ -129,13 +137,13 @@ class DTimeTest < Test::Unit::TestCase
129
137
 
130
138
  def test_to_yaml
131
139
  d = DTime.new(265678.42000)
132
- assert_equal('--- 3 days 1 hours 47 mins 58.4199999999837 secs', d.to_yaml)
140
+ assert_yaml_dump(d, '--- !dtime 3 days 1 hours 47 mins 58.4199999999837 secs')
133
141
  d = DTime.new(5678.42000)
134
- assert_equal('--- 1 hours 34 mins 38.4200000000001 secs', d.to_yaml)
142
+ assert_yaml_dump(d, '--- !dtime 1 hours 34 mins 38.4200000000001 secs')
135
143
  d = DTime.new(158.42000)
136
- assert_equal('--- 2 mins 38.42 secs', d.to_yaml)
144
+ assert_yaml_dump(d, '--- !dtime 2 mins 38.42 secs')
137
145
  d = DTime.new(58.42000)
138
- assert_equal('--- 58.42 secs', d.to_yaml)
146
+ assert_yaml_dump(d, '--- !dtime 58.42 secs')
139
147
  end
140
148
 
141
149
  def test_diff
@@ -162,6 +170,12 @@ class DTimeTest < Test::Unit::TestCase
162
170
  assert_equal(0, d.day)
163
171
  end
164
172
 
173
+ def test_method_missing
174
+ c = DTime.new 260.33
175
+ assert_nothing_raised { c /= 2 }
176
+ assert_nothing_raised { c > 3 }
177
+ end
178
+
165
179
  end # class DTimeTest
166
180
 
167
181
 
@@ -1,9 +1,8 @@
1
1
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
2
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
3
  # License:: Gnu General Public License.
4
- # Revision:: $Id: dumpable_proc.rb 299 2005-06-19 10:47:10Z ertai $
4
+ # Revision:: $Id: dumpable_proc.rb 334 2005-09-04 14:29:40Z ertai $
5
5
 
6
- require 'core_ex'
7
6
 
8
7
  class DumpableProc < Proc
9
8