hashery 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.ruby +30 -17
- data/.yardopts +1 -0
- data/Config.rb +28 -0
- data/{QED.rdoc → DEMO.rdoc} +0 -0
- data/HISTORY.rdoc +37 -0
- data/LICENSE.txt +26 -0
- data/NOTICE.txt +46 -0
- data/README.rdoc +10 -7
- data/lib/hashery.rb +6 -6
- data/lib/hashery.yml +30 -17
- data/lib/hashery/association.rb +169 -109
- data/lib/hashery/casting_hash.rb +128 -135
- data/lib/hashery/core_ext.rb +89 -61
- data/lib/hashery/crud_hash.rb +365 -0
- data/lib/hashery/dictionary.rb +545 -345
- data/lib/hashery/fuzzy_hash.rb +177 -125
- data/lib/hashery/ini_hash.rb +321 -0
- data/lib/hashery/key_hash.rb +54 -179
- data/lib/hashery/linked_list.rb +245 -191
- data/lib/hashery/lru_hash.rb +292 -202
- data/lib/hashery/open_cascade.rb +133 -78
- data/lib/hashery/open_hash.rb +127 -61
- data/lib/hashery/ordered_hash.rb +128 -122
- data/lib/hashery/path_hash.rb +238 -0
- data/lib/hashery/property_hash.rb +144 -80
- data/lib/hashery/query_hash.rb +85 -29
- data/lib/hashery/stash.rb +7 -3
- data/lib/hashery/static_hash.rb +46 -41
- data/test/case_association.rb +65 -4
- data/test/case_dictionary.rb +149 -5
- data/test/{case_keyhash.rb → case_key_hash.rb} +20 -14
- data/test/case_lru_hash.rb +162 -0
- data/test/{case_opencascade.rb → case_open_cascade.rb} +4 -8
- data/test/case_open_hash.rb +87 -0
- data/test/case_query_hash.rb +226 -0
- data/test/helper.rb +8 -0
- metadata +33 -63
- data/COPYING.rdoc +0 -45
- data/lib/hashery/basic_object.rb +0 -74
- data/lib/hashery/basic_struct.rb +0 -288
- data/lib/hashery/basicobject.rb +0 -1
- data/lib/hashery/basicstruct.rb +0 -1
- data/lib/hashery/castinghash.rb +0 -1
- data/lib/hashery/fuzzyhash.rb +0 -1
- data/lib/hashery/ini.rb +0 -268
- data/lib/hashery/keyhash.rb +0 -1
- data/lib/hashery/linkedlist.rb +0 -1
- data/lib/hashery/lruhash.rb +0 -1
- data/lib/hashery/memoizer.rb +0 -64
- data/lib/hashery/open_object.rb +0 -1
- data/lib/hashery/opencascade.rb +0 -1
- data/lib/hashery/openhash.rb +0 -1
- data/lib/hashery/openobject.rb +0 -1
- data/lib/hashery/orderedhash.rb +0 -1
- data/lib/hashery/ostructable.rb +0 -186
- data/lib/hashery/propertyhash.rb +0 -1
- data/lib/hashery/queryhash.rb +0 -1
- data/lib/hashery/statichash.rb +0 -1
- data/qed/01_openhash.rdoc +0 -57
- data/qed/02_queryhash.rdoc +0 -21
- data/qed/03_castinghash.rdoc +0 -13
- data/qed/04_statichash.rdoc +0 -22
- data/qed/05_association.rdoc +0 -59
- data/qed/06_opencascade.rdoc +0 -58
- data/qed/07_fuzzyhash.rdoc +0 -141
- data/qed/08_properyhash.rdoc +0 -38
- data/qed/09_ostructable.rdoc +0 -56
- data/qed/applique/ae.rb +0 -1
- data/test/case_basicstruct.rb +0 -192
- data/test/case_openhash.rb +0 -22
data/.ruby
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
---
|
2
2
|
source:
|
3
|
-
-
|
3
|
+
- meta
|
4
4
|
authors:
|
5
5
|
- name: Trans
|
6
6
|
email: transfire@gmail.com
|
@@ -14,10 +14,7 @@ copyrights:
|
|
14
14
|
- holder: Rubyworks
|
15
15
|
year: '2010'
|
16
16
|
license: BSD-2-Clause
|
17
|
-
replacements: []
|
18
|
-
alternatives: []
|
19
17
|
requirements:
|
20
|
-
- name: blankslate
|
21
18
|
- name: detroit
|
22
19
|
groups:
|
23
20
|
- build
|
@@ -31,33 +28,49 @@ requirements:
|
|
31
28
|
- test
|
32
29
|
development: true
|
33
30
|
dependencies: []
|
31
|
+
alternatives: []
|
34
32
|
conflicts: []
|
35
33
|
repositories:
|
36
34
|
- uri: git://github.com/rubyworks/hashery.git
|
37
35
|
scm: git
|
38
36
|
name: upstream
|
39
37
|
resources:
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
- uri: http://rubyworks.github.com/hashery
|
39
|
+
label: Website
|
40
|
+
type: home
|
41
|
+
- uri: http://github.com/rubyworks/hashery
|
42
|
+
label: Source Code
|
43
|
+
type: code
|
44
|
+
- uri: http://groups.google.com/group/rubyworks-mailinglist
|
45
|
+
label: Mailing List
|
46
|
+
type: mail
|
47
|
+
- uri: http://rubydoc.info/github/rubyworks/hashery/master/frames
|
48
|
+
label: Documentation
|
49
|
+
type: docs
|
50
|
+
- uri: http://wiki.github.com/rubyworks/hashery
|
51
|
+
label: User Guide
|
52
|
+
type: wiki
|
53
|
+
- uri: http://rubygems.org/gems/hashery
|
54
|
+
type: gems
|
55
|
+
categories: []
|
46
56
|
extra: {}
|
47
57
|
load_path:
|
48
58
|
- lib
|
59
|
+
- alt
|
49
60
|
revision: 0
|
50
61
|
created: '2010-04-21'
|
51
62
|
summary: Facets-bread collection of Hash-like classes.
|
52
63
|
title: Hashery
|
53
|
-
version:
|
64
|
+
version: 2.0.0
|
54
65
|
name: hashery
|
55
|
-
description: ! 'The Hashery is a collection of Hash-like classes
|
56
|
-
|
66
|
+
description: ! 'The Hashery is a tight collection of Hash-like classes. Included among
|
67
|
+
its many
|
68
|
+
|
69
|
+
offerings are the auto-sorting Dictionary class, the efficient LRUHash, the
|
57
70
|
|
58
|
-
|
71
|
+
flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
|
59
72
|
|
60
|
-
|
73
|
+
of the CRUDHash which defines a CRUD model on top of Ruby''s standard Hash
|
61
74
|
|
62
|
-
|
63
|
-
date: '
|
75
|
+
making it a snap to subclass and augment to fit any specific use case.'
|
76
|
+
date: '2012-06-18'
|
data/.yardopts
CHANGED
data/Config.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
config 'rubytest' do
|
2
|
+
$:.unshift 'test'
|
3
|
+
$:.unshift 'lib'
|
4
|
+
end
|
5
|
+
|
6
|
+
profile 'coverage' do
|
7
|
+
config 'rubytest' do
|
8
|
+
$:.unshift 'test'
|
9
|
+
$:.unshift 'lib'
|
10
|
+
|
11
|
+
require 'simplecov'
|
12
|
+
SimpleCov.command_name 'RubyTest'
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter '/test/'
|
15
|
+
add_filter '/lib/hashery/ordered_hash.rb'
|
16
|
+
coverage_dir 'log/coverage'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
config 'qed' do
|
21
|
+
require 'simplecov'
|
22
|
+
SimpleCov.command_name 'QED'
|
23
|
+
SimpleCov.start do
|
24
|
+
add_filter '/demo/'
|
25
|
+
coverage_dir 'log/coverage'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/{QED.rdoc → DEMO.rdoc}
RENAMED
File without changes
|
data/HISTORY.rdoc
CHANGED
@@ -1,5 +1,42 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
+
== 2.0.0 / 2012-05-11
|
4
|
+
|
5
|
+
This is a big release for Hashery which both culls some of it's
|
6
|
+
less fitting classes and modules while greatly improving the rest.
|
7
|
+
The first and most immediate change is use of a proper namespace.
|
8
|
+
All classes and modules are now appropriately kept in the `Hashery`
|
9
|
+
namespace. To get the old behavior you can `include Hashery` as the
|
10
|
+
toplevel. For the other changes and improvements dive into the
|
11
|
+
API documentation.
|
12
|
+
|
13
|
+
Changes:
|
14
|
+
|
15
|
+
* Use proper Hashery namespace.
|
16
|
+
* Add CRUDHash, which also serves a good base class.
|
17
|
+
* Improved OpenHash to be nearly 100% open.
|
18
|
+
* Deprecate BasicStruct, as it would be better to improve OpenStruct.
|
19
|
+
* Deprecate BasicCascade, though it never really came to be.
|
20
|
+
* Deprecate BasicObject emulator, as it is no longer needed.
|
21
|
+
* Deprecate Memoizer, not sure how that got in here anyway.
|
22
|
+
* Deprecate Ostructable, which can be paired up with better OpenStruct.
|
23
|
+
* Removed open_object.rb, which has long been deprecated.
|
24
|
+
|
25
|
+
|
26
|
+
== 1.5.1 / 2012-05-09
|
27
|
+
|
28
|
+
This release adds transformative #each method to OpenCascade, to
|
29
|
+
ensure #each returns an OpenCascade. Also, BasicCascade has been
|
30
|
+
added that is like OpenCascade by fully open by use of BasicObject
|
31
|
+
as a base class.
|
32
|
+
|
33
|
+
Changes:
|
34
|
+
|
35
|
+
* Fix OpenCascade#each (porecreat).
|
36
|
+
* Introduce BasicCascade class.
|
37
|
+
* Renamed `Ini` class to `IniHash` class.
|
38
|
+
|
39
|
+
|
3
40
|
== 1.5.0 / 2011-11-10
|
4
41
|
|
5
42
|
In this release, CoreExt module has been addeed to encapsulate
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Hashery
|
2
|
+
|
3
|
+
Copyright (c) 2009 Rubyworks
|
4
|
+
|
5
|
+
MIT License
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
8
|
+
this software and associated documentation files (the "Software"), to deal in
|
9
|
+
the Software without restriction, including without limitation the rights to use,
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
11
|
+
Software, and to permit persons to whom the Software is furnished to do so,
|
12
|
+
subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
15
|
+
copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
19
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
20
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
21
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
Website: http://rubyworks.github.com/hashery
|
25
|
+
License: http://www.spdx.org/licenses/MIT
|
26
|
+
|
data/NOTICE.txt
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
All copyright holders have released their wares with a compatible
|
2
|
+
license or have given permission for their work to be distributed
|
3
|
+
under the same license as Hashery.
|
4
|
+
|
5
|
+
BasicObject is based on Jim Weirich's BlankSlate class.
|
6
|
+
|
7
|
+
BlankSlate
|
8
|
+
Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
|
9
|
+
All rights reserved.
|
10
|
+
|
11
|
+
Dictionary is based on OrderedHash by Jan Molic.
|
12
|
+
|
13
|
+
OrderHash 2.0 Copyright (c) 2005 Jan Molic.
|
14
|
+
|
15
|
+
|
16
|
+
LRUHash is based on same by Robert Klemme
|
17
|
+
|
18
|
+
Copyright (c) 2010 Robert Klemme
|
19
|
+
|
20
|
+
|
21
|
+
Memoizer is based on same by Erik Veenstra
|
22
|
+
|
23
|
+
Copyright (c) 2006 Erik Veenstra
|
24
|
+
|
25
|
+
|
26
|
+
IniHash is based on Ini library by Jeena Paradies
|
27
|
+
|
28
|
+
Copyright (c) 2007 Jeena Paradies <info@jeenaparadies.net>
|
29
|
+
|
30
|
+
|
31
|
+
LinkedList is based on same by Kirk Haines
|
32
|
+
|
33
|
+
Copyright (C) 2006 Kirk Haines <khaines@enigo.com>
|
34
|
+
|
35
|
+
|
36
|
+
FuzzyHash is based on the same by Joshua Hull.
|
37
|
+
|
38
|
+
Copyright (c) 2009 Joshua Hull
|
39
|
+
|
40
|
+
|
41
|
+
PathHash is based on SlashedHash by Daniel Parker
|
42
|
+
|
43
|
+
HashMagic by Daniel Parker <gems@behindlogic.com>
|
44
|
+
Copyright (c) 2006 BehindLogic
|
45
|
+
http://hash_magic.rubyforge.org
|
46
|
+
|
data/README.rdoc
CHANGED
@@ -66,19 +66,22 @@ Well git in there!
|
|
66
66
|
|
67
67
|
Hashery uses QED[http://rubyworks.github.com/qed] and
|
68
68
|
Lemon[http://rubyworks.github.com/lemon] test frameworks.
|
69
|
-
|
69
|
+
The QED framework to create well tested high-level documentation.
|
70
|
+
Run the QED specs via:
|
70
71
|
|
71
|
-
$ qed -Ilib
|
72
|
+
$ qed -Ilib demo/
|
72
73
|
|
73
|
-
|
74
|
+
Lemon is used to create low-level unit tests. Run these via the
|
75
|
+
RubyTest univeral test harness.
|
76
|
+
|
77
|
+
$ rubytest -Ilib test/
|
74
78
|
|
75
|
-
$ lemon -Ilib test/
|
76
79
|
|
77
80
|
=== Patches
|
78
81
|
|
79
82
|
Hashery's repository is hosted on GitHub[http://github.com/rubyworks/hashery].
|
80
|
-
If you'd like to offer up a fix or feature
|
81
|
-
|
83
|
+
If you'd like to offer up a fix or feature, fork the repo and submit a pull
|
84
|
+
request (preferably in a topic branch). I assume you have heard
|
82
85
|
all the talk about proper {practices}[http://learn.github.com/p/intro.html],
|
83
86
|
so I won't bug you with it yet again.
|
84
87
|
|
@@ -100,5 +103,5 @@ See COPYING.rdoc file for further details.
|
|
100
103
|
|
101
104
|
Some libraries included in the Hashery have special copyrights
|
102
105
|
attributing specific authors. Please see each library script for
|
103
|
-
specifics.
|
106
|
+
specifics and the NOITCE.txt file for an overview.
|
104
107
|
|
data/lib/hashery.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
require 'hashery/core_ext'
|
2
2
|
|
3
|
-
require 'hashery/basic_struct'
|
3
|
+
#require 'hashery/basic_struct'
|
4
4
|
require 'hashery/casting_hash'
|
5
|
+
require 'hashery/crud_hash'
|
5
6
|
require 'hashery/dictionary'
|
6
7
|
require 'hashery/fuzzy_hash'
|
8
|
+
require 'hashery/ini_hash'
|
7
9
|
require 'hashery/linked_list'
|
8
10
|
require 'hashery/lru_hash'
|
9
11
|
require 'hashery/key_hash'
|
10
|
-
require 'hashery/memoizer'
|
11
12
|
require 'hashery/open_cascade'
|
12
13
|
require 'hashery/open_hash'
|
13
|
-
#require 'hashery/
|
14
|
-
require 'hashery/
|
15
|
-
require 'hashery/ostructable'
|
14
|
+
#require 'hashery/ordered_hash'
|
15
|
+
#require 'hashery/ostructable'
|
16
16
|
require 'hashery/property_hash'
|
17
17
|
require 'hashery/query_hash'
|
18
18
|
require 'hashery/static_hash'
|
19
19
|
|
20
|
-
|
20
|
+
|
data/lib/hashery.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
---
|
2
2
|
source:
|
3
|
-
-
|
3
|
+
- meta
|
4
4
|
authors:
|
5
5
|
- name: Trans
|
6
6
|
email: transfire@gmail.com
|
@@ -14,10 +14,7 @@ copyrights:
|
|
14
14
|
- holder: Rubyworks
|
15
15
|
year: '2010'
|
16
16
|
license: BSD-2-Clause
|
17
|
-
replacements: []
|
18
|
-
alternatives: []
|
19
17
|
requirements:
|
20
|
-
- name: blankslate
|
21
18
|
- name: detroit
|
22
19
|
groups:
|
23
20
|
- build
|
@@ -31,33 +28,49 @@ requirements:
|
|
31
28
|
- test
|
32
29
|
development: true
|
33
30
|
dependencies: []
|
31
|
+
alternatives: []
|
34
32
|
conflicts: []
|
35
33
|
repositories:
|
36
34
|
- uri: git://github.com/rubyworks/hashery.git
|
37
35
|
scm: git
|
38
36
|
name: upstream
|
39
37
|
resources:
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
- uri: http://rubyworks.github.com/hashery
|
39
|
+
label: Website
|
40
|
+
type: home
|
41
|
+
- uri: http://github.com/rubyworks/hashery
|
42
|
+
label: Source Code
|
43
|
+
type: code
|
44
|
+
- uri: http://groups.google.com/group/rubyworks-mailinglist
|
45
|
+
label: Mailing List
|
46
|
+
type: mail
|
47
|
+
- uri: http://rubydoc.info/github/rubyworks/hashery/master/frames
|
48
|
+
label: Documentation
|
49
|
+
type: docs
|
50
|
+
- uri: http://wiki.github.com/rubyworks/hashery
|
51
|
+
label: User Guide
|
52
|
+
type: wiki
|
53
|
+
- uri: http://rubygems.org/gems/hashery
|
54
|
+
type: gems
|
55
|
+
categories: []
|
46
56
|
extra: {}
|
47
57
|
load_path:
|
48
58
|
- lib
|
59
|
+
- alt
|
49
60
|
revision: 0
|
50
61
|
created: '2010-04-21'
|
51
62
|
summary: Facets-bread collection of Hash-like classes.
|
52
63
|
title: Hashery
|
53
|
-
version:
|
64
|
+
version: 2.0.0
|
54
65
|
name: hashery
|
55
|
-
description: ! 'The Hashery is a collection of Hash-like classes
|
56
|
-
|
66
|
+
description: ! 'The Hashery is a tight collection of Hash-like classes. Included among
|
67
|
+
its many
|
68
|
+
|
69
|
+
offerings are the auto-sorting Dictionary class, the efficient LRUHash, the
|
57
70
|
|
58
|
-
|
71
|
+
flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
|
59
72
|
|
60
|
-
|
73
|
+
of the CRUDHash which defines a CRUD model on top of Ruby''s standard Hash
|
61
74
|
|
62
|
-
|
63
|
-
date: '
|
75
|
+
making it a snap to subclass and augment to fit any specific use case.'
|
76
|
+
date: '2012-06-18'
|
data/lib/hashery/association.rb
CHANGED
@@ -1,123 +1,183 @@
|
|
1
|
-
|
1
|
+
module Hashery
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# associated with another. It has a variety of uses,
|
7
|
-
# link-lists, simple ordered maps and mixed collections,
|
8
|
-
# among them.
|
9
|
-
#
|
10
|
-
# NOTE: This class is still fairly experimental. And it is not
|
11
|
-
# loaded along with the other Hashery libraries when using
|
12
|
-
# `require 'hashery'`.
|
13
|
-
#
|
14
|
-
# == Usage
|
15
|
-
#
|
16
|
-
# Associations can be used to draw simple relationships.
|
17
|
-
#
|
18
|
-
# :Apple >> :Fruit
|
19
|
-
# :Apple >> :Red
|
20
|
-
#
|
21
|
-
# :Apple.associations #=> [ :Fruit, :Red ]
|
22
|
-
#
|
23
|
-
# It can also be used for simple lists of ordered pairs.
|
24
|
-
#
|
25
|
-
# c = [ :a >> 1, :b >> 2 ]
|
26
|
-
# c.each { |k,v| puts "#{k} associated with #{v} }
|
27
|
-
#
|
28
|
-
# produces
|
29
|
-
#
|
30
|
-
# a associated with 1
|
31
|
-
# b associated with 2
|
32
|
-
#
|
33
|
-
# == Limitations
|
34
|
-
#
|
35
|
-
# The method :>> is used to construct the association.
|
36
|
-
# It is a rarely used method so it is generally available.
|
37
|
-
# But you can't use it for any of the following classes
|
38
|
-
# becuase they use #>> for other things.
|
39
|
-
#
|
40
|
-
# Bignum
|
41
|
-
# Fixnum
|
42
|
-
# Date
|
43
|
-
# IPAddr
|
44
|
-
# Process::Status
|
45
|
-
#
|
46
|
-
#--
|
47
|
-
# TODO: Should associations be singleton?
|
48
|
-
#
|
49
|
-
# TODO: Is it really wise to keep a table of all associations?
|
50
|
-
#++
|
51
|
-
|
52
|
-
class Association
|
53
|
-
include Comparable
|
3
|
+
# TODO: Should associations be singleton?
|
4
|
+
#
|
5
|
+
# TODO: Is it really wise to keep a table of all associations?
|
54
6
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
7
|
+
# Association is a general binary association that allows one
|
8
|
+
# object to be associated with another. It has a variety of uses,
|
9
|
+
# such as linked-lists, simple ordered maps and mixed collections,
|
10
|
+
# among them.
|
11
|
+
#
|
12
|
+
# NOTE: This class is still fairly experimental. And it is not
|
13
|
+
# loaded along with the other Hashery libraries when using
|
14
|
+
# `require 'hashery'`. It must be required independently.
|
15
|
+
#
|
16
|
+
# Associations can be used to draw simple relationships.
|
17
|
+
#
|
18
|
+
# :Apple >> :Fruit
|
19
|
+
# :Apple >> :Red
|
20
|
+
#
|
21
|
+
# :Apple.associations #=> [ :Fruit, :Red ]
|
22
|
+
#
|
23
|
+
# It can also be used for simple lists of ordered pairs.
|
24
|
+
#
|
25
|
+
# c = [ :a >> 1, :b >> 2 ]
|
26
|
+
# c.each { |k,v| puts "#{k} associated with #{v} }
|
27
|
+
#
|
28
|
+
# produces
|
29
|
+
#
|
30
|
+
# a associated with 1
|
31
|
+
# b associated with 2
|
32
|
+
#
|
33
|
+
# The method :>> is used to construct the association.
|
34
|
+
# It is a rarely used method so it is generally available.
|
35
|
+
# But you can't use it for any of the following classes
|
36
|
+
# becuase they use #>> for other things.
|
37
|
+
#
|
38
|
+
# Bignum
|
39
|
+
# Fixnum
|
40
|
+
# Date
|
41
|
+
# IPAddr
|
42
|
+
# Process::Status
|
43
|
+
#
|
44
|
+
class Association
|
45
|
+
include Comparable
|
46
|
+
|
47
|
+
class << self
|
48
|
+
#
|
49
|
+
# Store association references.
|
50
|
+
#
|
51
|
+
# Returns `Hash` of all associaitons.
|
52
|
+
#
|
53
|
+
def reference
|
54
|
+
@reference ||= Hash.new{ |h,k,v| h[k]=[] }
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Shortcut for #new.
|
59
|
+
#
|
60
|
+
# index - The "index key" of the association.
|
61
|
+
# value - The "value" of the association.
|
62
|
+
#
|
63
|
+
# Returns `Association`.
|
64
|
+
#
|
65
|
+
def [](index, value)
|
66
|
+
new(index, value)
|
67
|
+
end
|
68
|
+
|
69
|
+
#def new(index, value)
|
70
|
+
# lookup[[index, value]] ||= new(index, value)
|
71
|
+
#end
|
72
|
+
|
73
|
+
#def lookup
|
74
|
+
# @lookup ||= {}
|
75
|
+
#end
|
59
76
|
end
|
60
77
|
|
61
|
-
|
62
|
-
|
78
|
+
#
|
79
|
+
# The "index key" of the association.
|
80
|
+
#
|
81
|
+
attr_accessor :index
|
82
|
+
|
83
|
+
#
|
84
|
+
# The "value" of the association.
|
85
|
+
#
|
86
|
+
attr_accessor :value
|
87
|
+
|
88
|
+
#
|
89
|
+
# Initialize new Association.
|
90
|
+
#
|
91
|
+
# index - The "index key" of the association.
|
92
|
+
# value - The "value" of the association.
|
93
|
+
#
|
94
|
+
def initialize(index, value=nil)
|
95
|
+
@index = index
|
96
|
+
@value = value
|
97
|
+
|
98
|
+
unless index.associations.include?(value)
|
99
|
+
index.associations << value
|
100
|
+
end
|
63
101
|
end
|
64
102
|
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
@index = index
|
79
|
-
@value = value
|
80
|
-
|
81
|
-
unless index.associations.include?(value)
|
82
|
-
index.associations << value
|
103
|
+
#
|
104
|
+
# Compare the values of two associations.
|
105
|
+
#
|
106
|
+
# TODO: Comparions with non-associations?
|
107
|
+
#
|
108
|
+
# assoc - The other `Association`.
|
109
|
+
#
|
110
|
+
# Returns [Integer] `1`, `0`, or `-1`.
|
111
|
+
#
|
112
|
+
def <=>(assoc)
|
113
|
+
return -1 if self.value < assoc.value
|
114
|
+
return 1 if self.value > assoc.value
|
115
|
+
return 0 if self.value == assoc.value
|
83
116
|
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def <=>(assoc)
|
87
|
-
return -1 if self.value < assoc.value
|
88
|
-
return 1 if self.value > assoc.value
|
89
|
-
return 0 if self.value == assoc.value
|
90
|
-
end
|
91
117
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
def inspect
|
103
|
-
%{#{@index.inspect} >> #{@value.inspect}}
|
104
|
-
end
|
118
|
+
#
|
119
|
+
# Invert association, making the index the value and vice-versa.
|
120
|
+
#
|
121
|
+
# Returns [Array] with two-elements reversed.
|
122
|
+
#
|
123
|
+
def invert!
|
124
|
+
temp = @index
|
125
|
+
@index = @value
|
126
|
+
@value = temp
|
127
|
+
end
|
105
128
|
|
106
|
-
|
107
|
-
|
108
|
-
|
129
|
+
#
|
130
|
+
# Produce a string representation.
|
131
|
+
#
|
132
|
+
# Returns [String].
|
133
|
+
#
|
134
|
+
def to_s
|
135
|
+
return "#{index} >> #{value}"
|
136
|
+
end
|
109
137
|
|
110
|
-
|
111
|
-
|
112
|
-
|
138
|
+
#
|
139
|
+
# Produce a literal code string for creating an association.
|
140
|
+
#
|
141
|
+
# Returns [String].
|
142
|
+
#
|
143
|
+
def inspect
|
144
|
+
"#{index.inspect} >> #{value.inspect}"
|
145
|
+
end
|
113
146
|
|
114
|
-
#
|
115
|
-
|
116
|
-
|
147
|
+
#
|
148
|
+
# Convert to two-element associative array.
|
149
|
+
#
|
150
|
+
# Returns [Array] Two-element Array of index and value pair.
|
151
|
+
#
|
152
|
+
def to_ary
|
153
|
+
[index, value]
|
117
154
|
end
|
118
155
|
|
119
|
-
|
120
|
-
|
156
|
+
#
|
157
|
+
# Object extensions.
|
158
|
+
#
|
159
|
+
module Kernel
|
160
|
+
|
161
|
+
#
|
162
|
+
# Define an association for +self+.
|
163
|
+
#
|
164
|
+
# to - The value of the association.
|
165
|
+
#
|
166
|
+
# Returns [Association].
|
167
|
+
#
|
168
|
+
def >>(to)
|
169
|
+
Association.new(self, to)
|
170
|
+
end
|
171
|
+
|
172
|
+
#
|
173
|
+
# List of associations for this object.
|
174
|
+
#
|
175
|
+
# Returns an `Array` of `Associations`.
|
176
|
+
#
|
177
|
+
def associations
|
178
|
+
Association.reference[self]
|
179
|
+
end
|
180
|
+
|
121
181
|
end
|
122
182
|
|
123
183
|
end
|
@@ -125,10 +185,9 @@ class Association
|
|
125
185
|
end
|
126
186
|
|
127
187
|
class Object #:nodoc:
|
128
|
-
include Association::Kernel
|
188
|
+
include Hashery::Association::Kernel
|
129
189
|
end
|
130
190
|
|
131
|
-
|
132
191
|
#--
|
133
192
|
# Setup the >> method in classes that use it already.
|
134
193
|
#
|
@@ -160,3 +219,4 @@ end
|
|
160
219
|
# end
|
161
220
|
#++
|
162
221
|
|
222
|
+
# Copyright (c) 2005 Rubyworks, Thomas Sawyer
|