class Restruct::Hash

Public Instance Methods

[](key) click to toggle source
# File lib/restruct/hash.rb, line 9
def [](key)
  deserialize connection.call('HGET', id, key)
end
[]=(key, value)
Alias for: store
clear() click to toggle source
# File lib/restruct/hash.rb, line 56
def clear
  destroy
  self
end
count()
Alias for: size
delete(key) click to toggle source
# File lib/restruct/hash.rb, line 39
def delete(key)
  value = self[key]
  connection.lazy 'HDEL', id, key
  value
end
delete_if() { |k, v| ... } click to toggle source
# File lib/restruct/hash.rb, line 45
def delete_if
  each { |k,v| delete k if yield k, v }
  self
end
each() { |key, self| ... } click to toggle source
# File lib/restruct/hash.rb, line 93
def each
  keys.each { |key| yield key, self[key] }
end
Also aliased as: each_pair
each_key() { |k| ... } click to toggle source
# File lib/restruct/hash.rb, line 98
def each_key
  each { |k,v| yield k }
end
each_pair()
Alias for: each
each_value() { |v| ... } click to toggle source
# File lib/restruct/hash.rb, line 102
def each_value
  each { |k,v| yield v }
end
empty?() click to toggle source
# File lib/restruct/hash.rb, line 89
def empty?
  size == 0
end
fetch(key, default=nil, &block) click to toggle source
# 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
has_key?(key)
Alias for: key?
has_value?(value)
Alias for: value?
keep_if() { |k, v| ... } click to toggle source
# File lib/restruct/hash.rb, line 50
def keep_if
  each { |k,v| delete k unless yield k, v }
  self
end
Also aliased as: select!
key(value) click to toggle source
# File lib/restruct/hash.rb, line 22
def key(value)
  index = values.index value
  keys[index] if index
end
key?(key) click to toggle source
# File lib/restruct/hash.rb, line 73
def key?(key)
  connection.call('HEXISTS', id, key) == 1
end
Also aliased as: has_key?
keys() click to toggle source
# File lib/restruct/hash.rb, line 61
def keys
  connection.call 'HKEYS', id
end
length()
Alias for: size
merge!(hash)
Alias for: update
select!()
Alias for: keep_if
size() click to toggle source
# File lib/restruct/hash.rb, line 83
def size
  connection.call 'HLEN', id
end
Also aliased as: count, length
store(key, value) click to toggle source
# File lib/restruct/hash.rb, line 27
def store(key, value)
  connection.lazy 'HSET', id, key, serialize(value)
  value
end
Also aliased as: []=
to_h() click to toggle source
# File lib/restruct/hash.rb, line 106
def to_h
  connection.call('HGETALL', id).each_slice(2).each_with_object({}) do |(k,v), hash|
    hash[k] = deserialize v
  end
end
Also aliased as: to_primitive
to_primitive()
Alias for: to_h
update(hash) click to toggle source
# File lib/restruct/hash.rb, line 33
def update(hash)
  hash.each { |k,v| store k, v }
  self
end
Also aliased as: merge!
value?(value) click to toggle source
# File lib/restruct/hash.rb, line 78
def value?(value)
  values.include? value
end
Also aliased as: has_value?
values() click to toggle source
# File lib/restruct/hash.rb, line 65
def values
  connection.call('HVALS', id).map { |v| deserialize v }
end
values_at(*keys) click to toggle source
# File lib/restruct/hash.rb, line 69
def values_at(*keys)
  keys.map { |k| self[k] }
end

Private Instance Methods

deserialize(string) click to toggle source
# File lib/restruct/hash.rb, line 119
def deserialize(string)
  string
end
serialize(string) click to toggle source
# File lib/restruct/hash.rb, line 115
def serialize(string)
  string
end