ruby_ex 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +693 -0
- data/NEWS +74 -0
- data/SPEC.dyn.yml +6 -6
- data/SPEC.gemspec +14 -0
- data/SPEC.yml +4 -4
- data/lib/abstract.rb +2 -4
- data/lib/abstract_node.rb +1 -2
- data/lib/algorithms/simulated_annealing.rb +50 -29
- data/lib/attributed_class.rb +50 -21
- data/lib/auto_object.rb +102 -0
- data/lib/blank_slate.rb +102 -0
- data/lib/cache.rb +1 -2
- data/lib/choose.rb +165 -163
- data/lib/commands.rb +2 -3
- data/lib/commands/command.rb +47 -20
- data/lib/commands/datas.rb +1 -1
- data/lib/commands/datas/composite.rb +5 -1
- data/lib/commands/datas/data.rb +102 -5
- data/lib/commands/datas/factory.rb +13 -6
- data/lib/commands/datas/temp.rb +3 -5
- data/lib/commands/factory.rb +1 -1
- data/lib/commands/helpers.rb +1 -1
- data/lib/commands/pipe.rb +10 -1
- data/lib/commands/runners.rb +1 -1
- data/lib/commands/runners/exec.rb +1 -1
- data/lib/commands/runners/fork.rb +3 -16
- data/lib/commands/runners/mock.rb +67 -0
- data/lib/commands/runners/runner.rb +5 -3
- data/lib/commands/runners/system.rb +1 -1
- data/lib/commands/seq.rb +2 -1
- data/lib/config_file.rb +10 -2
- data/lib/const_regexp.rb +1 -2
- data/lib/{dlogger.rb → d_logger.rb} +1 -2
- data/lib/daemon.rb +1 -2
- data/lib/diff.rb +1 -2
- data/lib/drb/drb_observable.rb +1 -2
- data/lib/drb/drb_observable_pool.rb +2 -2
- data/lib/drb/drb_service.rb +1 -2
- data/lib/drb/drb_undumped_attributes.rb +1 -2
- data/lib/drb/drb_undumped_indexed_object.rb +1 -2
- data/lib/drb/insecure_protected_methods.rb +1 -2
- data/lib/drb_ex.rb +2 -2
- data/lib/file_type.rb +466 -0
- data/lib/generate_id.rb +12 -6
- data/lib/genpasswd.rb +22 -0
- data/lib/hash_eval.rb +83 -0
- data/lib/histogram.rb +1 -2
- data/lib/hookable.rb +3 -4
- data/lib/hooker.rb +1 -3
- data/lib/html_encode.rb +191 -0
- data/lib/indexed_node.rb +0 -1
- data/lib/io_marshal.rb +4 -2
- data/lib/ioo.rb +3 -2
- data/lib/kill_all.rb +46 -0
- data/lib/labeled_node.rb +0 -1
- data/lib/logger_observer.rb +8 -4
- data/lib/md5sum.rb +3 -3
- data/lib/meta_factory.rb +99 -0
- data/lib/method_call.rb +87 -0
- data/lib/mocks.rb +12 -0
- data/lib/mocks/assertions.rb +50 -0
- data/lib/mocks/method_logger.rb +40 -0
- data/lib/mocks/mock.rb +64 -0
- data/lib/mocks/object.rb +47 -0
- data/lib/mocks/observer.rb +38 -0
- data/lib/module/autoload_tree.rb +30 -29
- data/lib/module/hierarchy.rb +176 -171
- data/lib/module/instance_method_visibility.rb +39 -38
- data/lib/node.rb +0 -1
- data/lib/object_monitor.rb +1 -2
- data/lib/object_monitor_activity.rb +1 -2
- data/lib/observable.rb +1 -2
- data/lib/observable_pool.rb +1 -2
- data/lib/{orderedhash.rb → ordered_hash.rb} +41 -5
- data/lib/pp_hierarchy.rb +7 -2
- data/lib/r_path.rb +307 -0
- data/lib/random_generators.rb +7 -20
- data/lib/random_generators/random_generator.rb +2 -4
- data/lib/random_generators/ruby.rb +4 -2
- data/lib/regex_path.rb +124 -0
- data/lib/ruby_ex.rb +28 -98
- data/lib/safe_eval.rb +1 -2
- data/lib/sendmail.rb +14 -17
- data/lib/service_manager.rb +1 -2
- data/lib/shuffle.rb +6 -2
- data/lib/spring.rb +1 -2
- data/lib/spring_set.rb +1 -2
- data/lib/{symtbl.rb → sym_tbl.rb} +90 -5
- data/lib/sym_tbl_gsub.rb +227 -0
- data/lib/{synflow.rb → syn_flow.rb} +1 -2
- data/lib/text.rb +218 -0
- data/lib/timeout_ex.rb +1 -2
- data/lib/trace.rb +9 -8
- data/lib/uri/druby.rb +1 -2
- data/lib/uri/file.rb +1 -1
- data/lib/uri/ftp_ex.rb +1 -1
- data/lib/uri/http_ex.rb +1 -1
- data/lib/uri/mysql.rb +121 -0
- data/lib/uri/pgsql.rb +19 -38
- data/lib/uri/svn.rb +1 -2
- data/lib/uri_ex.rb +45 -3
- data/lib/verbose_object.rb +181 -38
- data/lib/yaml/chop_header.rb +19 -11
- data/lib/yaml/transform.rb +17 -11
- data/lib/yaml/yregexpath.rb +11 -5
- data/test/algorithms/simulated_annealing_test.rb +2 -2
- data/test/resources/foo.tar.gz +0 -0
- data/test/resources/tar.gz.log +49 -0
- data/test/sanity-suite.yml +5 -7
- data/test/sanity/multiple-requires.yml +17 -7
- data/test/sanity/single-requires.yml +38 -20
- data/test/stress-tests/threads_and_exceptions.yml +13 -0
- data/test/test-unit-setup.rb +3 -1
- data/test/unit-suite.yml +7 -8
- metadata +42 -31
- data/lib/algorithms.rb +0 -12
- data/lib/ask.rb +0 -100
- data/lib/checkout.rb +0 -12
- data/lib/dumpable_proc.rb +0 -57
- data/lib/filetype.rb +0 -229
- data/lib/thread_mutex.rb +0 -11
- data/lib/yaml/basenode_ext.rb +0 -63
data/NEWS
CHANGED
@@ -1,3 +1,77 @@
|
|
1
|
+
= New in 0.2, 2005-09-16:
|
2
|
+
|
3
|
+
* Many many changes and new things for RubyEx.
|
4
|
+
|
5
|
+
* Remove many requires which are now useless, many are not yet
|
6
|
+
suppressed.
|
7
|
+
|
8
|
+
* Renaming to follow the camelize/underscore convention.
|
9
|
+
|
10
|
+
* Commands:
|
11
|
+
Provides a object oriented way to manage, combine and run your commands.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
require 'rubygems' ; require_gem 'ruby_ex' ; require 'ruby_ex'
|
15
|
+
Commands.import!
|
16
|
+
ls, wc, out = 'ls'.to_cmd, 'wc'.to_cmd, 'out'.to_path
|
17
|
+
|
18
|
+
data = ls.system # other runners exist (exec, fork, sh...)
|
19
|
+
|
20
|
+
p data.status
|
21
|
+
puts data.output.read
|
22
|
+
|
23
|
+
(ls > STDOUT).system
|
24
|
+
|
25
|
+
cmd = ls['*.rb'] | wc['-l'] > out
|
26
|
+
cmd.system
|
27
|
+
|
28
|
+
puts out.read
|
29
|
+
|
30
|
+
* RPath: Add RPath, a basic (but powerful) way to search your ruby data.
|
31
|
+
|
32
|
+
* BlankSlate, Mocks, VerboseObject, and AutoObject:
|
33
|
+
|
34
|
+
- BlankSlate: For method_missing based classes.
|
35
|
+
- Mocks::Mock: Abstract mock.
|
36
|
+
- Mocks::Object: Save all method calls.
|
37
|
+
- Mocks::Observer: Respond to update.
|
38
|
+
- Mocks::Assertions: Helpers for unit tests which use these classes.
|
39
|
+
- Mocks::MethodLogger: Respond to log and treat arguments as a method call.
|
40
|
+
- VerboseObject: Proxies that display received method calls and verbosify
|
41
|
+
objects recursivly.
|
42
|
+
- AutoObject: It's like a verbose object without a real object inside.
|
43
|
+
|
44
|
+
* Text:
|
45
|
+
A little text manipulation class.
|
46
|
+
Transformation available are:
|
47
|
+
- justifying
|
48
|
+
- spliting
|
49
|
+
- croping
|
50
|
+
- clipping
|
51
|
+
|
52
|
+
* FileType:
|
53
|
+
- Use the Commands module.
|
54
|
+
- Add extractable? and installable? methods.
|
55
|
+
- Add priorities for extensions.
|
56
|
+
- Clean the declaration syntax for extensions.
|
57
|
+
- Add many unit tests.
|
58
|
+
- Add gem filetype support (with the install method).
|
59
|
+
|
60
|
+
* MetaFactory: A class that help to produce objects that
|
61
|
+
share the same initializion step.
|
62
|
+
|
63
|
+
* Html encode: Support accent encoding for the iso latin1 charset.
|
64
|
+
|
65
|
+
* URI: Support for Mysql and Pgsql.
|
66
|
+
|
67
|
+
* HashEval:
|
68
|
+
Provides a block in which methods name are keys and arguments/blocks
|
69
|
+
are values.
|
70
|
+
|
71
|
+
* SymTbl (Symbol Table):
|
72
|
+
Add symtbl_gsub methods to provide a global substitution sheme over any
|
73
|
+
objects.
|
74
|
+
|
1
75
|
= New in 0.1, 2005-05-31:
|
2
76
|
|
3
77
|
Initial Release.
|
data/SPEC.dyn.yml
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:OpenStruct
|
2
2
|
table:
|
3
|
-
:date: "Sun, 05 Jun 2005"
|
4
|
-
:version: !ruby/object:Version
|
5
|
-
build: 2
|
6
|
-
major: 0
|
7
|
-
minor: 1
|
8
|
-
revision: 282
|
9
3
|
:version_id: ''
|
10
4
|
:url: svn://svn.feydakins.org/ruby_ex/trunk
|
5
|
+
:version: !ruby/object:Version
|
6
|
+
build: 0
|
7
|
+
major: 0
|
8
|
+
minor: 2
|
9
|
+
revision: 360
|
10
|
+
:date: "Fri, 16 Sep 2005"
|
data/SPEC.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{ruby_ex}
|
3
|
+
s.version = "0.2.0"
|
4
|
+
s.date = %q{2005-09-16}
|
5
|
+
s.summary = %q{RubyEx contains general purpose Ruby extensions.}
|
6
|
+
s.email = ["nicolas.despres@gmail.com", "ertai@feydakins.org"]
|
7
|
+
s.homepage = %q{http://api.feydakins.org/ruby_ex}
|
8
|
+
s.rubyforge_project = %q{ruby_ex}
|
9
|
+
s.description = %q{}
|
10
|
+
s.authors = ["Nicolas Despr\350s", "Nicolas Pouillard"]
|
11
|
+
s.files = ["lib/abstract.rb", "lib/abstract_node.rb", "lib/attributed_class.rb", "lib/auto_object.rb", "lib/blank_slate.rb", "lib/cache.rb", "lib/choose.rb", "lib/commands.rb", "lib/config_file.rb", "lib/const_regexp.rb", "lib/d_logger.rb", "lib/daemon.rb", "lib/diff.rb", "lib/drb_ex.rb", "lib/file_type.rb", "lib/generate_id.rb", "lib/genpasswd.rb", "lib/hash_eval.rb", "lib/histogram.rb", "lib/hookable.rb", "lib/hooker.rb", "lib/html_encode.rb", "lib/indexed_node.rb", "lib/io_marshal.rb", "lib/ioo.rb", "lib/kill_all.rb", "lib/labeled_node.rb", "lib/logger_observer.rb", "lib/md5sum.rb", "lib/meta_factory.rb", "lib/method_call.rb", "lib/mocks.rb", "lib/node.rb", "lib/object_monitor.rb", "lib/object_monitor_activity.rb", "lib/observable.rb", "lib/observable_pool.rb", "lib/ordered_hash.rb", "lib/pp_hierarchy.rb", "lib/r_path.rb", "lib/random_generators.rb", "lib/regex_path.rb", "lib/ruby_ex.rb", "lib/safe_eval.rb", "lib/sendmail.rb", "lib/service_manager.rb", "lib/shuffle.rb", "lib/spring.rb", "lib/spring_set.rb", "lib/sym_tbl.rb", "lib/sym_tbl_gsub.rb", "lib/syn_flow.rb", "lib/text.rb", "lib/timeout_ex.rb", "lib/trace.rb", "lib/uri_ex.rb", "lib/verbose_object.rb", "lib/algorithms/simulated_annealing.rb", "lib/commands/command.rb", "lib/commands/datas.rb", "lib/commands/factory.rb", "lib/commands/helpers.rb", "lib/commands/pipe.rb", "lib/commands/runners.rb", "lib/commands/seq.rb", "lib/commands/datas/composite.rb", "lib/commands/datas/data.rb", "lib/commands/datas/factory.rb", "lib/commands/datas/temp.rb", "lib/commands/runners/exec.rb", "lib/commands/runners/fork.rb", "lib/commands/runners/mock.rb", "lib/commands/runners/runner.rb", "lib/commands/runners/system.rb", "lib/drb/drb_observable.rb", "lib/drb/drb_observable_pool.rb", "lib/drb/drb_service.rb", "lib/drb/drb_undumped_attributes.rb", "lib/drb/drb_undumped_indexed_object.rb", "lib/drb/insecure_protected_methods.rb", "lib/mocks/assertions.rb", "lib/mocks/method_logger.rb", "lib/mocks/mock.rb", "lib/mocks/object.rb", "lib/mocks/observer.rb", "lib/module/autoload_tree.rb", "lib/module/hierarchy.rb", "lib/module/instance_method_visibility.rb", "lib/random_generators/random_generator.rb", "lib/random_generators/ruby.rb", "lib/uri/druby.rb", "lib/uri/file.rb", "lib/uri/ftp_ex.rb", "lib/uri/http_ex.rb", "lib/uri/mysql.rb", "lib/uri/pgsql.rb", "lib/uri/ssh.rb", "lib/uri/svn.rb", "lib/yaml/chop_header.rb", "lib/yaml/transform.rb", "lib/yaml/yregexpath.rb", "test/algorithms", "test/check-pkg-ruby_ex.yml", "test/check-ruby_ex.yml", "test/resources", "test/sanity", "test/sanity-suite.yml", "test/stress-tests", "test/test-unit-setup.rb", "test/unit-suite.yml", "test/algorithms/simulated_annealing_test.rb", "test/resources/autoload_tree", "test/resources/foo.tar.gz", "test/resources/foo.txt", "test/resources/tar.gz.log", "test/resources/autoload_tree/A.rb", "test/resources/autoload_tree/B.rb", "test/resources/autoload_tree/foo", "test/resources/autoload_tree/foo/C.rb", "test/sanity/multiple-requires.yml", "test/sanity/single-requires.yml", "test/stress-tests/threads_and_exceptions.yml", "AUTHORS", "ChangeLog", "NEWS", "Rakefile", "README", "SPEC.dyn.yml", "SPEC.gemspec", "SPEC.yml"]
|
12
|
+
s.add_dependency(%q<highline>, ["~> 1.0.1"])
|
13
|
+
s.add_dependency(%q<core_ex>, ["~> 0.3.1"])
|
14
|
+
end
|
data/SPEC.yml
CHANGED
@@ -22,7 +22,7 @@ rdoc_files: !filelist
|
|
22
22
|
- README
|
23
23
|
- AUTHORS
|
24
24
|
- NEWS
|
25
|
-
-
|
25
|
+
- lib/**/*.rb
|
26
26
|
|
27
27
|
pkg_files: !filelist
|
28
28
|
- lib/**/*.rb
|
@@ -30,7 +30,7 @@ pkg_files: !filelist
|
|
30
30
|
- '[A-Z]*'
|
31
31
|
|
32
32
|
dependencies:
|
33
|
-
core_ex:
|
34
|
-
|
35
|
-
# ttk: ~> 0.2.0
|
33
|
+
core_ex: [~> 0.3.1, 6077/core_ex-0.3.1.tar.gz]
|
34
|
+
highline: [~> 1.0.1, 5143/highline-1.0.1.tgz]
|
36
35
|
|
36
|
+
ttk_version: ~> 0.3.0
|
data/lib/abstract.rb
CHANGED
@@ -3,14 +3,12 @@
|
|
3
3
|
# License: Gnu General Public License.
|
4
4
|
|
5
5
|
# $LastChangedBy: ertai $
|
6
|
-
# $Id: abstract.rb
|
6
|
+
# $Id: abstract.rb 343 2005-09-08 01:32:57Z ertai $
|
7
7
|
|
8
8
|
|
9
9
|
#FIXME: find a way to be thread safe.
|
10
10
|
|
11
|
-
|
12
|
-
require 'module/instance_method_visibility'
|
13
|
-
require 'ruby_ex'
|
11
|
+
InstanceMethodVisibility.import!
|
14
12
|
|
15
13
|
|
16
14
|
module Concrete; end
|
data/lib/abstract_node.rb
CHANGED
@@ -3,10 +3,9 @@
|
|
3
3
|
# License: Gnu General Public License.
|
4
4
|
|
5
5
|
# $LastChangedBy: ertai $
|
6
|
-
# $Id: abstract_node.rb
|
6
|
+
# $Id: abstract_node.rb 339 2005-09-06 23:27:27Z ertai $
|
7
7
|
|
8
8
|
|
9
|
-
require 'ruby_ex'
|
10
9
|
require 'abstract'
|
11
10
|
|
12
11
|
class AbstractNode
|
@@ -3,13 +3,12 @@
|
|
3
3
|
# License:: GNU General Public License (GPL).
|
4
4
|
# Revision:: $Id$
|
5
5
|
|
6
|
-
require 'ruby_ex'
|
7
6
|
require 'choose'
|
8
7
|
|
9
8
|
module Algorithms
|
10
9
|
|
11
10
|
class SimulatedAnnealing
|
12
|
-
|
11
|
+
|
13
12
|
# Example:
|
14
13
|
#
|
15
14
|
# obj = MySimulatedAnnealingObject.new
|
@@ -39,50 +38,72 @@ module Algorithms
|
|
39
38
|
@iteration = 0
|
40
39
|
@probability_threshold = 0
|
41
40
|
end
|
42
|
-
|
43
|
-
|
41
|
+
|
42
|
+
@@zeros = [0.0] * 8
|
43
|
+
|
44
|
+
def reset
|
45
|
+
@up, @down, @same, @reject, @proba_mean, @proba_mean_count,
|
46
|
+
@threshold_mean, @threshold_mean_count = @@zeros
|
47
|
+
end
|
48
|
+
|
49
|
+
|
44
50
|
def run
|
51
|
+
reset
|
45
52
|
@output.puts '---'
|
46
53
|
while @cur_cost > @global_thresold
|
47
54
|
# puts "Iteration(#{it}) #{@cur_cost}"
|
48
55
|
@transition = @support.choose_transition(@generator)
|
49
56
|
@diff_cost = @support.transition_cost(@cur_cost, @transition)
|
50
|
-
|
57
|
+
|
51
58
|
@iteration += 1
|
52
59
|
if (@iteration % @step_modulus).zero?
|
53
|
-
@temperature *= @step_multiplicator
|
60
|
+
@temperature *= @step_multiplicator
|
54
61
|
end
|
55
62
|
|
56
63
|
if @diff_cost > 0
|
57
64
|
@probability_threshold = Math.exp(- @diff_cost.to_f / @temperature)
|
58
65
|
@proba = choose_probability(@generator)
|
66
|
+
@proba_mean += @proba
|
67
|
+
@proba_mean_count += 1
|
68
|
+
@threshold_mean += @probability_threshold
|
69
|
+
@threshold_mean_count += 1
|
59
70
|
if @proba < @probability_threshold
|
60
71
|
progression '+'
|
72
|
+
@up += 1
|
61
73
|
else
|
62
74
|
progression ' '
|
75
|
+
@reject += 1
|
63
76
|
next
|
64
77
|
end
|
65
78
|
else
|
66
79
|
@probability_threshold = 1
|
67
80
|
@proba = 1
|
68
|
-
|
81
|
+
if @diff_cost.zero?
|
82
|
+
@same += 1
|
83
|
+
progression '='
|
84
|
+
else
|
85
|
+
@down += 1
|
86
|
+
progression '-'
|
87
|
+
end
|
69
88
|
end
|
70
89
|
|
71
90
|
@support.apply_transition(@transition)
|
72
91
|
@cur_cost += @diff_cost
|
73
92
|
end
|
74
93
|
end
|
75
|
-
|
76
|
-
|
94
|
+
|
95
|
+
|
77
96
|
def disp ( output=@output )
|
78
|
-
args = [ @
|
79
|
-
@
|
80
|
-
|
97
|
+
args = [ @up, @same, @down, @reject, @temperature,
|
98
|
+
@proba_mean / @proba_mean_count,
|
99
|
+
@threshold_mean / @threshold_mean_count, @cur_cost ]
|
100
|
+
fmt = '- { "+%-5d|=%-5d|-%-5d|x%-5d", temp: %-4f, ' +
|
81
101
|
'prob: %-7f, thres: %-7f, cost: %-5s }'
|
82
102
|
output.puts fmt % args
|
103
|
+
reset
|
83
104
|
end
|
84
|
-
|
85
|
-
|
105
|
+
|
106
|
+
|
86
107
|
def summary
|
87
108
|
@output.puts %Q[
|
88
109
|
|---
|
@@ -91,52 +112,52 @@ module Algorithms
|
|
91
112
|
|Nb iterations: #@iteration
|
92
113
|
].head_cut!
|
93
114
|
end
|
94
|
-
|
95
|
-
|
115
|
+
|
116
|
+
|
96
117
|
def progression ( x )
|
97
118
|
@progression = x
|
98
119
|
disp if (@iteration % @iteration_modulus).zero?
|
99
120
|
end
|
100
|
-
|
101
|
-
|
121
|
+
|
122
|
+
|
102
123
|
def choose_probability ( generator=nil )
|
103
124
|
1.0.choose(generator)
|
104
125
|
end
|
105
|
-
|
106
|
-
|
126
|
+
|
127
|
+
|
107
128
|
module Support
|
108
|
-
|
129
|
+
|
109
130
|
def included ( aModule )
|
110
|
-
|
131
|
+
|
111
132
|
aModule.module_eval do
|
112
|
-
|
133
|
+
|
113
134
|
#
|
114
135
|
# Return a transition which can be applied by apply_transition.
|
115
136
|
#
|
116
137
|
def choose_transition ( generator=nil )
|
117
138
|
raise NotImplementedError
|
118
139
|
end
|
119
|
-
|
140
|
+
|
120
141
|
#
|
121
142
|
# Return a cost difference
|
122
143
|
#
|
123
144
|
def transition_cost ( current_cost, transition )
|
124
145
|
raise NotImplementedError
|
125
146
|
end
|
126
|
-
|
147
|
+
|
127
148
|
#
|
128
149
|
# Apply the given transition to your current object.
|
129
150
|
#
|
130
151
|
def apply_transition ( transition )
|
131
152
|
raise NotImplementedError
|
132
153
|
end
|
133
|
-
|
154
|
+
|
134
155
|
end
|
135
|
-
|
156
|
+
|
136
157
|
end
|
137
|
-
|
158
|
+
|
138
159
|
end # module Support
|
139
|
-
|
160
|
+
|
140
161
|
end # class SimulatedAnnealing
|
141
162
|
|
142
163
|
end # module Algorithms
|
data/lib/attributed_class.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# Copyright:: Copyright (c) 2004, 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: attributed_class.rb
|
4
|
+
# Revision:: $Id: attributed_class.rb 339 2005-09-06 23:27:27Z ertai $
|
5
5
|
|
6
6
|
|
7
|
-
require 'ruby_ex'
|
8
7
|
|
9
8
|
# Extension to have inherited, class attributes,
|
10
9
|
# Where each class have his own attributes set.
|
@@ -85,7 +84,7 @@ module AttributedClass
|
|
85
84
|
end
|
86
85
|
|
87
86
|
def valid? ( anObject )
|
88
|
-
return
|
87
|
+
return ! mandatory? if anObject.nil?
|
89
88
|
@klass.nil? or @klass.empty? or @klass.any? { |k| anObject.is_a? k }
|
90
89
|
end
|
91
90
|
|
@@ -93,8 +92,8 @@ module AttributedClass
|
|
93
92
|
def self.set_default_proc ( &block )
|
94
93
|
@@default_proc = block
|
95
94
|
end
|
96
|
-
|
97
|
-
|
95
|
+
|
96
|
+
|
98
97
|
def help ( output=STDERR )
|
99
98
|
output << "#@name: #@descr"
|
100
99
|
output << " [#@klass]" unless @klass.nil?
|
@@ -109,6 +108,33 @@ module AttributedClass
|
|
109
108
|
help('#<') + '>'
|
110
109
|
end
|
111
110
|
|
111
|
+
def to_form ( b )
|
112
|
+
if (!@default.nil?) or @klass.nil?
|
113
|
+
gfx = @default
|
114
|
+
elsif @klass.is_a? Array and @klass.size == 1
|
115
|
+
gfx = @klass.first.default_instance
|
116
|
+
else
|
117
|
+
gfx = @klass
|
118
|
+
end
|
119
|
+
gfx = gfx.to_gfx_element
|
120
|
+
gfx.object = 'test'
|
121
|
+
gfx.builder = b
|
122
|
+
gfx.attribute = self
|
123
|
+
gfx.to_form
|
124
|
+
end
|
125
|
+
|
126
|
+
def compute_default ( default_proc_args )
|
127
|
+
unless @default_proc.nil?
|
128
|
+
pr = @default_proc
|
129
|
+
if pr.arity == default_proc_args.size
|
130
|
+
@default = pr[*default_proc_args]
|
131
|
+
else
|
132
|
+
@default = pr[self, *default_proc_args]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
return @default
|
136
|
+
end
|
137
|
+
|
112
138
|
end # class Attribute
|
113
139
|
|
114
140
|
|
@@ -137,10 +163,10 @@ module AttributedClass
|
|
137
163
|
|
138
164
|
def initialize ( anObject, anAttr, val=nil )
|
139
165
|
if val.nil?
|
140
|
-
super "Missing the
|
166
|
+
super "Missing the `#{anAttr.name}' attribute for `#{anObject}'"
|
141
167
|
else
|
142
|
-
super "Attribute
|
143
|
-
"`#{val}' in #{anObject}"
|
168
|
+
super "Attribute `#{anAttr.name}' wait for a " +
|
169
|
+
"`#{anAttr.klass.join(' or ')}' not `#{val}' in #{anObject}"
|
144
170
|
end
|
145
171
|
end
|
146
172
|
|
@@ -154,19 +180,11 @@ module AttributedClass
|
|
154
180
|
def initialize_attributes ( *default_proc_args )
|
155
181
|
self.class.attributes.each do |attr|
|
156
182
|
next if attr.is_a? MethodAttribute
|
157
|
-
|
183
|
+
default = attr.compute_default(default_proc_args)
|
184
|
+
if default.nil?
|
158
185
|
instance_variable_set("@#{attr.name}", nil)
|
159
186
|
else
|
160
|
-
|
161
|
-
pr = attr.default_proc
|
162
|
-
if pr.arity == default_proc_args.size
|
163
|
-
default = pr[*default_proc_args]
|
164
|
-
else
|
165
|
-
default = pr[attr, *default_proc_args]
|
166
|
-
end
|
167
|
-
attr.default = default
|
168
|
-
end
|
169
|
-
send "#{attr.name}=", attr.default
|
187
|
+
send "#{attr.name}=", default
|
170
188
|
end
|
171
189
|
end
|
172
190
|
end
|
@@ -231,7 +249,19 @@ module AttributedClass
|
|
231
249
|
if att = get_attribute(name, MethodAttribute)
|
232
250
|
attributes.delete(att)
|
233
251
|
end
|
234
|
-
|
252
|
+
class_eval <<-"end;", __FILE__, __LINE__
|
253
|
+
def #{name}(arg=nil, &block)
|
254
|
+
if arg.nil?
|
255
|
+
if block.nil?
|
256
|
+
@#{name}
|
257
|
+
else
|
258
|
+
self.#{name} = block[]
|
259
|
+
end
|
260
|
+
else
|
261
|
+
self.#{name} = arg
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end;
|
235
265
|
end
|
236
266
|
meth = "#{name}="
|
237
267
|
unless method_defined? meth
|
@@ -282,7 +312,6 @@ test_section __FILE__ do
|
|
282
312
|
end
|
283
313
|
cc, d, dd = nil, nil, nil
|
284
314
|
assert_nothing_raised { cc = c.new }
|
285
|
-
assert_equal(nil, cc.foo)
|
286
315
|
assert_nothing_raised { cc.initialize_attributes }
|
287
316
|
assert_equal(42, cc.foo)
|
288
317
|
assert_nothing_raised { cc.foo = 32 }
|