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
data/lib/more/facets/snapshot.rb
DELETED
@@ -1,209 +0,0 @@
|
|
1
|
-
# = Snapshot
|
2
|
-
#
|
3
|
-
# A lightweight single-depth object state capture.
|
4
|
-
# The #take_snapshot method reads the object's state,
|
5
|
-
# which is generally it's collection of instance variables,
|
6
|
-
# and returns them in a hash. The state can be restored
|
7
|
-
# with #apply_snapshot.
|
8
|
-
#
|
9
|
-
# == Usage
|
10
|
-
#
|
11
|
-
# Customer = Struct.new("Customer", :name, :address, :zip)
|
12
|
-
# joe = Customer.new( "Joe Pitare", "1115 Lila Ln.", 47634 )
|
13
|
-
#
|
14
|
-
# # simple transactions
|
15
|
-
# joe_snap = joe.take_snapshot
|
16
|
-
# begin
|
17
|
-
# do_something_with( joe )
|
18
|
-
# rescue
|
19
|
-
# joe.apply_snapshot( joe_snap )
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# joe_snap[:name] => "Joe Pitare"
|
23
|
-
# joe_snap[:address] => "1115 Lila Ln."
|
24
|
-
# joe_snap[:zip] => 47634
|
25
|
-
#
|
26
|
-
# == Details
|
27
|
-
#
|
28
|
-
# Class Snapshot simply represents a collection of objects from
|
29
|
-
# which snapshots were taken via their methods #take_snapshot.
|
30
|
-
# It provides methods to add an object to a snapshot
|
31
|
-
# (Snapshot#add) as well as to restore all objects
|
32
|
-
# of the snapshot to their state stored in the snapshot (method
|
33
|
-
# Snapshot#restore).
|
34
|
-
#
|
35
|
-
# In Wee, this class is used to backtracking the state of
|
36
|
-
# components (or decorations/presenters). Components that want
|
37
|
-
# an undo-facility to be implemented (triggered for example by
|
38
|
-
# a browsers back-button), have to overwrite the
|
39
|
-
# Wee::Component#backtrack_state method.
|
40
|
-
#
|
41
|
-
# == Todo
|
42
|
-
#
|
43
|
-
# * Perhaps extend to offer multiple depths.
|
44
|
-
# * Should key consitancy be enforced? Currently
|
45
|
-
# Struct's will have symbol keys while other classes
|
46
|
-
# will have string keys in the form of "@name".
|
47
|
-
# * Add other core classes.
|
48
|
-
# * Convert to Kernel#to_data ?
|
49
|
-
# * I've been thinking about renaming this to State.
|
50
|
-
#
|
51
|
-
# == Authors
|
52
|
-
#
|
53
|
-
# * Michael Neumann
|
54
|
-
#
|
55
|
-
# == Copying
|
56
|
-
#
|
57
|
-
# Copyright (c) 2004 Michael Neumann
|
58
|
-
#
|
59
|
-
# Ruby License
|
60
|
-
#
|
61
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
62
|
-
# software under the same terms as Ruby.
|
63
|
-
#
|
64
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
65
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
66
|
-
# FOR A PARTICULAR PURPOSE.
|
67
|
-
|
68
|
-
# = Snapshot
|
69
|
-
#
|
70
|
-
# A lightweight single-depth object state capture.
|
71
|
-
# The #take_snapshot method reads the object's state,
|
72
|
-
# which is generally it's collection of instance variables,
|
73
|
-
# and returns them in a hash. The state can be restored
|
74
|
-
# with #apply_snapshot.
|
75
|
-
#
|
76
|
-
# == Usage
|
77
|
-
#
|
78
|
-
# Customer = Struct.new("Customer", :name, :address, :zip)
|
79
|
-
# joe = Customer.new( "Joe Pitare", "1115 Lila Ln.", 47634 )
|
80
|
-
#
|
81
|
-
# # simple transactions
|
82
|
-
# joe_snap = joe.take_snapshot
|
83
|
-
# begin
|
84
|
-
# do_something_with( joe )
|
85
|
-
# rescue
|
86
|
-
# joe.apply_snapshot( joe_snap )
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# joe_snap[:name] => "Joe Pitare"
|
90
|
-
# joe_snap[:address] => "1115 Lila Ln."
|
91
|
-
# joe_snap[:zip] => 47634
|
92
|
-
#
|
93
|
-
# == Details
|
94
|
-
#
|
95
|
-
# Class Snapshot simply represents a collection of objects from
|
96
|
-
# which snapshots were taken via their methods #take_snapshot.
|
97
|
-
# It provides methods to add an object to a snapshot
|
98
|
-
# (Snapshot#add) as well as to restore all objects
|
99
|
-
# of the snapshot to their state stored in the snapshot (method
|
100
|
-
# Snapshot#restore).
|
101
|
-
#
|
102
|
-
# In Wee, this class is used to backtracking the state of
|
103
|
-
# components (or decorations/presenters). Components that want
|
104
|
-
# an undo-facility to be implemented (triggered for example by
|
105
|
-
# a browsers back-button), have to overwrite the
|
106
|
-
# Wee::Component#backtrack_state method.
|
107
|
-
#
|
108
|
-
class Snapshot
|
109
|
-
def initialize
|
110
|
-
@objects = Hash.new
|
111
|
-
end
|
112
|
-
|
113
|
-
def add(object)
|
114
|
-
oid = object.object_id
|
115
|
-
@objects[oid] = [object, object.take_snapshot] unless @objects.include?(oid)
|
116
|
-
end
|
117
|
-
|
118
|
-
def restore
|
119
|
-
@objects.each_value do
|
120
|
-
|object, value| object.restore_snapshot(value)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# Simplist form of a snapshot.
|
126
|
-
#
|
127
|
-
#module SnapshotMixin
|
128
|
-
# def take_snapshot() dup end
|
129
|
-
# def restore_snapshot(snap) replace(snap) end
|
130
|
-
#end
|
131
|
-
|
132
|
-
# Implements a value holder. In Wee this is useful for
|
133
|
-
# backtracking the reference assigned to an instance variable
|
134
|
-
# (not the object itself!). An example where this is used is the
|
135
|
-
# <tt>@__decoration</tt> attribute of class Wee::Component.
|
136
|
-
|
137
|
-
class Snapshot::ValueHolder
|
138
|
-
attr_accessor :value
|
139
|
-
|
140
|
-
def initialize(value=nil)
|
141
|
-
@value = value
|
142
|
-
end
|
143
|
-
|
144
|
-
def take_snapshot
|
145
|
-
@value
|
146
|
-
end
|
147
|
-
|
148
|
-
def restore_snapshot(value)
|
149
|
-
@value = value
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
#--
|
154
|
-
# Extend some base classes of Ruby (Object, Array, String, Hash,
|
155
|
-
# Struct) for the two methods #take_snapshot and
|
156
|
-
# #restore_snapshot, required by Snapshot.
|
157
|
-
#++
|
158
|
-
|
159
|
-
class Object
|
160
|
-
def take_snapshot
|
161
|
-
snap = Hash.new
|
162
|
-
instance_variables.each do |iv|
|
163
|
-
snap[iv] = instance_variable_get(iv)
|
164
|
-
end
|
165
|
-
snap
|
166
|
-
end
|
167
|
-
|
168
|
-
def to_data
|
169
|
-
task_snapshot
|
170
|
-
end
|
171
|
-
|
172
|
-
def restore_snapshot(snap)
|
173
|
-
instance_variables.each do |iv|
|
174
|
-
instance_variable_set(iv, snap[iv])
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def from_data(snap)
|
179
|
-
restore_snapshot(snap)
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
class Array
|
184
|
-
def take_snapshot() dup end
|
185
|
-
def restore_snapshot(snap) replace(snap) end
|
186
|
-
end
|
187
|
-
|
188
|
-
class String
|
189
|
-
def take_snapshot() dup end
|
190
|
-
def restore_snapshot(snap) replace(snap) end
|
191
|
-
end
|
192
|
-
|
193
|
-
class Hash
|
194
|
-
def take_snapshot() dup end
|
195
|
-
def restore_snapshot(snap) replace(snap) end
|
196
|
-
end
|
197
|
-
|
198
|
-
class Struct
|
199
|
-
def take_snapshot
|
200
|
-
snap = Hash.new
|
201
|
-
each_pair {|k,v| snap[k] = v}
|
202
|
-
snap
|
203
|
-
end
|
204
|
-
|
205
|
-
def restore_snapshot(snap)
|
206
|
-
snap.each_pair {|k,v| send(k.to_s + "=", v)}
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
@@ -1,809 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# SparseArray
|
3
|
-
#
|
4
|
-
# Copyright (c) 2004,2005 Thomas Sawyer
|
5
|
-
#
|
6
|
-
# Ruby License
|
7
|
-
#
|
8
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
9
|
-
# software under the same terms as Ruby.
|
10
|
-
#
|
11
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
12
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13
|
-
# FOR A PARTICULAR PURPOSE.
|
14
|
-
#
|
15
|
-
# == AUTHOR(s)
|
16
|
-
#
|
17
|
-
# * Thamas Sawyer
|
18
|
-
#
|
19
|
-
# ==========================================================================
|
20
|
-
# Revision History ::
|
21
|
-
# --------------------------------------------------------------------------
|
22
|
-
# 04.09 Trans * 99% Compatible with Array
|
23
|
-
# ==========================================================================
|
24
|
-
#++
|
25
|
-
|
26
|
-
#:title: SparseArray
|
27
|
-
#
|
28
|
-
# SparseArray is an implemenation of the Array class using only Hashes.
|
29
|
-
# Regular Arrays are never used except once to delegate the #pack method,
|
30
|
-
# and for *args parameters (since there is no way around those is some cases).
|
31
|
-
# SparseArray is for all practical purposes 100% compatible with Array.
|
32
|
-
#
|
33
|
-
# SparseArray is slower then the built in Array class, but not as slow
|
34
|
-
# as one might expect, since a Hash in general is faster than an Array.
|
35
|
-
# It might be interesting to see how this would perform if it were
|
36
|
-
# written in c. Not all that useful, but an interesting example.
|
37
|
-
#
|
38
|
-
class SparseArray < Hash
|
39
|
-
|
40
|
-
def self.[](*args)
|
41
|
-
nha = new
|
42
|
-
args.each { |a| nha.set(nha.length,a) }
|
43
|
-
nha
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.new_h(hsh)
|
47
|
-
nha = new
|
48
|
-
nha.replace(hsh)
|
49
|
-
#nha.reindex!
|
50
|
-
end
|
51
|
-
|
52
|
-
def initialize(i=0,e=nil)
|
53
|
-
if i > 0
|
54
|
-
i.times { self.set(self.length,e) }
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def &(ha)
|
59
|
-
nha = self.class.new
|
60
|
-
(0..self.length-1).each do |i|
|
61
|
-
if ha.has_value?(self.fetch(i)) and !nha.has_value?(self.fetch(i))
|
62
|
-
nha.set(nha.length,self.fetch(i))
|
63
|
-
end
|
64
|
-
end
|
65
|
-
nha
|
66
|
-
end
|
67
|
-
|
68
|
-
def *(j)
|
69
|
-
if j.kind_of?(String)
|
70
|
-
return self.join(j)
|
71
|
-
else
|
72
|
-
nha = self.class.new
|
73
|
-
j.times { (0...self.length).each { |i| nha.set(nha.length,self.fetch(i)) } }
|
74
|
-
return nha
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def +(ha)
|
79
|
-
nha = self.dup
|
80
|
-
(0..ha.length-1).each { |i| nha.set(nha.length,ha.fetch(i)) }
|
81
|
-
nha
|
82
|
-
end
|
83
|
-
|
84
|
-
def -(ha)
|
85
|
-
nha = self.class.new
|
86
|
-
self.each { |v| nha << v if !ha.has_value?(v) }
|
87
|
-
#ha.each { |v| nha << i if !self.include?(v) }
|
88
|
-
nha
|
89
|
-
end
|
90
|
-
|
91
|
-
def <<(e)
|
92
|
-
self.set(self.length,e)
|
93
|
-
self
|
94
|
-
end
|
95
|
-
|
96
|
-
def <=>(ha)
|
97
|
-
(0..self.length-1).each do |i|
|
98
|
-
ieq = (self.fetch(i) <=> ha.fetch(i))
|
99
|
-
return ieq if ieq != 0
|
100
|
-
end
|
101
|
-
self.length <=> ha.length
|
102
|
-
end
|
103
|
-
|
104
|
-
def ===(ha)
|
105
|
-
self.==(ha)
|
106
|
-
end
|
107
|
-
|
108
|
-
alias_method :get, :[]
|
109
|
-
private :get
|
110
|
-
|
111
|
-
def [](i,l=nil)
|
112
|
-
if l
|
113
|
-
i = i...i+l
|
114
|
-
elsif ! i.kind_of?(Range)
|
115
|
-
return self.at(i)
|
116
|
-
end
|
117
|
-
nha = self.class.new
|
118
|
-
i.each { |j| nha.set(nha.length,get(j)) if has_key?(j) }
|
119
|
-
nha
|
120
|
-
end
|
121
|
-
|
122
|
-
alias set []=
|
123
|
-
def []=(i,b,c=nil)
|
124
|
-
if c
|
125
|
-
rng = (Integer(i)..Integer(i+b))
|
126
|
-
b = c
|
127
|
-
elsif i.kind_of? Range
|
128
|
-
rng = i
|
129
|
-
else
|
130
|
-
self.set(Integer(i),b)
|
131
|
-
return b
|
132
|
-
end
|
133
|
-
if b == nil
|
134
|
-
rng.each { |i| qdelete(i) }
|
135
|
-
self.reindex!
|
136
|
-
elsif b.kind_of?(Array) or b.kind_of?(self.class)
|
137
|
-
j = 0
|
138
|
-
rng.each { |i| self[i] = b[j]; j+=1 }
|
139
|
-
else
|
140
|
-
rng.each { |i| qdelete(i) }
|
141
|
-
self[rng.fist] = b
|
142
|
-
self.reindex!
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def |(ha)
|
147
|
-
nha = self.dup
|
148
|
-
ha.each { |v| nha << v if !nha.has_value?(v) }
|
149
|
-
nha
|
150
|
-
end
|
151
|
-
|
152
|
-
def assoc(k)
|
153
|
-
(0...self.length).each { |i| return self.fetch(i) if self.fetch(i)[0] == k }
|
154
|
-
return nil
|
155
|
-
end
|
156
|
-
|
157
|
-
def at(i)
|
158
|
-
i = self.length + i if i <= -1
|
159
|
-
get(i)
|
160
|
-
#return nil if i < 0 or i >= self.length
|
161
|
-
#return self.fetch(i)
|
162
|
-
end
|
163
|
-
|
164
|
-
# clear okay
|
165
|
-
|
166
|
-
def collect
|
167
|
-
nha = self.class.new
|
168
|
-
(0...self.length).each { |i| nha << yield(self.fetch(i)) }
|
169
|
-
nha
|
170
|
-
end
|
171
|
-
|
172
|
-
def collect!
|
173
|
-
nha = self.class.new
|
174
|
-
(0...self.length).each { |i| nha << yield(self.fetch(i)) }
|
175
|
-
self.replace(nha)
|
176
|
-
end
|
177
|
-
|
178
|
-
def compact
|
179
|
-
nha, j = self.class.new, 0
|
180
|
-
(0..self.length-1).each do |i|
|
181
|
-
if self.fetch(i) != nil
|
182
|
-
nha.set(j,self.fetch(i))
|
183
|
-
j+=1
|
184
|
-
end
|
185
|
-
end
|
186
|
-
nha
|
187
|
-
end
|
188
|
-
|
189
|
-
def compact!
|
190
|
-
if self.has_value?(nil)
|
191
|
-
nha, j = self.class.new, 0
|
192
|
-
(0..self.length-1).each do |i|
|
193
|
-
if self.fetch(i) != nil
|
194
|
-
nha.set(j,self.fetch(i))
|
195
|
-
j+=1
|
196
|
-
end
|
197
|
-
end
|
198
|
-
return self.replace(nha)
|
199
|
-
else
|
200
|
-
return nil
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
def concat(ha)
|
205
|
-
(0...ha.length).each { |i| self.set(self.length,ha.fetch(i)) }
|
206
|
-
self
|
207
|
-
end
|
208
|
-
|
209
|
-
def count(e=nil)
|
210
|
-
if block_given?
|
211
|
-
cnt = 0
|
212
|
-
(0...self.length).each { |i| cnt += 1 if yield(self.fetch(i)) }
|
213
|
-
return cnt
|
214
|
-
else
|
215
|
-
cnt = 0
|
216
|
-
(0...self.length).each { |i| cnt += 1 if self.fetch(i) == e }
|
217
|
-
return cnt
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
alias qdelete delete
|
222
|
-
private :qdelete
|
223
|
-
|
224
|
-
def delete(e)
|
225
|
-
if has_value?(e)
|
226
|
-
qdelete_if { |i,v| v == e }
|
227
|
-
reindex!
|
228
|
-
return e
|
229
|
-
else
|
230
|
-
return yield if block_given?
|
231
|
-
return nil
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
def delete_at(i)
|
236
|
-
if self.has_key?(i)
|
237
|
-
e = self.fetch(i)
|
238
|
-
qdelete(i)
|
239
|
-
reindex!
|
240
|
-
return e
|
241
|
-
else
|
242
|
-
return nil
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
alias qdelete_if delete_if
|
247
|
-
private :qdelete_if
|
248
|
-
|
249
|
-
def delete_if
|
250
|
-
qdelete_if { |i,v| yield(v) }
|
251
|
-
reindex!
|
252
|
-
end
|
253
|
-
|
254
|
-
def each
|
255
|
-
(0...self.length).each { |i| yield(self.fetch(i)) }
|
256
|
-
end
|
257
|
-
|
258
|
-
def each_index
|
259
|
-
(0...self.length).each { |i| yield(i) }
|
260
|
-
end
|
261
|
-
|
262
|
-
# empty? okay as is
|
263
|
-
|
264
|
-
def eql?(ha)
|
265
|
-
return false if self.length != ha.length
|
266
|
-
return true if (0...self.length).all? { |i| self.fetch(i).eql?(ha.fetch(i)) }
|
267
|
-
return false
|
268
|
-
end
|
269
|
-
|
270
|
-
def fill(f,s=nil,l=nil)
|
271
|
-
if s.kind_of?(Range)
|
272
|
-
r = s
|
273
|
-
else
|
274
|
-
s = 0 if !s
|
275
|
-
l = self.length - s if !l
|
276
|
-
r = s...(s+l)
|
277
|
-
end
|
278
|
-
r.each{ |i| self.set(i,f) }
|
279
|
-
self
|
280
|
-
end
|
281
|
-
|
282
|
-
def first
|
283
|
-
return nil if self.empty?
|
284
|
-
self.fetch(0)
|
285
|
-
end
|
286
|
-
|
287
|
-
def flatten
|
288
|
-
nha = self.class.new
|
289
|
-
(0...self.length).each do |i|
|
290
|
-
sfi = self.fetch(i)
|
291
|
-
if sfi.kind_of?(self.class) or sfi.kind_of?(Array)
|
292
|
-
nha.concat(sfi.flatten)
|
293
|
-
else
|
294
|
-
nha.set(nha.length,sfi)
|
295
|
-
end
|
296
|
-
end
|
297
|
-
nha
|
298
|
-
end
|
299
|
-
|
300
|
-
def flatten!
|
301
|
-
return nil if !self.any? { |e| e.kind_of?(self.class) or e.kind_of?(Array) }
|
302
|
-
self.replace(self.flatten)
|
303
|
-
end
|
304
|
-
|
305
|
-
def include?(v)
|
306
|
-
self.has_value?(v)
|
307
|
-
end
|
308
|
-
|
309
|
-
# index okay
|
310
|
-
|
311
|
-
def join(sep='')
|
312
|
-
s = ''
|
313
|
-
(0...self.length).each { |i| s << "#{self.fetch(i)}#{sep}" }
|
314
|
-
return s.chomp(sep)
|
315
|
-
end
|
316
|
-
|
317
|
-
def last
|
318
|
-
self[self.length-1]
|
319
|
-
end
|
320
|
-
|
321
|
-
# length okay
|
322
|
-
|
323
|
-
alias map! collect!
|
324
|
-
|
325
|
-
def nitems
|
326
|
-
cnt = 0
|
327
|
-
(0...self.length).each { |i| cnt += 1 if self.fetch(i) != nil }
|
328
|
-
cnt
|
329
|
-
end
|
330
|
-
|
331
|
-
def pack(*args)
|
332
|
-
self.to_a.pack(*args)
|
333
|
-
end
|
334
|
-
|
335
|
-
def pop
|
336
|
-
self.delete_at(self.length-1)
|
337
|
-
end
|
338
|
-
|
339
|
-
def push(*e)
|
340
|
-
self.concat(e)
|
341
|
-
end
|
342
|
-
|
343
|
-
def rassoc(k)
|
344
|
-
(0...self.length).each { |i| return self.fetch(i) if self.fetch(i)[1] == k }
|
345
|
-
return nil
|
346
|
-
end
|
347
|
-
|
348
|
-
def reindex
|
349
|
-
nha, j, k, tl = self.class.new, 0, 0, self.length
|
350
|
-
while k < tl
|
351
|
-
if self.has_key?(j)
|
352
|
-
nha.set(k,self.fetch(j))
|
353
|
-
j+=1; k+=1
|
354
|
-
else
|
355
|
-
j+=1
|
356
|
-
end
|
357
|
-
end
|
358
|
-
nha
|
359
|
-
end
|
360
|
-
|
361
|
-
def reindex!
|
362
|
-
self.replace(self.reindex)
|
363
|
-
end
|
364
|
-
|
365
|
-
def reject!
|
366
|
-
chg=nil
|
367
|
-
qdelete_if { |i,v| r=yield(v); chg=true if r; r }
|
368
|
-
return nil if !chg
|
369
|
-
reindex!
|
370
|
-
end
|
371
|
-
|
372
|
-
#def replace(ha)
|
373
|
-
# if ha.length < self.length
|
374
|
-
# (ha.length..self.length-1).each { |i| self.delete(i) }
|
375
|
-
# (0..ha.length-1).each { |i| self.set(i,ha[i]) }
|
376
|
-
# end
|
377
|
-
#end
|
378
|
-
|
379
|
-
def reverse
|
380
|
-
nha = self.class.new
|
381
|
-
(0...self.length).each { |i| nha.set(self.length-1-i,self.fetch(i)) }
|
382
|
-
nha
|
383
|
-
end
|
384
|
-
|
385
|
-
def reverse!
|
386
|
-
(0...self.length/2).each do |i|
|
387
|
-
ri = self.length-1-i
|
388
|
-
tmp = self.fetch(ri)
|
389
|
-
self.set(ri,self.fetch(i))
|
390
|
-
self.set(i,tmp)
|
391
|
-
end
|
392
|
-
self
|
393
|
-
end
|
394
|
-
|
395
|
-
def reverse_each
|
396
|
-
i = self.length - 1
|
397
|
-
while i >= 0
|
398
|
-
yield(self.fetch(i))
|
399
|
-
i -= 1
|
400
|
-
end
|
401
|
-
end
|
402
|
-
|
403
|
-
def rindex(e)
|
404
|
-
i = self.length - 1
|
405
|
-
while i >= 0
|
406
|
-
return i if self.fetch(i) == e
|
407
|
-
i -= 1
|
408
|
-
end
|
409
|
-
return nil
|
410
|
-
end
|
411
|
-
|
412
|
-
def shift
|
413
|
-
e1 = self[0]
|
414
|
-
tl = self.length - 1
|
415
|
-
(1..tl).each { |i| self.set(i-1,self.fetch(i)) }
|
416
|
-
self.delete_at(tl)
|
417
|
-
e1
|
418
|
-
end
|
419
|
-
|
420
|
-
# size okay
|
421
|
-
|
422
|
-
def slice(*args)
|
423
|
-
self[*args]
|
424
|
-
end
|
425
|
-
|
426
|
-
def slice!(*args)
|
427
|
-
result = self[*args]
|
428
|
-
self[*args] = nil
|
429
|
-
result
|
430
|
-
end
|
431
|
-
|
432
|
-
def sort
|
433
|
-
raise "SparseArray does not currently support sorting with blocks" if block_given?
|
434
|
-
nha = self.dup
|
435
|
-
qsort(nha,0,nha.length-1)
|
436
|
-
end
|
437
|
-
|
438
|
-
def qsort(ha, l, r)
|
439
|
-
l_hold = l
|
440
|
-
r_hold = r
|
441
|
-
pivot = ha[l]
|
442
|
-
while l < r
|
443
|
-
r -= 1 while (ha[r] <=> pivot) >= 0 and l < r
|
444
|
-
if l != r
|
445
|
-
ha[l] = ha[r]
|
446
|
-
l += 1
|
447
|
-
end
|
448
|
-
l += 1 while (ha[l] <=> pivot) <= 0 and l < r
|
449
|
-
if l != r
|
450
|
-
ha[r] = ha[l]
|
451
|
-
r -= 1
|
452
|
-
end
|
453
|
-
end
|
454
|
-
ha[l] = pivot
|
455
|
-
pivot = l
|
456
|
-
l = l_hold
|
457
|
-
r = r_hold
|
458
|
-
qsort(ha,l,pivot-1) if l < pivot
|
459
|
-
qsort(ha,pivot+1,r) if r > pivot
|
460
|
-
ha
|
461
|
-
end
|
462
|
-
|
463
|
-
def sort!
|
464
|
-
raise "SparseArray does not currently support sorting with blocks" if block_given?
|
465
|
-
qsort(self,0,self.length-1)
|
466
|
-
end
|
467
|
-
|
468
|
-
def to_a
|
469
|
-
a = []
|
470
|
-
(0..self.length-1).each { |i| a << self.fetch(i) }
|
471
|
-
a
|
472
|
-
end
|
473
|
-
|
474
|
-
def to_ary
|
475
|
-
self
|
476
|
-
end
|
477
|
-
|
478
|
-
def to_h
|
479
|
-
h = Hash.new
|
480
|
-
self.each { |k,v| h[k] = v }
|
481
|
-
h
|
482
|
-
end
|
483
|
-
|
484
|
-
def to_s
|
485
|
-
self.join
|
486
|
-
end
|
487
|
-
|
488
|
-
def uniq
|
489
|
-
nha = self.class.new
|
490
|
-
(0..self.length-1).each do |i|
|
491
|
-
nha[nha.length] = self[i] if !nha.has_value?(self[i])
|
492
|
-
end
|
493
|
-
nha
|
494
|
-
end
|
495
|
-
|
496
|
-
def uniq!
|
497
|
-
j = 0
|
498
|
-
(1..self.length-1).each do |i|
|
499
|
-
if !self[0..j].has_value?(self[i])
|
500
|
-
self[j+1] = self[i]
|
501
|
-
j+=1
|
502
|
-
end
|
503
|
-
end
|
504
|
-
(j+1..self.length-1).each { |i| qdelete(i) }
|
505
|
-
end
|
506
|
-
|
507
|
-
def unshift(e)
|
508
|
-
i = self.length - 1
|
509
|
-
while i >= 0
|
510
|
-
self.set(i+1,self.fetch(i))
|
511
|
-
return i if self.fetch(i) == e
|
512
|
-
i -= 1
|
513
|
-
end
|
514
|
-
self.set(0,e)
|
515
|
-
self
|
516
|
-
end
|
517
|
-
|
518
|
-
def values_at(*ix)
|
519
|
-
nha = self.class.new
|
520
|
-
ix.each {|i| nha[nha.length] = self.at(i)}
|
521
|
-
nha
|
522
|
-
end
|
523
|
-
|
524
|
-
end
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
=begin test
|
529
|
-
|
530
|
-
# This is a mostly complete testcase for SparseArray.
|
531
|
-
# SparseArray is tested by comparison to standard Array.
|
532
|
-
|
533
|
-
require 'test/unit'
|
534
|
-
|
535
|
-
class TC_SparseArray < Test::Unit::TestCase
|
536
|
-
|
537
|
-
def aha(a)
|
538
|
-
return a, SparseArray[*a]
|
539
|
-
end
|
540
|
-
|
541
|
-
def test_conversions
|
542
|
-
a, ha = aha [1,3,'a',8,nil,[1]]
|
543
|
-
assert_equal(ha, ha.to_ary) # these need to be opposite
|
544
|
-
assert_equal(a, ha.to_a) #
|
545
|
-
assert_equal(a.to_s, ha.to_s)
|
546
|
-
end
|
547
|
-
|
548
|
-
def test_set_operations
|
549
|
-
a, ha = aha [1,3,5,8,9]
|
550
|
-
b, hb = aha [2,3,6,8,9]
|
551
|
-
assert_equal(a | b, (ha | hb).to_a)
|
552
|
-
assert_equal(a & b, (ha & hb).to_a)
|
553
|
-
assert_equal(a + b, (ha + hb).to_a)
|
554
|
-
assert_equal(a - b, (ha - hb).to_a)
|
555
|
-
end
|
556
|
-
|
557
|
-
def test_multiple
|
558
|
-
a, ha = aha [1,3]
|
559
|
-
assert_equal(a*3,(ha*3).to_a)
|
560
|
-
end
|
561
|
-
|
562
|
-
def test_assign
|
563
|
-
a, ha = aha [1,2,3,4]
|
564
|
-
a[1..2] = [8,9]
|
565
|
-
ha[1..2] = [8,9]
|
566
|
-
assert_equal(a, ha.to_a)
|
567
|
-
end
|
568
|
-
def test_assoc
|
569
|
-
a, ha = aha [[1,2],[3,4],[3,6]]
|
570
|
-
assert_equal(a.assoc(3), ha.assoc(3).to_a)
|
571
|
-
end
|
572
|
-
def test_at
|
573
|
-
a, ha = aha [4,5,6,6]
|
574
|
-
assert_equal(a.at(0), ha.at(0))
|
575
|
-
assert_equal(a.at(2), ha.at(2))
|
576
|
-
assert_equal(a.at(4), ha.at(4))
|
577
|
-
assert_equal(a.at(9), ha.at(9))
|
578
|
-
assert_equal(a.at(-1), ha.at(-1))
|
579
|
-
assert_equal(a.at(-3), ha.at(-3))
|
580
|
-
assert_equal(a.at(-4), ha.at(-4))
|
581
|
-
assert_equal(a.at(-5), ha.at(-5))
|
582
|
-
end
|
583
|
-
def test_collect
|
584
|
-
a, ha = aha [4,5,6,6]
|
585
|
-
assert_equal(a.collect{|e|e}, ha.collect{|e|e}.to_a)
|
586
|
-
assert_equal(a.collect!{|e|e}, ha.collect!{|e|e}.to_a)
|
587
|
-
assert_equal(a,ha.to_a)
|
588
|
-
end
|
589
|
-
def test_compact
|
590
|
-
a, ha = aha [4,nil,5,nil,6]
|
591
|
-
assert_equal(a.compact, ha.compact.to_a)
|
592
|
-
end
|
593
|
-
def test_concat
|
594
|
-
a, ha = aha [1,3,5,8,9]
|
595
|
-
b, hb = aha [2,3,6,8,9]
|
596
|
-
assert_equal(a.concat(b),ha.concat(hb).to_a)
|
597
|
-
end
|
598
|
-
def test_count
|
599
|
-
ha = SparseArray[9,3,9,5,nil,nil,9,3]
|
600
|
-
assert_equal(2,ha.count)
|
601
|
-
assert_equal(2,ha.count(3))
|
602
|
-
assert_equal(3,ha.count{|e|e==9})
|
603
|
-
end
|
604
|
-
def test_delete
|
605
|
-
a, ha = aha [1,3,5,8,9,'a','b','c','c','d']
|
606
|
-
# test delete
|
607
|
-
assert_equal(a.delete(1),ha.delete(1))
|
608
|
-
assert_equal(a,ha.to_a)
|
609
|
-
assert_equal(a.delete('a'),ha.delete('a'))
|
610
|
-
assert_equal(a,ha.to_a)
|
611
|
-
# test delete_at
|
612
|
-
assert_equal(a.delete_at(0),ha.delete_at(0))
|
613
|
-
assert_equal(a,ha.to_a)
|
614
|
-
# test delete_if
|
615
|
-
assert_equal(a.delete_if{|v|v=='c'},ha.delete_if{|v|v=='c'}.to_a)
|
616
|
-
assert_equal(a,ha.to_a)
|
617
|
-
end
|
618
|
-
def test_each
|
619
|
-
a, ha = aha [4,'a',nil,'b']
|
620
|
-
# test each
|
621
|
-
ca, cha = '', ''
|
622
|
-
a.each{|e| ca += e.to_s}
|
623
|
-
ha.each{|e| cha += e.to_s}
|
624
|
-
assert_equal(ca,cha)
|
625
|
-
assert_equal(a,ha.to_a)
|
626
|
-
# test each_index
|
627
|
-
ca, cha = '', ''
|
628
|
-
a.each_index{|i| ca += i.to_s}
|
629
|
-
ha.each_index{|i| cha += i.to_s}
|
630
|
-
assert_equal(ca,cha)
|
631
|
-
assert_equal(a,ha.to_a)
|
632
|
-
end
|
633
|
-
def test_eql?
|
634
|
-
a, ha = aha [4,'a',nil,'b']
|
635
|
-
b, hb = aha [4,'a',nil,'b']
|
636
|
-
assert_equal(a,b)
|
637
|
-
assert_equal(ha,hb)
|
638
|
-
assert_equal(a.eql?(b),ha.eql?(hb))
|
639
|
-
assert_equal(b.eql?(a),hb.eql?(ha))
|
640
|
-
assert(ha.eql?(hb))
|
641
|
-
assert(hb.eql?(ha))
|
642
|
-
end
|
643
|
-
def test_empty?
|
644
|
-
a, ha = aha []
|
645
|
-
assert_equal(a.empty?,ha.empty?)
|
646
|
-
a, ha = aha [1,2,3]
|
647
|
-
assert_equal(a.empty?,ha.empty?)
|
648
|
-
end
|
649
|
-
def test_fill
|
650
|
-
a, ha = aha ['a','b','c','d']
|
651
|
-
assert_equal(a.fill('x'),ha.fill('x').to_a)
|
652
|
-
assert_equal(a,ha.to_a)
|
653
|
-
assert_equal(a.fill('y',2,2),ha.fill('y',2,2).to_a)
|
654
|
-
assert_equal(a,ha.to_a)
|
655
|
-
assert_equal(a.fill('z',0..1),ha.fill('z',0..1).to_a)
|
656
|
-
assert_equal(a,ha.to_a)
|
657
|
-
end
|
658
|
-
def test_first
|
659
|
-
a, ha = aha [2,3,4]
|
660
|
-
assert_equal(a.first,ha.first)
|
661
|
-
end
|
662
|
-
def test_flatten
|
663
|
-
a, ha = aha [2,[3],'a',[[1,2],4],nil,5]
|
664
|
-
assert_equal(a.flatten,ha.flatten.to_a)
|
665
|
-
a, ha = aha [2,[3],'a',[[1,2],4],nil,5]
|
666
|
-
assert_equal(a.flatten!,ha.flatten!.to_a)
|
667
|
-
assert_equal(a,ha.to_a)
|
668
|
-
a, ha = aha [2,3,'a',nil,5]
|
669
|
-
assert_equal(a.flatten!,ha.flatten!)
|
670
|
-
end
|
671
|
-
def test_include?
|
672
|
-
a, ha = aha ['a','b','c','d']
|
673
|
-
assert_equal(a.include?('b'),ha.include?('b'))
|
674
|
-
assert_equal(a.include?('x'),ha.include?('x'))
|
675
|
-
end
|
676
|
-
def test_index
|
677
|
-
a, ha = aha ['a','b','b','c','d']
|
678
|
-
assert_equal(a.index('b'),ha.index('b'))
|
679
|
-
assert_equal(a.index('x'),ha.index('x'))
|
680
|
-
end
|
681
|
-
def test_join
|
682
|
-
a, ha = aha [2,3,4]
|
683
|
-
assert_equal(a.join,ha.join)
|
684
|
-
assert_equal(a.join(','),ha.join(','))
|
685
|
-
end
|
686
|
-
def test_last
|
687
|
-
a, ha = aha [2,3,4]
|
688
|
-
assert_equal(a.last,ha.last)
|
689
|
-
end
|
690
|
-
def test_length
|
691
|
-
a, ha = aha [2,3,4]
|
692
|
-
assert_equal(a.length,ha.length)
|
693
|
-
end
|
694
|
-
def test_map!
|
695
|
-
a, ha = aha [4,5,6,6]
|
696
|
-
assert_equal(a.map!{|e|e}, ha.map!{|e|e}.to_a)
|
697
|
-
assert_equal(a,ha.to_a)
|
698
|
-
end
|
699
|
-
def test_nitems
|
700
|
-
a, ha = aha [4,5,nil,6,nil]
|
701
|
-
assert_equal(a.nitems, ha.nitems)
|
702
|
-
end
|
703
|
-
def test_pop
|
704
|
-
a, ha = aha [4,5,nil,6,nil]
|
705
|
-
assert_equal(a.pop, ha.pop)
|
706
|
-
assert_equal(a, ha.to_a)
|
707
|
-
assert_equal(a.pop, ha.pop)
|
708
|
-
assert_equal(a, ha.to_a)
|
709
|
-
end
|
710
|
-
def test_push
|
711
|
-
a, ha = aha [4,5,nil,6,nil]
|
712
|
-
args = [1,2,3]
|
713
|
-
assert_equal(a.push(*args), ha.push(*args).to_a)
|
714
|
-
assert_equal(a, ha.to_a)
|
715
|
-
end
|
716
|
-
def test_rassoc
|
717
|
-
a, ha = aha [[1,2],[1,3],[1,3]]
|
718
|
-
assert_equal(a.rassoc(3), ha.rassoc(3).to_a)
|
719
|
-
end
|
720
|
-
def test_reject!
|
721
|
-
a, ha = aha ['a','b','c','c','d']
|
722
|
-
assert_equal(a.reject!{|v|v=='c'},ha.reject!{|v|v=='c'}.to_a)
|
723
|
-
assert_equal(a,ha.to_a)
|
724
|
-
assert_equal(a.reject!{|v|v=='x'},ha.reject!{|v|v=='x'})
|
725
|
-
assert_equal(a,ha.to_a)
|
726
|
-
end
|
727
|
-
def test_reverse
|
728
|
-
a, ha = aha ['a','b','c','c','d']
|
729
|
-
assert_equal(a.reverse,ha.reverse.to_a)
|
730
|
-
assert_equal(a.reverse!,ha.reverse!.to_a)
|
731
|
-
assert_equal(a,ha.to_a)
|
732
|
-
a, ha = aha [1,2,3,'a','b','c']
|
733
|
-
assert_equal(a.reverse!,ha.reverse!.to_a)
|
734
|
-
assert_equal(a,ha.to_a)
|
735
|
-
end
|
736
|
-
def test_reverse_each
|
737
|
-
a, ha = aha [4,'a',nil,'b']
|
738
|
-
# test each
|
739
|
-
ca, cha = '', ''
|
740
|
-
a.reverse_each{|e| ca += e.to_s}
|
741
|
-
ha.reverse_each{|e| cha += e.to_s}
|
742
|
-
assert_equal(ca,cha)
|
743
|
-
assert_equal(a,ha.to_a)
|
744
|
-
end
|
745
|
-
def test_rindex
|
746
|
-
a, ha = aha ['a','b','c','c','d']
|
747
|
-
assert_equal(a.rindex('c'),ha.rindex('c'))
|
748
|
-
assert_equal(a.rindex('x'),ha.rindex('x'))
|
749
|
-
end
|
750
|
-
def test_shift
|
751
|
-
a, ha = aha ['a','b','c','c','d']
|
752
|
-
assert_equal(a.shift,ha.shift)
|
753
|
-
assert_equal(a,ha.to_a)
|
754
|
-
end
|
755
|
-
|
756
|
-
def test_slice
|
757
|
-
a, ha = aha [1,2,3,4]
|
758
|
-
# test []
|
759
|
-
assert_equal(a[1], ha[1])
|
760
|
-
assert_equal(a[1..2], ha[1..2].to_a)
|
761
|
-
assert_equal(a[1...2], ha[1...2].to_a)
|
762
|
-
assert_equal(a[1..7], ha[1..7].to_a)
|
763
|
-
assert_equal(a[1,2], ha[1,2].to_a)
|
764
|
-
# test slice
|
765
|
-
assert_equal(a.slice(1), ha.slice(1))
|
766
|
-
assert_equal(a.slice(1..2), ha.slice(1..2).to_a)
|
767
|
-
assert_equal(a.slice(1...2), ha.slice(1...2).to_a)
|
768
|
-
assert_equal(a.slice(1...2), ha.slice(1...2).to_a)
|
769
|
-
assert_equal(a.slice(1,2), ha.slice(1,2).to_a)
|
770
|
-
# test slice!
|
771
|
-
assert_equal(a.slice!(1..2), ha.slice!(1..2).to_a)
|
772
|
-
assert_equal(a, ha.to_a)
|
773
|
-
end
|
774
|
-
|
775
|
-
def test_sort
|
776
|
-
a, ha = aha [1,2,3,4]
|
777
|
-
# test sort
|
778
|
-
assert_equal(a.sort, ha.sort.to_a)
|
779
|
-
#assert_equal(a.sort{|x,y| y<=>x}, ha.sort{|x,y| y<=>x}.to_a)
|
780
|
-
# test sort!
|
781
|
-
assert_equal(a.sort!, ha.sort!.to_a)
|
782
|
-
assert_equal(a, ha.to_a)
|
783
|
-
end
|
784
|
-
|
785
|
-
def test_uniq
|
786
|
-
a, ha = aha [1,1,2,3,3,4,5,6,6]
|
787
|
-
assert_equal(a.uniq, ha.uniq.to_a)
|
788
|
-
end
|
789
|
-
|
790
|
-
def test_uniq!
|
791
|
-
a, ha = aha [1,1,2,3,3,4,5,6,6]
|
792
|
-
a.uniq!; ha.uniq!
|
793
|
-
assert_equal(a, ha.to_a)
|
794
|
-
end
|
795
|
-
|
796
|
-
def test_values_at
|
797
|
-
a, ha = aha ['a','b','c','d']
|
798
|
-
assert_equal(a.values_at(1,3),ha.values_at(1,3).to_a)
|
799
|
-
end
|
800
|
-
|
801
|
-
def test_unshift
|
802
|
-
a, ha = aha ['a','b','c','c','d']
|
803
|
-
assert_equal(a.unshift('x'),ha.unshift('x').to_a)
|
804
|
-
assert_equal(a,ha.to_a)
|
805
|
-
end
|
806
|
-
end
|
807
|
-
|
808
|
-
=end
|
809
|
-
|