Class: Restruct::Hash
- Inherits:
-
Structure
show all
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/restruct/hash.rb
Instance Attribute Summary
Attributes inherited from Structure
#id, #redis
Instance Method Summary
collapse
Methods inherited from Structure
#==, #destroy, #dump, #initialize, #restore
Instance Method Details
#[](key) ⇒ Object
9
10
11
|
# File 'lib/restruct/hash.rb', line 9
def [](key)
deserialize redis.call('HGET', id, key)
end
|
#clear ⇒ Object
56
57
58
59
|
# File 'lib/restruct/hash.rb', line 56
def clear
destroy
self
end
|
#delete(key) ⇒ Object
39
40
41
42
43
|
# File 'lib/restruct/hash.rb', line 39
def delete(key)
value = self[key]
redis.call 'HDEL', id, key
value
end
|
#delete_if ⇒ Object
45
46
47
48
|
# File 'lib/restruct/hash.rb', line 45
def delete_if
each { |k,v| delete k if yield k, v }
self
end
|
#each ⇒ Object
Also known as:
each_pair
93
94
95
|
# File 'lib/restruct/hash.rb', line 93
def each
keys.each { |key| yield key, self[key] }
end
|
#each_key ⇒ Object
98
99
100
|
# File 'lib/restruct/hash.rb', line 98
def each_key
each { |k,v| yield k }
end
|
#each_value ⇒ Object
102
103
104
|
# File 'lib/restruct/hash.rb', line 102
def each_value
each { |k,v| yield v }
end
|
#empty? ⇒ Boolean
89
90
91
|
# File 'lib/restruct/hash.rb', line 89
def empty?
size == 0
end
|
#fetch(key, default = nil, &block) ⇒ Object
13
14
15
16
17
18
19
20
|
# File 'lib/restruct/hash.rb', line 13
def fetch(key, default=nil, &block)
if key? key
self[key]
else
raise KeyError, "key not found: #{key}" if default.nil? && block.nil?
default || block.call(key)
end
end
|
#keep_if ⇒ Object
Also known as:
select!
50
51
52
53
|
# File 'lib/restruct/hash.rb', line 50
def keep_if
each { |k,v| delete k unless yield k, v }
self
end
|
#key(value) ⇒ Object
22
23
24
25
|
# File 'lib/restruct/hash.rb', line 22
def key(value)
index = values.index value
keys[index] if index
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
73
74
75
|
# File 'lib/restruct/hash.rb', line 73
def key?(key)
redis.call('HEXISTS', id, key) == 1
end
|
#keys ⇒ Object
61
62
63
|
# File 'lib/restruct/hash.rb', line 61
def keys
redis.call 'HKEYS', id
end
|
#size ⇒ Object
Also known as:
count, length
83
84
85
|
# File 'lib/restruct/hash.rb', line 83
def size
redis.call 'HLEN', id
end
|
#store(key, value) ⇒ Object
Also known as:
[]=
27
28
29
30
|
# File 'lib/restruct/hash.rb', line 27
def store(key, value)
redis.call 'HSET', id, key, serialize(value)
value
end
|
#to_h ⇒ Object
Also known as:
to_primitive
106
107
108
109
110
|
# File 'lib/restruct/hash.rb', line 106
def to_h
redis.call('HGETALL', id).each_slice(2).each_with_object({}) do |(k,v), hash|
hash[k] = deserialize v
end
end
|
#update(hash) ⇒ Object
Also known as:
merge!
33
34
35
36
|
# File 'lib/restruct/hash.rb', line 33
def update(hash)
hash.each { |k,v| store k, v }
self
end
|
#value?(value) ⇒ Boolean
Also known as:
has_value?
78
79
80
|
# File 'lib/restruct/hash.rb', line 78
def value?(value)
values.include? value
end
|
#values ⇒ Object
65
66
67
|
# File 'lib/restruct/hash.rb', line 65
def values
redis.call('HVALS', id).map { |v| deserialize v }
end
|
#values_at(*keys) ⇒ Object
69
70
71
|
# File 'lib/restruct/hash.rb', line 69
def values_at(*keys)
keys.map { |k| self[k] }
end
|