ruby_ex 0.1.2 → 0.2.0

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 (122) hide show
  1. data/ChangeLog +693 -0
  2. data/NEWS +74 -0
  3. data/SPEC.dyn.yml +6 -6
  4. data/SPEC.gemspec +14 -0
  5. data/SPEC.yml +4 -4
  6. data/lib/abstract.rb +2 -4
  7. data/lib/abstract_node.rb +1 -2
  8. data/lib/algorithms/simulated_annealing.rb +50 -29
  9. data/lib/attributed_class.rb +50 -21
  10. data/lib/auto_object.rb +102 -0
  11. data/lib/blank_slate.rb +102 -0
  12. data/lib/cache.rb +1 -2
  13. data/lib/choose.rb +165 -163
  14. data/lib/commands.rb +2 -3
  15. data/lib/commands/command.rb +47 -20
  16. data/lib/commands/datas.rb +1 -1
  17. data/lib/commands/datas/composite.rb +5 -1
  18. data/lib/commands/datas/data.rb +102 -5
  19. data/lib/commands/datas/factory.rb +13 -6
  20. data/lib/commands/datas/temp.rb +3 -5
  21. data/lib/commands/factory.rb +1 -1
  22. data/lib/commands/helpers.rb +1 -1
  23. data/lib/commands/pipe.rb +10 -1
  24. data/lib/commands/runners.rb +1 -1
  25. data/lib/commands/runners/exec.rb +1 -1
  26. data/lib/commands/runners/fork.rb +3 -16
  27. data/lib/commands/runners/mock.rb +67 -0
  28. data/lib/commands/runners/runner.rb +5 -3
  29. data/lib/commands/runners/system.rb +1 -1
  30. data/lib/commands/seq.rb +2 -1
  31. data/lib/config_file.rb +10 -2
  32. data/lib/const_regexp.rb +1 -2
  33. data/lib/{dlogger.rb → d_logger.rb} +1 -2
  34. data/lib/daemon.rb +1 -2
  35. data/lib/diff.rb +1 -2
  36. data/lib/drb/drb_observable.rb +1 -2
  37. data/lib/drb/drb_observable_pool.rb +2 -2
  38. data/lib/drb/drb_service.rb +1 -2
  39. data/lib/drb/drb_undumped_attributes.rb +1 -2
  40. data/lib/drb/drb_undumped_indexed_object.rb +1 -2
  41. data/lib/drb/insecure_protected_methods.rb +1 -2
  42. data/lib/drb_ex.rb +2 -2
  43. data/lib/file_type.rb +466 -0
  44. data/lib/generate_id.rb +12 -6
  45. data/lib/genpasswd.rb +22 -0
  46. data/lib/hash_eval.rb +83 -0
  47. data/lib/histogram.rb +1 -2
  48. data/lib/hookable.rb +3 -4
  49. data/lib/hooker.rb +1 -3
  50. data/lib/html_encode.rb +191 -0
  51. data/lib/indexed_node.rb +0 -1
  52. data/lib/io_marshal.rb +4 -2
  53. data/lib/ioo.rb +3 -2
  54. data/lib/kill_all.rb +46 -0
  55. data/lib/labeled_node.rb +0 -1
  56. data/lib/logger_observer.rb +8 -4
  57. data/lib/md5sum.rb +3 -3
  58. data/lib/meta_factory.rb +99 -0
  59. data/lib/method_call.rb +87 -0
  60. data/lib/mocks.rb +12 -0
  61. data/lib/mocks/assertions.rb +50 -0
  62. data/lib/mocks/method_logger.rb +40 -0
  63. data/lib/mocks/mock.rb +64 -0
  64. data/lib/mocks/object.rb +47 -0
  65. data/lib/mocks/observer.rb +38 -0
  66. data/lib/module/autoload_tree.rb +30 -29
  67. data/lib/module/hierarchy.rb +176 -171
  68. data/lib/module/instance_method_visibility.rb +39 -38
  69. data/lib/node.rb +0 -1
  70. data/lib/object_monitor.rb +1 -2
  71. data/lib/object_monitor_activity.rb +1 -2
  72. data/lib/observable.rb +1 -2
  73. data/lib/observable_pool.rb +1 -2
  74. data/lib/{orderedhash.rb → ordered_hash.rb} +41 -5
  75. data/lib/pp_hierarchy.rb +7 -2
  76. data/lib/r_path.rb +307 -0
  77. data/lib/random_generators.rb +7 -20
  78. data/lib/random_generators/random_generator.rb +2 -4
  79. data/lib/random_generators/ruby.rb +4 -2
  80. data/lib/regex_path.rb +124 -0
  81. data/lib/ruby_ex.rb +28 -98
  82. data/lib/safe_eval.rb +1 -2
  83. data/lib/sendmail.rb +14 -17
  84. data/lib/service_manager.rb +1 -2
  85. data/lib/shuffle.rb +6 -2
  86. data/lib/spring.rb +1 -2
  87. data/lib/spring_set.rb +1 -2
  88. data/lib/{symtbl.rb → sym_tbl.rb} +90 -5
  89. data/lib/sym_tbl_gsub.rb +227 -0
  90. data/lib/{synflow.rb → syn_flow.rb} +1 -2
  91. data/lib/text.rb +218 -0
  92. data/lib/timeout_ex.rb +1 -2
  93. data/lib/trace.rb +9 -8
  94. data/lib/uri/druby.rb +1 -2
  95. data/lib/uri/file.rb +1 -1
  96. data/lib/uri/ftp_ex.rb +1 -1
  97. data/lib/uri/http_ex.rb +1 -1
  98. data/lib/uri/mysql.rb +121 -0
  99. data/lib/uri/pgsql.rb +19 -38
  100. data/lib/uri/svn.rb +1 -2
  101. data/lib/uri_ex.rb +45 -3
  102. data/lib/verbose_object.rb +181 -38
  103. data/lib/yaml/chop_header.rb +19 -11
  104. data/lib/yaml/transform.rb +17 -11
  105. data/lib/yaml/yregexpath.rb +11 -5
  106. data/test/algorithms/simulated_annealing_test.rb +2 -2
  107. data/test/resources/foo.tar.gz +0 -0
  108. data/test/resources/tar.gz.log +49 -0
  109. data/test/sanity-suite.yml +5 -7
  110. data/test/sanity/multiple-requires.yml +17 -7
  111. data/test/sanity/single-requires.yml +38 -20
  112. data/test/stress-tests/threads_and_exceptions.yml +13 -0
  113. data/test/test-unit-setup.rb +3 -1
  114. data/test/unit-suite.yml +7 -8
  115. metadata +42 -31
  116. data/lib/algorithms.rb +0 -12
  117. data/lib/ask.rb +0 -100
  118. data/lib/checkout.rb +0 -12
  119. data/lib/dumpable_proc.rb +0 -57
  120. data/lib/filetype.rb +0 -229
  121. data/lib/thread_mutex.rb +0 -11
  122. data/lib/yaml/basenode_ext.rb +0 -63
data/lib/timeout_ex.rb CHANGED
@@ -3,10 +3,9 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: timeout_ex.rb 279 2005-06-05 17:46:53Z ertai $
6
+ # $Id: timeout_ex.rb 339 2005-09-06 23:27:27Z ertai $
7
7
 
8
8
 
9
- require 'ruby_ex'
10
9
  require 'thread'
11
10
 
12
11
 
data/lib/trace.rb CHANGED
@@ -1,12 +1,9 @@
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, 2005 Nicolas Despres. All rights reserved.
2
+ # Author:: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: trace.rb 339 2005-09-06 23:27:27Z ertai $
4
5
 
5
- # $LastChangedBy: ertai $
6
- # $Id: trace.rb 279 2005-06-05 17:46:53Z ertai $
7
-
8
-
9
- module Kernel
6
+ module Trace
10
7
 
11
8
  def trace_on
12
9
  set_trace_func proc { |event, file, line, id, binding, classname|
@@ -23,4 +20,8 @@ module Kernel
23
20
  set_trace_func nil
24
21
  end
25
22
 
23
+ end # module Trace
24
+
25
+ module Kernel
26
+ include Trace
26
27
  end # module Kernel
data/lib/uri/druby.rb CHANGED
@@ -3,8 +3,7 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: druby.rb 279 2005-06-05 17:46:53Z ertai $
7
-
6
+ # $Id: druby.rb 339 2005-09-06 23:27:27Z ertai $
8
7
 
9
8
  require 'uri_ex'
10
9
 
data/lib/uri/file.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: file.rb 279 2005-06-05 17:46:53Z ertai $
6
+ # $Id: file.rb 258 2005-06-01 00:22:51Z ertai $
7
7
 
8
8
  require 'uri_ex'
9
9
 
data/lib/uri/ftp_ex.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: ftp_ex.rb 279 2005-06-05 17:46:53Z ertai $
6
+ # $Id: ftp_ex.rb 258 2005-06-01 00:22:51Z ertai $
7
7
 
8
8
  require 'uri_ex'
9
9
  require 'uri/ftp'
data/lib/uri/http_ex.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: http_ex.rb 279 2005-06-05 17:46:53Z ertai $
6
+ # $Id: http_ex.rb 258 2005-06-01 00:22:51Z ertai $
7
7
 
8
8
  require 'uri_ex'
9
9
  require 'uri/http'
data/lib/uri/mysql.rb ADDED
@@ -0,0 +1,121 @@
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: mysql.rb 310 2005-06-27 07:14:36Z ertai $
5
+
6
+ require 'uri_ex'
7
+
8
+ module URI
9
+
10
+ class MySQL < Generic
11
+
12
+ SCHEME = 'mysql'.freeze
13
+ DEFAULT_HOST = 'localhost'.freeze
14
+ DEFAULT_PORT = 3306
15
+ DEFAULT_QUERY = ''.freeze
16
+ DUMP = 'mysqldump'.to_cmd.freeze
17
+
18
+ COMPONENT = [
19
+ :scheme,
20
+ :userinfo,
21
+ :host,
22
+ :path,
23
+ :query
24
+ ].freeze
25
+
26
+
27
+ def self.build ( args )
28
+ tmp = Util::make_components_hash(self, args)
29
+ return super(tmp)
30
+ end
31
+
32
+
33
+ def mk_connection_opts
34
+ opts = []
35
+ opts << '-h' << @host
36
+ opts << '-P' << @port if @port != DEFAULT_PORT
37
+ opts << '-p' << @password if @password
38
+ opts << '-u' << @user if @user
39
+ opts
40
+ end
41
+
42
+
43
+ def mk_dump_opts ( out=nil )
44
+ opts = %w[ -C ]
45
+ opts << '-r' << out if out
46
+ base, table = pathname.split
47
+ base, table = base.to_s, table.to_s
48
+ if base =~ /[\.\/]/
49
+ opts << table
50
+ else
51
+ if base =~ /\//
52
+ raise ArgumentError, "Use database/table not #{pathname}"
53
+ end
54
+ opts << base << table
55
+ end
56
+ opts
57
+ end
58
+
59
+
60
+ def save
61
+ out = TempPath.new('dump', pathname.basename.to_s)
62
+ cmd = DUMP + mk_connection_opts + mk_dump_opts(out) + mk_custom_opts
63
+ cmd.run(self.runner)
64
+ out
65
+ end
66
+
67
+ end # class MySQL
68
+
69
+ @@schemes[MySQL::SCHEME.upcase] = MySQL
70
+
71
+ test_section __FILE__ do
72
+ class MySQLTest < Test::Unit::TestCase
73
+
74
+ def setup
75
+ assert_nothing_raised do
76
+ @foo = URI.parse('mysql://foo/qux/bar')
77
+ @foo_at_bar = URI.parse('mysql://foo:baz@bar/baz/ben')
78
+ @foo_at_bar_42 = URI.parse('mysql://foo@bar:42/bing')
79
+ @cmplx = URI.parse('mysql://foo@bar/qux/bob?a=b,c,d=e,f,ghi,jkl=mno')
80
+ end
81
+ @all = [ @foo, @foo_at_bar, @foo_at_bar_42, @cmplx ]
82
+ end
83
+
84
+ def test_0_parse_basic
85
+ @all.each do |uri|
86
+ assert_kind_of(MySQL, uri)
87
+ end
88
+ end
89
+
90
+ def test_with_query
91
+ ref = %w[-h bar -u foo -C -r out bob -a b -c -d e -f --ghi --jkl mno]
92
+ assert_equal(ref, @cmplx.mk_connection_opts +
93
+ @cmplx.mk_dump_opts('out') +
94
+ @cmplx.mk_custom_opts)
95
+ end
96
+
97
+ def test_mk_connection_opts
98
+ assert_equal(['-h', 'foo'], @foo.mk_connection_opts)
99
+ assert_equal(%w[-h bar -p baz -u foo], @foo_at_bar.mk_connection_opts)
100
+ assert_equal(%w[-h bar -P]+[42, '-u', 'foo'],
101
+ @foo_at_bar_42.mk_connection_opts)
102
+ assert_equal(["-h", "bar", "-u", "foo"], @cmplx.mk_connection_opts)
103
+ end
104
+
105
+
106
+ def test_mk_dump_opts
107
+ base = %w[-C -r out]
108
+ assert_equal(%w[ -C bar ], @foo.mk_dump_opts())
109
+ assert_equal(base + ['bar'], @foo.mk_dump_opts('out'))
110
+ assert_equal(base + ['ben'], @foo_at_bar.mk_dump_opts('out'))
111
+ assert_equal(base + ['bing'], @foo_at_bar_42.mk_dump_opts('out'))
112
+ assert_equal(base + ['bob'], @cmplx.mk_dump_opts('out'))
113
+ end
114
+
115
+ def test_save
116
+ # FIXME
117
+ end
118
+ end # class MySQLTest
119
+ end
120
+
121
+ end # module URI
data/lib/uri/pgsql.rb CHANGED
@@ -1,7 +1,7 @@
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: pgsql.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: pgsql.rb 310 2005-06-27 07:14:36Z ertai $
5
5
 
6
6
  require 'uri_ex'
7
7
 
@@ -13,7 +13,7 @@ module URI
13
13
  DEFAULT_HOST = 'localhost'.freeze
14
14
  DEFAULT_PORT = 5432
15
15
  DEFAULT_QUERY = ''.freeze
16
- PG_DUMP = 'pg_dump'.to_cmd.freeze
16
+ DUMP = 'pg_dump'.to_cmd.freeze
17
17
 
18
18
  COMPONENT = [
19
19
  :scheme,
@@ -39,25 +39,9 @@ module URI
39
39
  end
40
40
 
41
41
 
42
- def mk_custom_opts
43
- opts = []
44
- return opts if @query.nil?
45
- @query.split(/,/).map do |x|
46
- k, v = x.split(/=/)
47
- if k.size == 1
48
- opts << "-#{k}"
49
- else
50
- opts << "--#{k}"
51
- end
52
- opts << v unless v.nil?
53
- end
54
- opts
55
- end
56
-
57
-
58
- def mk_pg_dump_opts ( out )
59
- opts = %w[ -F c -Z 9 -C -f ]
60
- opts << out
42
+ def mk_dump_opts ( out=nil )
43
+ opts = %w[ -F c -Z 9 -C ]
44
+ opts << '-f' << out if out
61
45
  base, table = pathname.split
62
46
  base, table = base.to_s, table.to_s
63
47
  if base =~ /[\.\/]/
@@ -74,7 +58,7 @@ module URI
74
58
 
75
59
  def save
76
60
  out = TempPath.new('dump', pathname.basename.to_s)
77
- cmd = PG_DUMP + mk_connection_opts + mk_pg_dump_opts(out) + mk_custom_opts
61
+ cmd = DUMP + mk_connection_opts + mk_dump_opts(out) + mk_custom_opts
78
62
  cmd.run(self.runner)
79
63
  out
80
64
  end
@@ -87,21 +71,25 @@ module URI
87
71
  class PgSQLTest < Test::Unit::TestCase
88
72
 
89
73
  def setup
90
- assert_nothing_raised { @foo = URI.parse('pgsql://foo/qux/bar') }
91
- assert_nothing_raised { @foo_at_bar = URI.parse('pgsql://foo@bar/baz/ben') }
92
- assert_nothing_raised { @foo_at_bar_42 = URI.parse('pgsql://foo@bar:42/bing') }
93
74
  assert_nothing_raised do
75
+ @foo = URI.parse('pgsql://foo/qux/bar')
76
+ @foo_at_bar = URI.parse('pgsql://foo@bar/baz/ben')
77
+ @foo_at_bar_42 = URI.parse('pgsql://foo@bar:42/bing')
94
78
  @cmplx = URI.parse('pgsql://foo@bar/qux/bob?a=b,c,d=e,f,ghi,jkl=mno')
95
79
  end
80
+ @all = [ @foo, @foo_at_bar, @foo_at_bar_42, @cmplx ]
96
81
  end
97
82
 
98
83
  def test_0_parse_basic
84
+ @all.each do |uri|
85
+ assert_kind_of(PgSQL, uri)
86
+ end
99
87
  end
100
88
 
101
89
  def test_with_query
102
90
  ref = %w[-h bar -U foo -F c -Z 9 -C -f out bob -a b -c -d e -f --ghi --jkl mno]
103
91
  assert_equal(ref, @cmplx.mk_connection_opts +
104
- @cmplx.mk_pg_dump_opts('out') +
92
+ @cmplx.mk_dump_opts('out') +
105
93
  @cmplx.mk_custom_opts)
106
94
  end
107
95
 
@@ -112,19 +100,12 @@ module URI
112
100
  assert_equal(["-h", "bar", "-U", "foo"], @cmplx.mk_connection_opts)
113
101
  end
114
102
 
115
- def test_mk_custom_opts
116
- assert_equal([], @foo.mk_custom_opts)
117
- assert_equal([], @foo_at_bar.mk_custom_opts)
118
- assert_equal([], @foo_at_bar_42.mk_custom_opts)
119
- assert_equal(%w[-a b -c -d e -f --ghi --jkl mno], @cmplx.mk_custom_opts)
120
- end
121
-
122
- def test_mk_pg_dump_opts
103
+ def test_mk_dump_opts
123
104
  base = %w[-F c -Z 9 -C -f out]
124
- assert_equal(base + ['bar'], @foo.mk_pg_dump_opts('out'))
125
- assert_equal(base + ['ben'], @foo_at_bar.mk_pg_dump_opts('out'))
126
- assert_equal(base + ['bing'], @foo_at_bar_42.mk_pg_dump_opts('out'))
127
- assert_equal(base + ['bob'], @cmplx.mk_pg_dump_opts('out'))
105
+ assert_equal(base + ['bar'], @foo.mk_dump_opts('out'))
106
+ assert_equal(base + ['ben'], @foo_at_bar.mk_dump_opts('out'))
107
+ assert_equal(base + ['bing'], @foo_at_bar_42.mk_dump_opts('out'))
108
+ assert_equal(base + ['bob'], @cmplx.mk_dump_opts('out'))
128
109
  end
129
110
 
130
111
  def test_save
data/lib/uri/svn.rb CHANGED
@@ -3,10 +3,9 @@
3
3
  # License: Gnu General Public License.
4
4
 
5
5
  # $LastChangedBy: ertai $
6
- # $Id: svn.rb 279 2005-06-05 17:46:53Z ertai $
6
+ # $Id: svn.rb 339 2005-09-06 23:27:27Z ertai $
7
7
 
8
8
  require 'uri_ex'
9
- require 'abstract'
10
9
 
11
10
  module URI
12
11
 
data/lib/uri_ex.rb CHANGED
@@ -1,12 +1,12 @@
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: uri_ex.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: uri_ex.rb 339 2005-09-06 23:27:27Z ertai $
5
5
 
6
6
  require 'uri'
7
- require 'ruby_ex'
8
- require 'commands'
9
7
 
8
+ module UriEx
9
+ end # module UriEx
10
10
 
11
11
  module URI
12
12
 
@@ -66,6 +66,48 @@ module URI
66
66
  end
67
67
  end
68
68
 
69
+
70
+ def mk_custom_opts
71
+ opts = []
72
+ return opts if @query.nil?
73
+ @query.split(/,/).map do |x|
74
+ k, v = x.split(/=/)
75
+ if k.size == 1
76
+ opts << "-#{k}"
77
+ else
78
+ opts << "--#{k}"
79
+ end
80
+ opts << v unless v.nil?
81
+ end
82
+ opts
83
+ end
84
+
85
+
69
86
  end # module Generic
70
87
 
88
+
89
+
90
+ test_section __FILE__ do
91
+ class GenericTest < Test::Unit::TestCase
92
+
93
+ def setup
94
+ assert_nothing_raised { @foo = URI.parse('http://foo/qux/bar') }
95
+ assert_nothing_raised do
96
+ @cmplx = URI.parse('http://foo@bar/qux/bob?a=b,c,d=e,f,ghi,jkl=mno')
97
+ end
98
+ end
99
+
100
+ def test_mk_custom_opts
101
+ assert_equal([], @foo.mk_custom_opts)
102
+ assert_equal(%w[-a b -c -d e -f --ghi --jkl mno], @cmplx.mk_custom_opts)
103
+ end
104
+
105
+ end # class GenericTest
106
+ end
107
+
108
+ PathList[__FILE__.to_path.dirname + 'uri/**/*.rb'].each do |path|
109
+ require path.to_s
110
+ end
111
+
71
112
  end # module URI
113
+
@@ -1,70 +1,213 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: verbose_object.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: verbose_object.rb 339 2005-09-06 23:27:27Z ertai $
5
5
 
6
- require 'ruby_ex'
6
+ require 'blank_slate'
7
+ require 'method_call'
7
8
 
8
9
  module Verbosify
9
10
 
10
- SKIP_METHODS = %w[ instance_eval ]
11
+ attr_accessor :verbose_object_reference
12
+ attr_reader :verbose_object_options
11
13
 
12
- def self.extend_object ( anObject )
13
- anObject.methods.each do |meth|
14
- next if meth =~ /__.*__/
15
- next if SKIP_METHODS.include? meth
16
- anObject.instance_eval %Q{
17
- def #{meth} ( *a, &b )
18
- __log__(:#{meth}, *a, &b)
19
- super
20
- end
21
- }
14
+ def initialize ( anObject, opts=nil )
15
+ @verbose_object_reference = anObject
16
+ self.verbose_object_options = opts
17
+ end
18
+
19
+ def verbose_object_options= ( opts )
20
+ @verbose_object_reference ||= nil
21
+ @verbose_object_options = opts || {}
22
+ unless @verbose_object_options.is_a? Hash
23
+ raise ArgumentError,
24
+ "Need a hash of options not a #@verbose_object_options"
22
25
  end
23
- anObject.instance_eval do
24
- def verbose_object?
25
- true
26
- end
27
- def __log__ ( *a )
28
- STDERR.puts "LOG: #{a.inspect}"
26
+ if @verbose_object_options[:logger].nil?
27
+ logger = @verbose_object_options[:logger] = Object.new
28
+ def logger.log ( obj, *a )
29
+ STDERR.puts "LOG: #{obj.__id__}: #{a.inspect}"
29
30
  end
30
31
  end
31
32
  end
32
33
 
33
- end # module Verbosify
34
+ def verbose_object?
35
+ true
36
+ end
34
37
 
38
+ def verbose_object_send ( m, *a, &b )
39
+ if @verbose_object_reference.nil?
40
+ __send__("__blank_slate_#{m}", *a, &b)
41
+ else
42
+ @verbose_object_reference.__send__(m, *a, &b)
43
+ end
44
+ end
35
45
 
36
- class VerboseObject
46
+ def method_missing ( m, *a, &b )
47
+ super($1.to_sym) if m.to_s =~ /^__blank_slate_(.+)/
48
+ verbose_object_method_missing(m, *a, &b)
49
+ end
37
50
 
38
- def initialize ( anObject )
39
- @obj = anObject
51
+ def verbose_object_method_missing ( m, *a, &b )
52
+ begin
53
+ @verbose_object_options[:logger].log(self, m, *a)
54
+ rescue NoMethodError => ex
55
+ STDERR.puts "No valid logger (#{m}) (#{ex})"
56
+ end
57
+ res = verbose_object_send(m, *a, &b)
58
+ return res if @verbose_object_options[:no_rec]
59
+ call = MethodCall.new(m, *a, &b)
60
+ opts = @verbose_object_options.merge(:call => call, :last => self)
61
+ res.verbosify(opts)
40
62
  end
41
63
 
42
- instance_methods.each do |meth|
43
- next if meth =~ /__.*__/
44
- module_eval %Q{
45
- def #{meth} ( *a, &b )
46
- method_missing(:#{meth}, *a, &b)
47
- end
48
- }
64
+ def verbose_object_trace ( action=nil )
65
+ call = @verbose_object_options[:call]
66
+ last = @verbose_object_options[:last]
67
+ return verbose_object_empty_trace(action) if call.nil?
68
+ if last.__blank_slate_eql? self
69
+ last = verbose_object_empty_trace(action)
70
+ else
71
+ last = last.verbose_object_trace(action)
72
+ end
73
+ call.to_ruby(last)
49
74
  end
50
75
 
51
- def verbose_object?
52
- true
76
+ def verbose_object_empty_trace ( action )
77
+ inspect # FIXME
78
+ end
79
+
80
+ end # module Verbosify
81
+
82
+
83
+ class VerboseObject
84
+ def self.blank_slate_ignore ( name )
85
+ super or name =~ /^(verbos(ify|e_object)|(nil\?|inspect|send)$)/
53
86
  end
87
+ include BlankSlate
88
+ include Verbosify
89
+ end # class VerboseObject
54
90
 
55
- def __log__ ( *a )
56
- STDERR.puts "LOG: #{a.inspect}"
91
+
92
+ class Class
93
+
94
+ def verbosify! ( opts=nil )
95
+ raise TypeError
57
96
  end
58
97
 
59
- def method_missing ( *a, &b )
60
- __log__(*a)
61
- @obj.__send__(*a, &b)
98
+ alias_method :old_class_equal, :==
99
+ def == ( rhs )
100
+ return object_id == rhs.object_id
62
101
  end
63
102
 
64
- end # class VerboseObject
103
+ end # class Class
104
+
65
105
 
66
106
  class Object
107
+
67
108
  def verbose_object?
68
109
  false
69
110
  end
111
+
112
+ def verbosify ( opts=nil )
113
+ begin
114
+ verbosify!(opts)
115
+ rescue TypeError => ex
116
+ VerboseObject.new(self, opts)
117
+ end
118
+ end
119
+
120
+ def verbosify! ( opts=nil )
121
+ unless blank_slate?
122
+ class << self
123
+ def self.blank_slate_ignore ( name )
124
+ VerboseObject.blank_slate_ignore(name)
125
+ end
126
+ end
127
+ extend BlankSlate unless blank_slate?
128
+ end
129
+ __blank_slate_extend Verbosify unless verbose_object?
130
+ self.verbose_object_options = opts
131
+ self
132
+ rescue TypeError => ex
133
+ raise TypeError, "can't make a verbose object from #{self} (#{ex})"
134
+ end
135
+
70
136
  end # class Object
137
+
138
+ test_section __FILE__ do
139
+ require 'mocks'
140
+
141
+ class TestVerboseObject < ::Test::Unit::TestCase
142
+ include Mocks::Assertions
143
+
144
+ def assert_verbosify ( anObject )
145
+ assert_nothing_raised { return anObject.verbosify(@opts) }
146
+ end
147
+
148
+ def assert_same_result ( myVerboseObject, refObject, aMethod, *args, &block )
149
+ assert_nothing_raised do
150
+ my = myVerboseObject.__send__(aMethod, *args, &block)
151
+ ref = refObject.__send__(aMethod, *args, &block)
152
+ assert_equal(ref, my)
153
+ end
154
+ end
155
+
156
+ def setup
157
+ @mock_object = Mocks::Object.new
158
+ @opts = { :logger => @mock_object, :no_rec => false }
159
+ @str = "foo"
160
+ @arr = [1, 3, @str.dup, [1, 2]]
161
+ @hash = { :foo => @arr.dup, :bar => "value" }
162
+ @num = 42
163
+ @objs = [@str, @arr, @hash, @num]
164
+ end
165
+
166
+ def teardown
167
+ end
168
+
169
+ def assert_same_result_many ( meth, *args, &block )
170
+ @objs.each do |element|
171
+ assert_same_result(assert_verbosify(element.try_dup), element, meth, *args, &block)
172
+ end
173
+ end
174
+
175
+ def test_simple
176
+ assert_same_result_many :size
177
+ assert_mock [@str, :size], [3, :==, 3],
178
+ [@arr, :size], [4, :==, 4],
179
+ [@hash, :size], [2, :==, 2],
180
+ [@num, :size], [4, :==, 4]
181
+ end
182
+
183
+ def test_class
184
+ assert_same_result_many :class
185
+ mock_object.mock_freeze
186
+ args = mock_object.mock_args.delete_if { |x, y, z| y == :== }
187
+ assert_equal [[@str, :class], [String, :object_id],
188
+ [@arr, :class], [Array, :object_id],
189
+ [@hash, :class], [Hash, :object_id],
190
+ [@num, :class], [Fixnum, :object_id]], args
191
+ end
192
+
193
+ def test_with_class
194
+ @class = String
195
+ @vclass = assert_verbosify @class
196
+ assert_same_result @vclass, @class, :name
197
+ mock_object.mock_freeze
198
+ assert_mock [@class, :name]
199
+ end
200
+
201
+ def test_trace
202
+ @vrange = assert_verbosify 1..3
203
+ assert_nothing_raised do
204
+ @res = (@vrange.map{|x| x + 1}.join(',').size * -4).abs
205
+ end
206
+ assert_equal 20, @res
207
+ assert_equal '(1..3.each { ... }.map { ... }.join(",").size * -4).abs',
208
+ @res.verbose_object_trace
209
+ end
210
+
211
+ end # class TestVerboseObject
212
+
213
+ end