facets 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.rdoc +331 -35
- data/MANIFEST +685 -826
- data/{doc/guide/notes.rd → NOTES} +0 -0
- data/README.rdoc +73 -25
- data/Rakefile +245 -2
- data/TODO +5 -0
- data/demo/hook.rd +47 -0
- data/demo/scenario_require.rd +9 -0
- data/doc/README.more +24 -6
- data/doc/manual/about.rb +47 -0
- data/doc/manual/annotations.rdoc +60 -0
- data/doc/manual/associations.rdoc +55 -0
- data/doc/manual/blockups.rdoc +101 -0
- data/doc/manual/capsule.rdoc +34 -0
- data/doc/manual/command.rdoc +177 -0
- data/doc/manual/core.rdoc +37 -0
- data/doc/manual/faq.rdoc +32 -0
- data/doc/manual/typecast.html +112 -0
- data/lib/core/facets.rb +359 -11
- data/lib/core/facets/array/combination.rb +3 -3
- data/lib/core/facets/array/index.rb +4 -1
- data/lib/core/facets/array/permutation.rb +2 -2
- data/lib/core/facets/array/product.rb +1 -1
- data/lib/core/facets/binding/eval.rb +1 -1
- data/lib/core/facets/denumerable.rb +76 -0
- data/lib/core/facets/duplicable.rb +34 -0
- data/lib/core/facets/enumerable/count.rb +10 -4
- data/lib/core/facets/enumerable/defer.rb +77 -0
- data/lib/core/facets/enumerable/each_by.rb +1 -1
- data/lib/core/facets/enumerable/every.rb +35 -0
- data/lib/{more/facets/elementwise.rb → core/facets/enumerable/ewise.rb} +0 -0
- data/lib/core/facets/enumerable/filter.rb +25 -0
- data/lib/core/facets/enumerable/group_by.rb +1 -1
- data/lib/core/facets/enumerable/none.rb +3 -2
- data/lib/core/facets/enumerable/one.rb +3 -2
- data/lib/core/facets/enumerable/per.rb +61 -0
- data/lib/core/facets/exception/raised.rb +14 -0
- data/lib/core/facets/integer/odd.rb +5 -1
- data/lib/core/facets/kernel/__dir__.rb +13 -3
- data/lib/core/facets/kernel/__here__.rb +14 -0
- data/lib/core/facets/kernel/__method__.rb +9 -3
- data/lib/core/facets/kernel/ask.rb +1 -0
- data/lib/core/facets/kernel/equate.rb +13 -0
- data/lib/core/facets/kernel/extension.rb +9 -0
- data/lib/core/facets/kernel/identical.rb +4 -0
- data/lib/core/facets/kernel/instance_exec.rb +2 -1
- data/lib/core/facets/kernel/method.rb +49 -0
- data/lib/core/facets/kernel/object_send.rb +2 -2
- data/lib/core/facets/kernel/{state.rb → object_state.rb} +23 -12
- data/lib/core/facets/kernel/require_all.rb +6 -1
- data/lib/core/facets/kernel/require_local.rb +8 -1
- data/lib/core/facets/kernel/require_relative.rb +52 -0
- data/lib/core/facets/kernel/source_location.rb +13 -0
- data/lib/core/facets/kernel/tap.rb +13 -6
- data/lib/core/facets/module/attr_setter.rb +57 -0
- data/lib/core/facets/module/instance_method.rb +24 -0
- data/lib/core/facets/module/module_load.rb +60 -44
- data/lib/core/facets/module/module_require.rb +1 -0
- data/lib/core/facets/nilclass/to_f.rb +1 -1
- data/lib/core/facets/objectspace/op_fetch.rb +3 -0
- data/lib/core/facets/proc/curry.rb +4 -3
- data/lib/core/facets/string/bytes.rb +10 -4
- data/lib/core/facets/string/camelcase.rb +6 -5
- data/lib/core/facets/string/chars.rb +5 -1
- data/lib/core/facets/string/each_char.rb +1 -1
- data/lib/core/facets/string/each_word.rb +1 -1
- data/lib/core/facets/string/lines.rb +11 -4
- data/lib/core/facets/string/start_with.rb +9 -2
- data/lib/core/facets/string/unfold.rb +27 -0
- data/lib/core/facets/symbol/succ.rb +3 -3
- data/lib/core/facets/symbol/thrown.rb +20 -0
- data/lib/core/facets/symbol/to_proc.rb +3 -2
- data/lib/core/facets/time/to_time.rb +1 -1
- data/lib/core/facets/to_hash.rb +41 -100
- data/lib/core/facets/unboundmethod/name.rb +20 -23
- data/lib/more/facets/ansicode.rb +1 -10
- data/lib/more/facets/autoarray.rb +3 -31
- data/lib/more/facets/basicobject.rb +73 -0
- data/lib/more/facets/blankslate.rb +2 -66
- data/lib/{lore → more}/facets/cgi.rb +0 -0
- data/lib/more/facets/class_extend.rb +1 -0
- data/lib/{lore → more}/facets/continuation.rb +0 -0
- data/lib/{lore → more}/facets/date.rb +3 -3
- data/lib/more/facets/enumargs.rb +192 -0
- data/lib/more/facets/enumerablepass.rb +2 -216
- data/lib/more/facets/enumerator.rb +62 -0
- data/lib/more/facets/{equatable.rb → equitable.rb} +11 -11
- data/lib/more/facets/expirable.rb +13 -41
- data/lib/{lore → more}/facets/fileutils.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/head.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/safe_ln.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/slice.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/tail.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/wc.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/whereis.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/which.rb +0 -0
- data/lib/{lore → more}/facets/getoptlong.rb +0 -0
- data/lib/more/facets/hook.rb +2 -29
- data/lib/more/facets/inheritor.rb +2 -2
- data/lib/more/facets/instance_eval.rb +50 -0
- data/lib/more/facets/instance_function.rb +78 -0
- data/lib/more/facets/main.rb +20 -15
- data/lib/more/facets/memoize.rb +1 -113
- data/lib/more/facets/module/attr.rb +83 -0
- data/lib/more/facets/module/attr_tester.rb +44 -0
- data/lib/more/facets/module/attr_toggler.rb +59 -0
- data/lib/more/facets/module/attr_validator.rb +34 -0
- data/lib/more/facets/{class_extension.rb → module/class_extend.rb} +21 -13
- data/lib/more/facets/once.rb +59 -0
- data/lib/more/facets/openmodule.rb +1 -0
- data/lib/more/facets/orderedhash.rb +1 -33
- data/lib/{lore → more}/facets/ostruct.rb +0 -0
- data/lib/more/facets/ostructable.rb +1 -4
- data/lib/more/facets/partial.rb +18 -16
- data/lib/{lore → more}/facets/pathname.rb +0 -0
- data/lib/more/facets/preinitialize.rb +157 -0
- data/lib/{lore → more}/facets/rbconfig.rb +0 -0
- data/lib/more/facets/recorder.rb +1 -2
- data/lib/{lore → more}/facets/set.rb +0 -0
- data/lib/{lore → more}/facets/shellwords.rb +0 -0
- data/lib/{lore → more}/facets/uri.rb +0 -0
- data/lib/{lore → more}/facets/yaml.rb +0 -0
- data/lib/{lore → more}/facets/zlib.rb +0 -0
- data/meta/loadpath +0 -1
- data/meta/sitemap +4 -0
- data/meta/version +1 -1
- data/test/core/enumerable/test_count.rb +1 -1
- data/test/{more/test_filter.rb → core/enumerable/test_defer.rb} +24 -22
- data/test/{more/test_elementor.rb → core/enumerable/test_every.rb} +2 -15
- data/test/core/enumerable/test_ewise.rb +23 -0
- data/test/core/enumerable/test_per.rb +18 -0
- data/test/core/enumerable/test_take.rb +13 -0
- data/test/core/kernel/test_deepcopy.rb +1 -1
- data/test/{more/test_1stclassmethod.rb → core/kernel/test_method.rb} +2 -7
- data/test/core/kernel/test_tap.rb +1 -1
- data/test/core/proc/test_curry.rb +11 -0
- data/test/core/string/test_bytes.rb +1 -1
- data/test/core/string/test_camelcase.rb +23 -6
- data/test/core/string/test_lines.rb +1 -1
- data/test/core/string/test_unfold.rb +14 -0
- data/test/{more → core}/test_blank.rb +0 -0
- data/test/{more → core}/test_boolean.rb +0 -0
- data/test/{more → core}/test_functor.rb +0 -0
- data/test/{lore → more}/test_basicobject.rb +0 -0
- data/test/more/{test_class_extension.rb → test_class_extend.rb} +6 -6
- data/test/{lore → more}/test_continuation.rb +0 -0
- data/test/{lore → more}/test_date.rb +0 -0
- data/test/more/{test_enumerablepass.rb → test_enumargs.rb} +2 -4
- data/test/more/{test_equatable.rb → test_equitable.rb} +2 -2
- data/test/more/{test_instantise.rb → test_instance_function.rb} +3 -2
- data/test/more/test_memoize.rb +1 -1
- data/test/{lore → more}/test_ostruct.rb +0 -0
- metadata +865 -1016
- data/RELEASE +0 -38
- data/doc/README.lore +0 -51
- data/doc/log/basic_stats/index.html +0 -39
- data/doc/log/changelog.html +0 -648
- data/doc/log/changelog.txt +0 -217
- data/doc/log/stats/index.html +0 -39
- data/doc/log/testlog.txt +0 -278
- data/doc/notes/CHANGES +0 -2529
- data/doc/rdoc/lore/classes/Array.html +0 -176
- data/doc/rdoc/lore/classes/CGI.html +0 -191
- data/doc/rdoc/lore/classes/Config.html +0 -135
- data/doc/rdoc/lore/classes/Continuation.html +0 -113
- data/doc/rdoc/lore/classes/Date.html +0 -631
- data/doc/rdoc/lore/classes/DateTime.html +0 -583
- data/doc/rdoc/lore/classes/Enumerable.html +0 -89
- data/doc/rdoc/lore/classes/Enumerable/Enumerator.html +0 -147
- data/doc/rdoc/lore/classes/File.html +0 -128
- data/doc/rdoc/lore/classes/FileUtils.html +0 -434
- data/doc/rdoc/lore/classes/GetoptLong.html +0 -118
- data/doc/rdoc/lore/classes/GetoptLong/DSL.html +0 -208
- data/doc/rdoc/lore/classes/Kernel.html +0 -135
- data/doc/rdoc/lore/classes/Logger.html +0 -229
- data/doc/rdoc/lore/classes/Logger/Ansicolor.html +0 -277
- data/doc/rdoc/lore/classes/Logger/LogDevice.html +0 -121
- data/doc/rdoc/lore/classes/NilClass.html +0 -119
- data/doc/rdoc/lore/classes/OpenStruct.html +0 -432
- data/doc/rdoc/lore/classes/Pathname.html +0 -353
- data/doc/rdoc/lore/classes/Set.html +0 -117
- data/doc/rdoc/lore/classes/Shellwords.html +0 -111
- data/doc/rdoc/lore/classes/String.html +0 -140
- data/doc/rdoc/lore/classes/Time.html +0 -154
- data/doc/rdoc/lore/classes/URI.html +0 -454
- data/doc/rdoc/lore/classes/URI/Hash.html +0 -105
- data/doc/rdoc/lore/classes/URI/Kernel.html +0 -122
- data/doc/rdoc/lore/classes/Zlib.html +0 -188
- data/doc/rdoc/lore/created.rid +0 -1
- data/doc/rdoc/lore/files/README.html +0 -286
- data/doc/rdoc/lore/files/doc/README_lore.html +0 -155
- data/doc/rdoc/lore/files/lib/lore/facets/basicobject_rb.html +0 -118
- data/doc/rdoc/lore/files/lib/lore/facets/cgi_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/continuation_rb.html +0 -147
- data/doc/rdoc/lore/files/lib/lore/facets/date_rb.html +0 -97
- data/doc/rdoc/lore/files/lib/lore/facets/enumerator_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/head_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/safe_ln_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/slice_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/tail_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/wc_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/whereis_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/which_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils_rb.html +0 -131
- data/doc/rdoc/lore/files/lib/lore/facets/getoptlong_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/logger_rb.html +0 -142
- data/doc/rdoc/lore/files/lib/lore/facets/ostruct_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/pathname_rb.html +0 -145
- data/doc/rdoc/lore/files/lib/lore/facets/rbconfig_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/set_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/shellwords_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/uri_rb.html +0 -125
- data/doc/rdoc/lore/files/lib/lore/facets/yaml_rb.html +0 -146
- data/doc/rdoc/lore/files/lib/lore/facets/zlib_rb.html +0 -97
- data/doc/rdoc/lore/fr_class_index.html +0 -73
- data/doc/rdoc/lore/fr_file_index.html +0 -71
- data/doc/rdoc/lore/fr_method_index.html +0 -177
- data/doc/rdoc/lore/index.html +0 -26
- data/doc/rdoc/lore/rdoc-style.css +0 -177
- data/doc/release-notes/RELEASE-2.0.5 +0 -8
- data/doc/release-notes/RELEASE-2.1.0 +0 -9
- data/doc/release-notes/RELEASE-2.1.1 +0 -5
- data/doc/release-notes/RELEASE-2.1.2 +0 -6
- data/doc/release-notes/RELEASE-2.1.3 +0 -5
- data/doc/release-notes/RELEASE-2.2.0 +0 -14
- data/doc/release-notes/RELEASE-2.2.1 +0 -4
- data/doc/release-notes/RELEASE-2.3.0 +0 -6
- data/doc/release-notes/RELEASE-2.4.0 +0 -70
- data/doc/release-notes/RELEASE-2.4.1 +0 -8
- data/doc/release-notes/RELEASE-2.4.2 +0 -4
- data/doc/release-notes/RELEASE-2.4.3 +0 -78
- data/doc/release-notes/RELEASE-2.4.4 +0 -38
- data/doc/release-notes/RELEASE-2.4.5 +0 -37
- data/doc/release-notes/RELEASE-2.5.0 +0 -83
- data/lib/core/facets/kernel/instance.rb +0 -19
- data/lib/lore/facets/basicobject.rb +0 -14
- data/lib/lore/facets/enumerator.rb +0 -67
- data/lib/lore/facets/logger.rb +0 -291
- data/lib/more/facets/1stclassmethod.rb +0 -140
- data/lib/more/facets/advisable.rb +0 -162
- data/lib/more/facets/association.rb +0 -210
- data/lib/more/facets/attr.rb +0 -209
- data/lib/more/facets/basex.rb +0 -37
- data/lib/more/facets/bbcode.rb +0 -397
- data/lib/more/facets/bicrypt.rb +0 -265
- data/lib/more/facets/binreadable.rb +0 -221
- data/lib/more/facets/censor.rb +0 -97
- data/lib/more/facets/classmethods.rb +0 -199
- data/lib/more/facets/consoleutils.rb +0 -99
- data/lib/more/facets/crypt.rb +0 -166
- data/lib/more/facets/dependency.rb +0 -151
- data/lib/more/facets/downloader.rb +0 -281
- data/lib/more/facets/duplicable.rb +0 -43
- data/lib/more/facets/elementor.rb +0 -133
- data/lib/more/facets/filter.rb +0 -121
- data/lib/more/facets/heap.rb +0 -22
- data/lib/more/facets/infinity.rb +0 -193
- data/lib/more/facets/ini.rb +0 -264
- data/lib/more/facets/instantise.rb +0 -1
- data/lib/more/facets/instantize.rb +0 -95
- data/lib/more/facets/interval.rb +0 -282
- data/lib/more/facets/iteration.rb +0 -65
- data/lib/more/facets/linkedlist.rb +0 -222
- data/lib/more/facets/lrucache.rb +0 -157
- data/lib/more/facets/matcher.rb +0 -140
- data/lib/more/facets/memoizer.rb +0 -74
- data/lib/more/facets/minitar.rb +0 -1063
- data/lib/more/facets/nackclass.rb +0 -41
- data/lib/more/facets/net/smtp_tls.rb +0 -131
- data/lib/more/facets/nilstatus.rb +0 -48
- data/lib/more/facets/overload.rb +0 -94
- data/lib/more/facets/paramix.rb +0 -202
- data/lib/more/facets/pool.rb +0 -91
- data/lib/more/facets/pqueue.rb +0 -449
- data/lib/more/facets/pry.rb +0 -32
- data/lib/more/facets/reflection.rb +0 -145
- data/lib/more/facets/semaphore.rb +0 -92
- data/lib/more/facets/settings.rb +0 -248
- data/lib/more/facets/snapshot.rb +0 -209
- data/lib/more/facets/sparse_array.rb +0 -809
- data/lib/more/facets/string/mask.rb +0 -278
- data/lib/more/facets/string/obfuscate.rb +0 -65
- data/lib/more/facets/string/stylize.rb +0 -169
- data/lib/more/facets/string/words.rb +0 -167
- data/lib/more/facets/syncarray.rb +0 -114
- data/lib/more/facets/synchash.rb +0 -157
- data/lib/more/facets/typecast.rb +0 -261
- data/lib/more/facets/uninheritable.rb +0 -50
- data/lib/more/facets/xmlhash.rb +0 -112
- data/lib/more/facets/xoxo.rb +0 -259
- data/lib/more/facets/ziputils.rb +0 -490
- data/task/conflicts +0 -63
- data/task/coverage.rake +0 -37
- data/task/methods +0 -49
- data/task/rdoc.rake +0 -17
- data/task/setup.rake +0 -38
- data/task/test.rake +0 -145
- data/test/more/test_advisable.rb +0 -71
- data/test/more/test_association.rb +0 -38
- data/test/more/test_bbcode.rb +0 -21
- data/test/more/test_binreadable.rb +0 -50
- data/test/more/test_buildable.rb +0 -73
- data/test/more/test_classmethods.rb +0 -56
- data/test/more/test_crypt.rb +0 -32
- data/test/more/test_dependency.rb +0 -69
- data/test/more/test_elementwise.rb +0 -25
- data/test/more/test_infinity.rb +0 -40
- data/test/more/test_interval.rb +0 -151
- data/test/more/test_linkedlist.rb +0 -41
- data/test/more/test_lrucache.rb +0 -14
- data/test/more/test_overload.rb +0 -160
- data/test/more/test_paramix.rb +0 -170
- data/test/more/test_prototype.rb +0 -35
- data/test/more/test_snapshot.rb +0 -21
- data/test/more/test_sparsearray.rb +0 -279
- data/test/more/test_syncarray.rb +0 -15
- data/test/more/test_synchash.rb +0 -16
- data/test/more/test_typecast.rb +0 -54
- data/test/more/test_uninheritable.rb +0 -31
- data/test/more/test_xoxo.rb +0 -274
- data/test/test_facets.rb +0 -9
@@ -0,0 +1,34 @@
|
|
1
|
+
class Module
|
2
|
+
|
3
|
+
# Like attr_writer, but the writer method validates the
|
4
|
+
# setting against the given block.
|
5
|
+
#
|
6
|
+
# CREDIT: ?
|
7
|
+
|
8
|
+
def attr_validator(*symbols, &validator)
|
9
|
+
made = []
|
10
|
+
symbols.each do |symbol|
|
11
|
+
define_method "#{symbol}=" do |val|
|
12
|
+
unless validator.call(val)
|
13
|
+
raise ArgumentError, "Invalid value provided for #{symbol}"
|
14
|
+
end
|
15
|
+
instance_variable_set("@#{symbol}", val)
|
16
|
+
end
|
17
|
+
made << "#{symbol}=".to_sym
|
18
|
+
end
|
19
|
+
made
|
20
|
+
end
|
21
|
+
|
22
|
+
# Create aliases for validators.
|
23
|
+
|
24
|
+
def alias_validator(*args)
|
25
|
+
orig = args.last
|
26
|
+
args = args - [orig]
|
27
|
+
args.each do |name|
|
28
|
+
#alias_method(name, orig)
|
29
|
+
alias_method("#{name}=", "#{orig}=")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
@@ -11,7 +11,7 @@
|
|
11
11
|
# puts 'inst_meth'
|
12
12
|
# end
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# class_extend do
|
15
15
|
# def class_meth
|
16
16
|
# "Class Method!"
|
17
17
|
# end
|
@@ -53,8 +53,6 @@ class Module
|
|
53
53
|
|
54
54
|
alias_method :append_features_without_class_extension, :append_features
|
55
55
|
|
56
|
-
# = class_extension
|
57
|
-
#
|
58
56
|
# Normally when including modules, class/module methods are not
|
59
57
|
# extended. To achieve this behavior requires some clever
|
60
58
|
# Ruby Karate. Instead class_extension provides an easy to use
|
@@ -66,7 +64,7 @@ class Module
|
|
66
64
|
# puts 'inst_meth'
|
67
65
|
# end
|
68
66
|
#
|
69
|
-
#
|
67
|
+
# class_extend do
|
70
68
|
# def class_meth
|
71
69
|
# "Class Method!"
|
72
70
|
# end
|
@@ -79,35 +77,45 @@ class Module
|
|
79
77
|
#
|
80
78
|
# X.class_meth #=> "Class Method!"
|
81
79
|
#
|
82
|
-
|
83
|
-
|
80
|
+
# NOTE: This old #class_extension version of this method
|
81
|
+
# did not extend the containing class automatically --it had
|
82
|
+
# to be done by hand. With #class_extend, that is no longer
|
83
|
+
# the case.
|
84
|
+
#
|
85
|
+
def class_extend(*mods, &block)
|
84
86
|
@class_extension ||= Module.new do
|
85
87
|
def self.append_features(mod)
|
86
88
|
append_features_without_class_extension(mod)
|
87
89
|
end
|
88
90
|
end
|
91
|
+
@class_extension.__send__(:include, *mods)
|
89
92
|
@class_extension.module_eval(&block) if block_given?
|
93
|
+
extend(@class_extension) # extend this module too
|
90
94
|
@class_extension
|
91
95
|
end
|
92
96
|
|
93
|
-
|
97
|
+
# TODO: DEPRECATE
|
98
|
+
alias_method :class_extension, :class_extend
|
94
99
|
|
100
|
+
#private :class_extend
|
101
|
+
|
102
|
+
# Override +append_features+ to handle class-inheritable extensions.
|
95
103
|
def append_features(mod)
|
96
104
|
append_features_without_class_extension(mod)
|
97
|
-
mod.extend(
|
105
|
+
mod.extend(class_extend)
|
98
106
|
if mod.instance_of? Module
|
99
|
-
mod.__send__(:
|
107
|
+
mod.__send__(:class_extend).__send__(:include, class_extend)
|
100
108
|
end
|
101
109
|
end
|
102
110
|
|
103
111
|
end
|
104
112
|
|
105
113
|
class Class
|
106
|
-
# For
|
107
|
-
# The alternative is to "undef_method :
|
114
|
+
# For Class, #class_extend is the same as class_eval.
|
115
|
+
# The alternative is to "undef_method :class_extend",
|
108
116
|
# but this seems uneccessarily limited.
|
109
|
-
|
110
|
-
def
|
117
|
+
#
|
118
|
+
def class_extend(&blk)
|
111
119
|
class_eval(&blk)
|
112
120
|
end
|
113
121
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Kernel #:nodoc:
|
2
|
+
|
3
|
+
# Directive for making your functions faster by trading
|
4
|
+
# space for time. When you "memoize" a method/function
|
5
|
+
# using #once its results are cached so that later calls
|
6
|
+
# with the same arguments return results from the cache
|
7
|
+
# instead of recalculating them.
|
8
|
+
#
|
9
|
+
# class T
|
10
|
+
# def initialize(a)
|
11
|
+
# @a = a
|
12
|
+
# end
|
13
|
+
# def a
|
14
|
+
# "#{@a ^ 3 + 4}"
|
15
|
+
# end
|
16
|
+
# once :a
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# t = T.new
|
20
|
+
# t.a.__id__ == t.a.__id__ #=> true
|
21
|
+
#
|
22
|
+
# This method can also be used at the instance level
|
23
|
+
# to cache singleton/eigen methods.
|
24
|
+
#
|
25
|
+
# Note, this method used to be called +cache+ along with
|
26
|
+
# it's other alias #memoize, but +once+ is the term used
|
27
|
+
# in PickAxe so it has been adopted instead. The #memoize
|
28
|
+
# alias has also been retained.
|
29
|
+
#
|
30
|
+
# CREDIT Robert Feldt
|
31
|
+
#
|
32
|
+
def once(*ids)
|
33
|
+
if ids.empty?
|
34
|
+
@_once ||= Hash::new{|h,k| h[k]={}}
|
35
|
+
else
|
36
|
+
base = (Module === self ? self : (class << self; self; end))
|
37
|
+
ids.each do |m|
|
38
|
+
base.module_eval <<-code
|
39
|
+
alias_method '#{ m }:once', '#{ m }'
|
40
|
+
private '#{ m }:once'
|
41
|
+
def #{ m }(*__a__,&__b__)
|
42
|
+
c = once['#{ m }']
|
43
|
+
k = [__a__,__b__]
|
44
|
+
if c.has_key? k
|
45
|
+
c[k]
|
46
|
+
else
|
47
|
+
c[k] = __send__('#{ m }:once',*__a__,&__b__)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
code
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# +once+ is also widely known as +memoize+.
|
56
|
+
alias_method :memoize, :once
|
57
|
+
|
58
|
+
end
|
59
|
+
|
@@ -1,37 +1,5 @@
|
|
1
|
-
# = OrderedHash
|
2
|
-
#
|
3
|
-
# Another name for Dictionary.
|
4
|
-
#
|
5
|
-
# == Todo
|
6
|
-
#
|
7
|
-
# * Make this an alternative to Dictionary that
|
8
|
-
# is more "slim", not having the extra sorting
|
9
|
-
# features of dictionary.
|
10
|
-
#
|
11
|
-
# = Copying
|
12
|
-
#
|
13
|
-
# Copyright (c) 2005 Thomas Sawyer, George Moschovitis
|
14
|
-
#
|
15
|
-
# Ruby License
|
16
|
-
#
|
17
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
18
|
-
# software under the same terms as Ruby.
|
19
|
-
#
|
20
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
21
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
22
|
-
# FOR A PARTICULAR PURPOSE.
|
23
|
-
|
24
1
|
require 'facets/dictionary'
|
25
2
|
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# OrderedHash is an alias for the Dictionary class
|
29
|
-
|
3
|
+
# Ordered hash is the same as a Dictionary.
|
30
4
|
OrderedHash = Dictionary
|
31
5
|
|
32
|
-
# = AutoOrderedHash
|
33
|
-
#
|
34
|
-
# AutoOrderedHash is an alias for the AutoDictionary class
|
35
|
-
|
36
|
-
#AutoOrderedHash = AutoDictionary # was replaced by Dictionary.auto() (or autonew?)
|
37
|
-
|
File without changes
|
@@ -30,15 +30,13 @@
|
|
30
30
|
#
|
31
31
|
#++
|
32
32
|
|
33
|
-
#:title: OpenStructable
|
34
|
-
#
|
35
33
|
# OpensStructable is a mixin module which can provide OpenStruct behavior to
|
36
34
|
# any class or object. OpenStructable allows extention of data objects
|
37
35
|
# with arbitrary attributes.
|
38
36
|
#
|
39
37
|
# == Usage
|
40
38
|
#
|
41
|
-
# require '
|
39
|
+
# require 'ostructable'
|
42
40
|
#
|
43
41
|
# class Record
|
44
42
|
# include OpenStructable
|
@@ -58,7 +56,6 @@
|
|
58
56
|
# * Yukihiro Matsumoto
|
59
57
|
# * Gavin Sinclair (Documentation)
|
60
58
|
#
|
61
|
-
|
62
59
|
module OpenStructable
|
63
60
|
|
64
61
|
def initialize(hash=nil)
|
data/lib/more/facets/partial.rb
CHANGED
@@ -1,33 +1,23 @@
|
|
1
|
-
# MissingArgument exception class is used to represent an argument "hole".
|
2
|
-
|
3
|
-
class MissingArgument < ArgumentError
|
4
|
-
end
|
5
|
-
|
6
|
-
# Convenience method for an argument hole.
|
7
|
-
# It simple returns the MissingArgument exception class.
|
8
|
-
|
9
|
-
def __
|
10
|
-
MissingArgument
|
11
|
-
end
|
12
|
-
|
13
1
|
class Proc
|
14
2
|
|
15
3
|
# Convert a Proc object into new partial Proc object.
|
16
4
|
#
|
17
5
|
# a = proc { |a,b,c| a+b+c }
|
18
|
-
# b = a.partial(
|
6
|
+
# b = a.partial(X,2,X)
|
7
|
+
# b[1,3] #=> 6
|
8
|
+
#
|
9
|
+
# a = proc { |a,b,c| a+b+c }
|
10
|
+
# b = a.partial(__,2,__)
|
19
11
|
# b[1,3] #=> 6
|
20
12
|
#
|
21
13
|
# This method is similar to Proc#curry.
|
22
14
|
#
|
23
15
|
# CREDT Trans
|
24
|
-
#
|
25
|
-
# TODO: Parhaps ArgumentError would suffice, and we don't need MissingArgument?
|
26
16
|
|
27
17
|
def partial(*args)
|
28
18
|
Proc.new do |*spice|
|
29
19
|
result = args.collect do |a|
|
30
|
-
|
20
|
+
X == a ? spice.pop : a
|
31
21
|
end
|
32
22
|
call(*result)
|
33
23
|
end
|
@@ -35,3 +25,15 @@ class Proc
|
|
35
25
|
|
36
26
|
end
|
37
27
|
|
28
|
+
# X class is used to represent an argument "slot".
|
29
|
+
|
30
|
+
class X < ArgumentError
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convenience method for an argument slot.
|
34
|
+
# It simple returns the X exception class.
|
35
|
+
|
36
|
+
def __
|
37
|
+
X
|
38
|
+
end
|
39
|
+
|
File without changes
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# TITLE:
|
2
|
+
#
|
3
|
+
# Preinitialize
|
4
|
+
#
|
5
|
+
# DESCRIPTION:
|
6
|
+
#
|
7
|
+
# Presend a module to another module, or to a class.
|
8
|
+
#
|
9
|
+
# COPYRIGHT:
|
10
|
+
#
|
11
|
+
# Copyright (c) 2005 Thomas Sawyer
|
12
|
+
#
|
13
|
+
# LICENSE:
|
14
|
+
#
|
15
|
+
# Ruby License
|
16
|
+
#
|
17
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
18
|
+
# software under the same terms as Ruby.
|
19
|
+
#
|
20
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
21
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
22
|
+
# FOR A PARTICULAR PURPOSE.
|
23
|
+
#
|
24
|
+
# AUTHORS:
|
25
|
+
#
|
26
|
+
# - Thomas Sawyer
|
27
|
+
|
28
|
+
require 'facets/kernel/object' # for object_class
|
29
|
+
|
30
|
+
# = Preinitialize
|
31
|
+
#
|
32
|
+
# This is an object preinitialize system, which provides
|
33
|
+
# an elegant way to initialize an object allowing the
|
34
|
+
# class to provide additional default structure to an
|
35
|
+
# object prior to calling initialize.
|
36
|
+
#
|
37
|
+
# In effect it does two things after allocating the object
|
38
|
+
# but prior to initializing it.
|
39
|
+
#
|
40
|
+
# First, it calls the class method #default_instance_variables,
|
41
|
+
# which returns a hash and by default returns the hash
|
42
|
+
# stored in @default_instance_variables. It uses this to
|
43
|
+
# pre-define instance variables.
|
44
|
+
#
|
45
|
+
# Then it goes to the top of the class hierarchy and works
|
46
|
+
# it's way down calling #preinitialize if defined.
|
47
|
+
# WARNING! It is rather useless to use <tt>super</tt>
|
48
|
+
# inside the any preinitialize hook.
|
49
|
+
#
|
50
|
+
# module M
|
51
|
+
# def preinitialize
|
52
|
+
# @a = 23
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# class X
|
57
|
+
# include M
|
58
|
+
# def a ; @a ; end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# x = X.new
|
62
|
+
# x.a #=> 23
|
63
|
+
#
|
64
|
+
# If neded the original new method has been aliased, albeit
|
65
|
+
# <tt>postinitialize_new</tt> is probably a bit of a misnomer.
|
66
|
+
|
67
|
+
#--
|
68
|
+
# class Module
|
69
|
+
#
|
70
|
+
# def default_instance_variables(complete=false)
|
71
|
+
# @default_instance_variables ||= {}
|
72
|
+
# unless complete
|
73
|
+
# return @default_instance_variables
|
74
|
+
# else
|
75
|
+
# parent = ancestors[1]
|
76
|
+
# if parent
|
77
|
+
# return @default_instance_variables.merge(parent.default_instance_variables)
|
78
|
+
# else
|
79
|
+
# return @default_instance_variables
|
80
|
+
# end
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# end
|
85
|
+
#++
|
86
|
+
|
87
|
+
class Class
|
88
|
+
alias_method :postinitialize_new, :new
|
89
|
+
|
90
|
+
def new(*args, &blk)
|
91
|
+
o = allocate
|
92
|
+
|
93
|
+
#if respond_to?(:default_instance_variables)
|
94
|
+
# default_instance_variables.each{|k,v| o.instance_variable_set( "@#{k.to_s.gsub(/\W$/,'')}",v )}
|
95
|
+
#end
|
96
|
+
|
97
|
+
a = ancestors
|
98
|
+
until a.empty?
|
99
|
+
m = a.pop
|
100
|
+
#if m.private_instance_methods.include?('preinitialize') or m.public_instance_methods.include?('preinitialize')
|
101
|
+
if m.method_defined?('preinitialize') or m.private_method_defined?('preinitialize')
|
102
|
+
im = instance_method('preinitialize')
|
103
|
+
im.arity == 0 ? im.bind(o).call : im.bind(o).call(*args, &blk)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
o.__send__(:initialize, *args, &blk) if o.object_class.private_method_defined?(:initialize)
|
108
|
+
|
109
|
+
o
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
# _____ _
|
116
|
+
# |_ _|__ ___| |_
|
117
|
+
# | |/ _ \/ __| __|
|
118
|
+
# | | __/\__ \ |_
|
119
|
+
# |_|\___||___/\__|
|
120
|
+
#
|
121
|
+
|
122
|
+
=begin test
|
123
|
+
|
124
|
+
require 'test/unit'
|
125
|
+
|
126
|
+
class TC_Preinitalize < Test::Unit::TestCase
|
127
|
+
|
128
|
+
module M
|
129
|
+
def preinitialize
|
130
|
+
@a = 10
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class X
|
135
|
+
include M
|
136
|
+
def a ; @a ; end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Y < X
|
140
|
+
def initialize
|
141
|
+
super
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_01
|
146
|
+
x = X.new
|
147
|
+
assert_equal( 10, x.a )
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_01
|
151
|
+
y = Y.new
|
152
|
+
assert_equal( 10, y.a )
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
=end
|