hashery 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.ruby +98 -0
- data/HISTORY.rdoc +17 -4
- data/NOTICE +11 -0
- data/lib/hashery.rb +1 -1
- data/lib/hashery.yml +98 -0
- data/lib/hashery/basic_struct.rb +1 -0
- data/lib/hashery/basicobject.rb +74 -0
- data/lib/hashery/basicstruct.rb +280 -0
- data/lib/hashery/open_object.rb +1 -1
- data/lib/hashery/openobject.rb +1 -279
- data/lib/hashery/ostructable.rb +48 -71
- data/qed/01_openhash.rdoc +57 -0
- data/qed/02_queryhash.rdoc +21 -0
- data/qed/03_castinghash.rdoc +13 -0
- data/qed/04_statichash.rdoc +22 -0
- data/qed/05_association.rdoc +59 -0
- data/qed/06_opencascade.rdoc +58 -0
- data/qed/07_fuzzyhash.rdoc +141 -0
- data/qed/08_properyhash.rdoc +38 -0
- data/qed/09_ostructable.rdoc +56 -0
- data/qed/applique/ae.rb +1 -0
- metadata +53 -9
- data/PROFILE +0 -35
- data/VERSION +0 -3
data/.ruby
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
---
|
2
|
+
name: hashery
|
3
|
+
company: RubyWorks
|
4
|
+
title: Hashery
|
5
|
+
contact: Trans <transfire@gmail.com>
|
6
|
+
maintainers:
|
7
|
+
- Trans <transfire@gmail.com>
|
8
|
+
pom_verison: 1.0.0
|
9
|
+
requires:
|
10
|
+
- group:
|
11
|
+
- build
|
12
|
+
name: syckle
|
13
|
+
version: 0+
|
14
|
+
- group:
|
15
|
+
- test
|
16
|
+
name: qed
|
17
|
+
version: 0+
|
18
|
+
resources:
|
19
|
+
repo: git://github.com/rubyworks/hashery.git
|
20
|
+
code: http://github.com/rubyworks/hashery
|
21
|
+
api: http://rubyworks.github.com/hashery/docs/api
|
22
|
+
mail: http://groups.google.com/group/rubyworks-mailinglist
|
23
|
+
host: http://rubygems.org/gems/hashery
|
24
|
+
wiki: http://wiki.github.com/rubyworks/hashery
|
25
|
+
home: http://rubyworks.github.com/hashery
|
26
|
+
manifest:
|
27
|
+
- .ruby
|
28
|
+
- lib/hashery/association.rb
|
29
|
+
- lib/hashery/basic_struct.rb
|
30
|
+
- lib/hashery/basicobject.rb
|
31
|
+
- lib/hashery/basicstruct.rb
|
32
|
+
- lib/hashery/casting_hash.rb
|
33
|
+
- lib/hashery/castinghash.rb
|
34
|
+
- lib/hashery/dictionary.rb
|
35
|
+
- lib/hashery/fuzzy_hash.rb
|
36
|
+
- lib/hashery/fuzzyhash.rb
|
37
|
+
- lib/hashery/ini.rb
|
38
|
+
- lib/hashery/linked_list.rb
|
39
|
+
- lib/hashery/linkedlist.rb
|
40
|
+
- lib/hashery/lru_hash.rb
|
41
|
+
- lib/hashery/lruhash.rb
|
42
|
+
- lib/hashery/memoizer.rb
|
43
|
+
- lib/hashery/open_cascade.rb
|
44
|
+
- lib/hashery/open_hash.rb
|
45
|
+
- lib/hashery/open_object.rb
|
46
|
+
- lib/hashery/opencascade.rb
|
47
|
+
- lib/hashery/openhash.rb
|
48
|
+
- lib/hashery/openobject.rb
|
49
|
+
- lib/hashery/ordered_hash.rb
|
50
|
+
- lib/hashery/orderedhash.rb
|
51
|
+
- lib/hashery/ostructable.rb
|
52
|
+
- lib/hashery/property_hash.rb
|
53
|
+
- lib/hashery/propertyhash.rb
|
54
|
+
- lib/hashery/query_hash.rb
|
55
|
+
- lib/hashery/queryhash.rb
|
56
|
+
- lib/hashery/sparse_array.rb
|
57
|
+
- lib/hashery/sparsearray.rb
|
58
|
+
- lib/hashery/stash.rb
|
59
|
+
- lib/hashery/static_hash.rb
|
60
|
+
- lib/hashery/statichash.rb
|
61
|
+
- lib/hashery.rb
|
62
|
+
- lib/hashery.yml
|
63
|
+
- qed/01_openhash.rdoc
|
64
|
+
- qed/02_queryhash.rdoc
|
65
|
+
- qed/03_castinghash.rdoc
|
66
|
+
- qed/04_statichash.rdoc
|
67
|
+
- qed/05_association.rdoc
|
68
|
+
- qed/06_opencascade.rdoc
|
69
|
+
- qed/07_fuzzyhash.rdoc
|
70
|
+
- qed/08_properyhash.rdoc
|
71
|
+
- qed/09_ostructable.rdoc
|
72
|
+
- qed/applique/ae.rb
|
73
|
+
- test/case_association.rb
|
74
|
+
- test/case_dictionary.rb
|
75
|
+
- test/case_opencascade.rb
|
76
|
+
- test/case_openhash.rb
|
77
|
+
- test/case_openobject.rb
|
78
|
+
- test/case_sparsearray.rb
|
79
|
+
- test/case_stash.rb
|
80
|
+
- HISTORY.rdoc
|
81
|
+
- LICENSE
|
82
|
+
- README.rdoc
|
83
|
+
- NOTICE
|
84
|
+
version: 1.4.0
|
85
|
+
licenses:
|
86
|
+
- Apache 2.0
|
87
|
+
copyright: Copyright (c) 2010 Thomas Sawyer
|
88
|
+
description: The Hashery is a collection of Hash-like classes, spun-off from the original Ruby Facets library. Included are the widely used OrderedHash, the related but more featured Dictionary class, a number of open classes, similiar to the standard OpenStruct and a few variations on the standard Hash.
|
89
|
+
summary: Facets-bread collection of Hash-like classes.
|
90
|
+
authors:
|
91
|
+
- Thomas Sawyer
|
92
|
+
- Kirk Haines
|
93
|
+
- Robert Klemme
|
94
|
+
- Jan Molic
|
95
|
+
- George Moschovitis
|
96
|
+
- Jeena Paradies
|
97
|
+
- Erik Veenstra
|
98
|
+
created: 2010-04-21
|
data/HISTORY.rdoc
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
-
== 1.
|
3
|
+
== 1.4.0 / 2011-01-19
|
4
|
+
|
5
|
+
This release includes a copy of Ruby Facets' BasicObject class, which
|
6
|
+
fixes the loading bug of the previous version. This release also renames
|
7
|
+
OpenObject to BasicStruct, which is a much better destription of what the
|
8
|
+
class actually provides.
|
9
|
+
|
10
|
+
Changes:
|
11
|
+
|
12
|
+
* Rename OpenObject to BasicStruct.
|
13
|
+
* Fix basicobject.rb loading issue.
|
14
|
+
|
15
|
+
|
16
|
+
== 1.3.0 / 2010-10-01
|
4
17
|
|
5
18
|
This release fixes a minor bug in CastingHash and adds a new
|
6
19
|
PropertyHash class.
|
@@ -16,7 +29,7 @@ Changes:
|
|
16
29
|
* Fixed CastingHash#new where #to_proc is called against NilClass
|
17
30
|
|
18
31
|
|
19
|
-
== 1.2.0
|
32
|
+
== 1.2.0 / 2010-06-04
|
20
33
|
|
21
34
|
This release makes two signifficant changes to the Hashery.
|
22
35
|
First, we have a new shiny library called FuzzyHash by
|
@@ -37,7 +50,7 @@ Changes:
|
|
37
50
|
* OpenCascade subclasses OpenHash and handles Array cascading.
|
38
51
|
|
39
52
|
|
40
|
-
== 1.1.0
|
53
|
+
== 1.1.0 / 2010-04-28
|
41
54
|
|
42
55
|
A follow-up release of Hashery that adds two new libraries:
|
43
56
|
Association and SparseArray. Both of these may seem like odd
|
@@ -56,7 +69,7 @@ Changes:
|
|
56
69
|
* Added sparsearray.rb
|
57
70
|
|
58
71
|
|
59
|
-
== 1.0.0
|
72
|
+
== 1.0.0 / 2010-04-21
|
60
73
|
|
61
74
|
This is the first release of the Facets Hashery.
|
62
75
|
Most of included classes come directly from Ruby
|
data/NOTICE
ADDED
data/lib/hashery.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
require 'hashery/basicstruct'
|
1
2
|
require 'hashery/castinghash'
|
2
3
|
require 'hashery/dictionary'
|
3
4
|
require 'hashery/lruhash'
|
4
5
|
require 'hashery/memoizer'
|
5
6
|
require 'hashery/opencascade'
|
6
7
|
require 'hashery/openhash'
|
7
|
-
require 'hashery/openobject'
|
8
8
|
require 'hashery/orderedhash'
|
9
9
|
require 'hashery/ostructable'
|
10
10
|
require 'hashery/queryhash'
|
data/lib/hashery.yml
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
---
|
2
|
+
name: hashery
|
3
|
+
company: RubyWorks
|
4
|
+
title: Hashery
|
5
|
+
contact: Trans <transfire@gmail.com>
|
6
|
+
maintainers:
|
7
|
+
- Trans <transfire@gmail.com>
|
8
|
+
pom_verison: 1.0.0
|
9
|
+
requires:
|
10
|
+
- group:
|
11
|
+
- build
|
12
|
+
name: syckle
|
13
|
+
version: 0+
|
14
|
+
- group:
|
15
|
+
- test
|
16
|
+
name: qed
|
17
|
+
version: 0+
|
18
|
+
resources:
|
19
|
+
repo: git://github.com/rubyworks/hashery.git
|
20
|
+
code: http://github.com/rubyworks/hashery
|
21
|
+
api: http://rubyworks.github.com/hashery/docs/api
|
22
|
+
mail: http://groups.google.com/group/rubyworks-mailinglist
|
23
|
+
host: http://rubygems.org/gems/hashery
|
24
|
+
wiki: http://wiki.github.com/rubyworks/hashery
|
25
|
+
home: http://rubyworks.github.com/hashery
|
26
|
+
manifest:
|
27
|
+
- .ruby
|
28
|
+
- lib/hashery/association.rb
|
29
|
+
- lib/hashery/basic_struct.rb
|
30
|
+
- lib/hashery/basicobject.rb
|
31
|
+
- lib/hashery/basicstruct.rb
|
32
|
+
- lib/hashery/casting_hash.rb
|
33
|
+
- lib/hashery/castinghash.rb
|
34
|
+
- lib/hashery/dictionary.rb
|
35
|
+
- lib/hashery/fuzzy_hash.rb
|
36
|
+
- lib/hashery/fuzzyhash.rb
|
37
|
+
- lib/hashery/ini.rb
|
38
|
+
- lib/hashery/linked_list.rb
|
39
|
+
- lib/hashery/linkedlist.rb
|
40
|
+
- lib/hashery/lru_hash.rb
|
41
|
+
- lib/hashery/lruhash.rb
|
42
|
+
- lib/hashery/memoizer.rb
|
43
|
+
- lib/hashery/open_cascade.rb
|
44
|
+
- lib/hashery/open_hash.rb
|
45
|
+
- lib/hashery/open_object.rb
|
46
|
+
- lib/hashery/opencascade.rb
|
47
|
+
- lib/hashery/openhash.rb
|
48
|
+
- lib/hashery/openobject.rb
|
49
|
+
- lib/hashery/ordered_hash.rb
|
50
|
+
- lib/hashery/orderedhash.rb
|
51
|
+
- lib/hashery/ostructable.rb
|
52
|
+
- lib/hashery/property_hash.rb
|
53
|
+
- lib/hashery/propertyhash.rb
|
54
|
+
- lib/hashery/query_hash.rb
|
55
|
+
- lib/hashery/queryhash.rb
|
56
|
+
- lib/hashery/sparse_array.rb
|
57
|
+
- lib/hashery/sparsearray.rb
|
58
|
+
- lib/hashery/stash.rb
|
59
|
+
- lib/hashery/static_hash.rb
|
60
|
+
- lib/hashery/statichash.rb
|
61
|
+
- lib/hashery.rb
|
62
|
+
- lib/hashery.yml
|
63
|
+
- qed/01_openhash.rdoc
|
64
|
+
- qed/02_queryhash.rdoc
|
65
|
+
- qed/03_castinghash.rdoc
|
66
|
+
- qed/04_statichash.rdoc
|
67
|
+
- qed/05_association.rdoc
|
68
|
+
- qed/06_opencascade.rdoc
|
69
|
+
- qed/07_fuzzyhash.rdoc
|
70
|
+
- qed/08_properyhash.rdoc
|
71
|
+
- qed/09_ostructable.rdoc
|
72
|
+
- qed/applique/ae.rb
|
73
|
+
- test/case_association.rb
|
74
|
+
- test/case_dictionary.rb
|
75
|
+
- test/case_opencascade.rb
|
76
|
+
- test/case_openhash.rb
|
77
|
+
- test/case_openobject.rb
|
78
|
+
- test/case_sparsearray.rb
|
79
|
+
- test/case_stash.rb
|
80
|
+
- HISTORY.rdoc
|
81
|
+
- LICENSE
|
82
|
+
- README.rdoc
|
83
|
+
- NOTICE
|
84
|
+
version: 1.4.0
|
85
|
+
licenses:
|
86
|
+
- Apache 2.0
|
87
|
+
copyright: Copyright (c) 2010 Thomas Sawyer
|
88
|
+
description: The Hashery is a collection of Hash-like classes, spun-off from the original Ruby Facets library. Included are the widely used OrderedHash, the related but more featured Dictionary class, a number of open classes, similiar to the standard OpenStruct and a few variations on the standard Hash.
|
89
|
+
summary: Facets-bread collection of Hash-like classes.
|
90
|
+
authors:
|
91
|
+
- Thomas Sawyer
|
92
|
+
- Kirk Haines
|
93
|
+
- Robert Klemme
|
94
|
+
- Jan Molic
|
95
|
+
- George Moschovitis
|
96
|
+
- Jeena Paradies
|
97
|
+
- Erik Veenstra
|
98
|
+
created: 2010-04-21
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'hashery/superstruct'
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Facets' BasicObject is an implementation of Jim Weirich's BlankSlate.
|
2
|
+
#
|
3
|
+
# BlankSlate
|
4
|
+
# Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Since Ruby 1.9 has a BasicObject class this will of course be
|
8
|
+
# deprecated as 1.9 goes mainstream.
|
9
|
+
|
10
|
+
unless defined? BasicObject # in case it already exists!
|
11
|
+
|
12
|
+
# BasicObject provides an abstract base class with no predefined
|
13
|
+
# methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
|
14
|
+
# BlankSlate is useful as a base class when writing classes that
|
15
|
+
# depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
|
16
|
+
class BasicObject
|
17
|
+
class << self
|
18
|
+
# Hide the method named +name+ in the BlankSlate class. Don't
|
19
|
+
# hide +instance_eval+ or any method beginning with "__".
|
20
|
+
#
|
21
|
+
# According to 1.9.1 it should have only these methods:
|
22
|
+
#
|
23
|
+
# * #__send__
|
24
|
+
# * #instance_eval
|
25
|
+
# * #instance_exec
|
26
|
+
# * #equal?
|
27
|
+
# * #==
|
28
|
+
# * #!
|
29
|
+
# * #!=
|
30
|
+
# * respond_to?
|
31
|
+
#
|
32
|
+
# Seems to me it should have #__id__ too.
|
33
|
+
def hide(name)
|
34
|
+
undef_method name if
|
35
|
+
instance_methods.include?(name.to_s) and
|
36
|
+
name !~ /^(__|respond_to\?|instance_eval$|instance_exec$|equal\?$|\=\=$)/
|
37
|
+
end
|
38
|
+
end
|
39
|
+
instance_methods.each{ |m| hide(m) }
|
40
|
+
end
|
41
|
+
|
42
|
+
# Since Ruby is very dynamic, methods added to the ancestors of
|
43
|
+
# BlankSlate <em>after BlankSlate is defined</em> will show up in the
|
44
|
+
# list of available BlankSlate methods. We handle this by defining a
|
45
|
+
# hook in the Object and Kernel classes that will hide any defined
|
46
|
+
module Kernel #:nodoc:
|
47
|
+
class << self
|
48
|
+
alias_method :basic_object_method_added, :method_added
|
49
|
+
|
50
|
+
# Detect method additions to Kernel and remove them in the
|
51
|
+
# BlankSlate class.
|
52
|
+
def method_added(name)
|
53
|
+
basic_object_method_added(name)
|
54
|
+
return if self != Kernel
|
55
|
+
BasicObject.hide(name)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Object #:nodoc:
|
61
|
+
class << self
|
62
|
+
alias_method :basic_object_method_added, :method_added
|
63
|
+
|
64
|
+
# Detect method additions to Object and remove them in the
|
65
|
+
# BlankSlate class.
|
66
|
+
def method_added(name)
|
67
|
+
basic_object_method_added(name)
|
68
|
+
return if self != Object
|
69
|
+
BasicObject.hide(name)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -0,0 +1,280 @@
|
|
1
|
+
require 'hashery/basicobject'
|
2
|
+
|
3
|
+
#require 'facets/hash/to_h'
|
4
|
+
#require 'facets/kernel/object_class'
|
5
|
+
#require 'facets/kernel/object_hexid'
|
6
|
+
|
7
|
+
# = BasicStruct
|
8
|
+
#
|
9
|
+
# BasicObject is very similar to Ruby's own OpenStruct, but it offers some
|
10
|
+
# advantages. With OpenStruct, slots with the same name as predefined
|
11
|
+
# Object methods cannot be used. With BasicObject, almost any slot can be
|
12
|
+
# defined. BasicObject is a subclass of BasicObject to ensure all method
|
13
|
+
# slots, except those that are absolutely essential, are open for use.
|
14
|
+
#
|
15
|
+
#--
|
16
|
+
# If you wish to pass an BasicObject to a routine that normal takes a Hash,
|
17
|
+
# but are uncertain it can handle the distictions properly you can convert
|
18
|
+
# easily to a Hash using #as_hash! and the result will automatically be
|
19
|
+
# converted back to an BasicObject on return.
|
20
|
+
#
|
21
|
+
# o = BasicObject.new(:a=>1,:b=>2)
|
22
|
+
# o.as_hash!{ |h| h.update(:a=>6) }
|
23
|
+
# o #=> #<BasicObject {:a=>6,:b=>2}>
|
24
|
+
#++
|
25
|
+
#
|
26
|
+
# Unlike a Hash, all BasicObject's keys are symbols and all keys are converted
|
27
|
+
# to such using #to_sym on the fly.
|
28
|
+
|
29
|
+
class BasicStruct < BasicObject
|
30
|
+
|
31
|
+
#PUBLIC_METHODS = /(^__|^instance_|^object_|^\W|^as$|^send$|^class$|\?$)/
|
32
|
+
#protected(*public_instance_methods.select{ |m| m !~ PUBLIC_METHODS })
|
33
|
+
|
34
|
+
def self.[](hash=nil)
|
35
|
+
new(hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Inititalizer for BasicObject is slightly different than that of Hash.
|
39
|
+
# It does not take a default parameter, but an initial priming Hash,
|
40
|
+
# like OpenStruct. The initializer can still take a default block
|
41
|
+
# however. To set the default value use <code>#default!(value)</code>.
|
42
|
+
#
|
43
|
+
# BasicObject.new(:a=>1).default!(0)
|
44
|
+
#
|
45
|
+
def initialize(hash=nil, &yld)
|
46
|
+
@table = Hash.new(&yld)
|
47
|
+
if hash
|
48
|
+
hash.each{ |k,v| store(k,v) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
def initialize_copy(orig)
|
54
|
+
orig.each{ |k,v| store(k,v) }
|
55
|
+
end
|
56
|
+
|
57
|
+
# Object inspection.
|
58
|
+
# TODO: Need to get __class__ and __id__ in hex form.
|
59
|
+
def inspect
|
60
|
+
#@table.inspect
|
61
|
+
hexid = __id__
|
62
|
+
klass = "BasicObject" # __class__
|
63
|
+
"#<#{klass}:#{hexid} #{@table.inspect}>"
|
64
|
+
end
|
65
|
+
|
66
|
+
# Convert to an associative array.
|
67
|
+
def to_a
|
68
|
+
@table.to_a
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
def to_h
|
73
|
+
@table.dup
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
def to_hash
|
78
|
+
@table.dup
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
def to_basicstruct
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
# Convert to an assignment procedure.
|
87
|
+
def to_proc(response=false)
|
88
|
+
hash = @table
|
89
|
+
if response
|
90
|
+
lambda do |o|
|
91
|
+
hash.each do |k,v|
|
92
|
+
o.__send__("#{k}=", v) rescue nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
else
|
96
|
+
lambda do |o|
|
97
|
+
hash.each{ |k,v| o.__send__("#{k}=", v) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# NOT SURE ABOUT THIS
|
103
|
+
def as_hash
|
104
|
+
@table
|
105
|
+
end
|
106
|
+
|
107
|
+
# Is a given +key+ defined?
|
108
|
+
def key?(key)
|
109
|
+
@table.key?(key.to_sym)
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
def is_a?(klass)
|
114
|
+
return true if klass == Hash # TODO: Is this wise? How to fake a subclass?
|
115
|
+
return true if klass == BasicObject
|
116
|
+
false
|
117
|
+
end
|
118
|
+
|
119
|
+
# Iterate over each key-value pair.
|
120
|
+
def each(&yld)
|
121
|
+
@table.each(&yld)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Set the default value.
|
125
|
+
def default!(default)
|
126
|
+
@table.default = default
|
127
|
+
end
|
128
|
+
|
129
|
+
# Check equality.
|
130
|
+
def ==( other )
|
131
|
+
case other
|
132
|
+
when BasicObject
|
133
|
+
@table == other.as_hash
|
134
|
+
when Hash
|
135
|
+
@table == other
|
136
|
+
else
|
137
|
+
if other.respond_to?(:to_hash)
|
138
|
+
@table == other.to_hash
|
139
|
+
else
|
140
|
+
false
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
def eql?( other )
|
147
|
+
case other
|
148
|
+
when BasicObject
|
149
|
+
@table.eql?(other.as_hash)
|
150
|
+
else
|
151
|
+
false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
#
|
156
|
+
def <<(x)
|
157
|
+
case x
|
158
|
+
when Hash
|
159
|
+
@table.update(x)
|
160
|
+
when Array
|
161
|
+
x.each_slice(2) do |(k,v)|
|
162
|
+
@table[k] = v
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
#
|
168
|
+
def []=(key, value)
|
169
|
+
@table[key.to_sym] = value
|
170
|
+
end
|
171
|
+
|
172
|
+
#
|
173
|
+
def [](key)
|
174
|
+
@table[key.to_sym]
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
def merge!(other)
|
179
|
+
BasicObject.new(@table.merge!(other))
|
180
|
+
end
|
181
|
+
|
182
|
+
#
|
183
|
+
def update!(other)
|
184
|
+
@table.update(other)
|
185
|
+
self
|
186
|
+
end
|
187
|
+
|
188
|
+
protected
|
189
|
+
|
190
|
+
def store(k, v)
|
191
|
+
@table.store(k.to_sym, v)
|
192
|
+
end
|
193
|
+
|
194
|
+
def fetch(k, *d, &b)
|
195
|
+
@table.fetch(k.to_sym, *d, &b)
|
196
|
+
end
|
197
|
+
|
198
|
+
#def as_hash!
|
199
|
+
# Functor.new do |op,*a,&b|
|
200
|
+
# result = @table.__send__(op,*a,&b)
|
201
|
+
# case result
|
202
|
+
# when Hash
|
203
|
+
# BasicObject.new(result)
|
204
|
+
# else
|
205
|
+
# result
|
206
|
+
# end
|
207
|
+
# end
|
208
|
+
#end
|
209
|
+
|
210
|
+
#def define_slot(key, value=nil)
|
211
|
+
# @table[key.to_sym] = value
|
212
|
+
#end
|
213
|
+
|
214
|
+
#def protect_slot( key )
|
215
|
+
# (class << self; self; end).class_eval {
|
216
|
+
# protected key rescue nil
|
217
|
+
# }
|
218
|
+
#end
|
219
|
+
|
220
|
+
def method_missing(sym, *args, &blk)
|
221
|
+
type = sym.to_s[-1,1]
|
222
|
+
key = sym.to_s.sub(/[=?!]$/,'').to_sym
|
223
|
+
case type
|
224
|
+
when '='
|
225
|
+
store(key, args[0])
|
226
|
+
when '!'
|
227
|
+
@table.__send__(key, *args, &blk)
|
228
|
+
# if key?(key)
|
229
|
+
# fetch(key)
|
230
|
+
# else
|
231
|
+
# store(key, BasicObject.new)
|
232
|
+
# end
|
233
|
+
when '?'
|
234
|
+
fetch(key)
|
235
|
+
else
|
236
|
+
fetch(key)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
# Core Extensions
|
243
|
+
|
244
|
+
class Hash
|
245
|
+
# Convert a Hash into a BasicStruct.
|
246
|
+
def to_basicstruct
|
247
|
+
BasicStruct[self]
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
=begin
|
252
|
+
class NilClass
|
253
|
+
# Nil converts to an empty BasicObject.
|
254
|
+
def to_basicstruct
|
255
|
+
BasicObject.new
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
class Proc
|
260
|
+
# Translates a Proc into an BasicObject. By droping an BasicObject into
|
261
|
+
# the Proc, the resulting assignments incured as the procedure is
|
262
|
+
# evaluated produce the BasicObject. This technique is simlar to that
|
263
|
+
# of MethodProbe.
|
264
|
+
#
|
265
|
+
# p = lambda { |x|
|
266
|
+
# x.word = "Hello"
|
267
|
+
# }
|
268
|
+
# o = p.to_basicstruct
|
269
|
+
# o.word #=> "Hello"
|
270
|
+
#
|
271
|
+
# NOTE The Proc must have an arity of one --no more and no less.
|
272
|
+
def to_basicstruct
|
273
|
+
raise ArgumentError, 'bad arity for converting Proc to basicstruct' if arity != 1
|
274
|
+
o = BasicObject.new
|
275
|
+
self.call( o )
|
276
|
+
o
|
277
|
+
end
|
278
|
+
end
|
279
|
+
=end
|
280
|
+
|