Class: Hashery::CRUDHash
- Inherits:
-
Hash
show all
- Defined in:
- lib/hashery/crud_hash.rb
Overview
The CRUDHash is essentailly the same as the Hash class, but it reduces the
the set of necessary methods to the fundametal CRUD requirements. All other
methods route through these CRUD methods. This is a better general design,
although it is, of course, a little bit slower. The utility of this class
becomes appearent when subclassing or delegating, as only a handful of
methods need to be changed for all other methods to work accordingly.
In addition to the CRUD features, CRUDHash supports a `#key_proc`, akin to
`#default_proc`, that can be used to normalize keys.
Instance Method Summary
(collapse)
Methods inherited from Hash
#to_keyhash, #to_stash
Methods included from CoreExt
#rekey, #rekey!
Instance Method Details
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/hashery/crud_hash.rb', line 58
def <<(x)
case x
when Hash
update(x)
when Array
x.each_slice(2) do |(k,v)|
store(k,v)
end
else
raise ArgumentError end
end
|
72
73
74
75
76
77
78
79
80
|
# File 'lib/hashery/crud_hash.rb', line 72
def [](key)
if key?(key)
fetch(key)
elsif default_proc
default_proc.call(self, key)
else
default
end
end
|
- (Object) []=(key, value)
83
84
85
|
# File 'lib/hashery/crud_hash.rb', line 83
def []=(key,value)
store(key,value)
end
|
- (Object) delete(key)
53
54
55
|
# File 'lib/hashery/crud_hash.rb', line 53
def delete(key)
super cast_key(key)
end
|
- (Object) each
Also known as:
each_pair
106
107
108
109
110
|
# File 'lib/hashery/crud_hash.rb', line 106
def each super do |k,v|
yield(k, v)
end
end
|
- (Object) fetch(key)
CRUD method for reading value.
43
44
45
|
# File 'lib/hashery/crud_hash.rb', line 43
def fetch(key)
super cast_key(key)
end
|
- (Boolean) key?(key)
Also known as:
has_key?, member?, include?
CRUD method for checking if key exists.
38
39
40
|
# File 'lib/hashery/crud_hash.rb', line 38
def key?(key)
super cast_key(key)
end
|
- (Object) key_proc(&block)
32
33
34
35
|
# File 'lib/hashery/crud_hash.rb', line 32
def key_proc(&block)
@key_proc = block if block
@key_proc
end
|
- (Object) key_proc=(proc)
22
23
24
25
|
# File 'lib/hashery/crud_hash.rb', line 22
def key_proc=(proc)
raise ArgumentError unless Proc === proc or NilClass === proc
@key_proc = proc
end
|
- (Object) merge(other)
98
99
100
101
102
103
|
# File 'lib/hashery/crud_hash.rb', line 98
def merge(other)
copy = dup
other.each{ |k,v| copy.store(k, v) }
copy
end
|
- (Object) replace(other)
119
120
121
|
# File 'lib/hashery/crud_hash.rb', line 119
def replace(other)
super cast(other)
end
|
- (Object) store(key, value)
CRUD method for create and update.
48
49
50
|
# File 'lib/hashery/crud_hash.rb', line 48
def store(key, value)
super(cast_key(key), value)
end
|
- (Object) to_hash
Also known as:
to_h
TODO:
Should CRUDHash#to_hash convert to traditional hash?
130
131
132
|
# File 'lib/hashery/crud_hash.rb', line 130
def to_hash
h = {}; each{ |k,v| h[k] = v }; h
end
|
- (Object) update(other)
Also known as:
merge!
88
89
90
91
92
|
# File 'lib/hashery/crud_hash.rb', line 88
def update(other)
other.each do |k,v|
store(k, v)
end
end
|
- (Object) values_at(*keys)
124
125
126
|
# File 'lib/hashery/crud_hash.rb', line 124
def values_at(*keys)
super *keys.map{ |key| cast_key(key) }
end
|