Class: Redpear::Store::Hash

Inherits:
Enumerable show all
Defined in:
lib/redpear/store/hash.rb

Constant Summary

Constants inherited from Base

Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET

Instance Attribute Summary

Attributes inherited from Base

#conn, #key

Instance Method Summary collapse

Methods inherited from Enumerable

#count, #size

Methods inherited from Base

#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #watch

Constructor Details

This class inherits a constructor from Redpear::Store::Base

Instance Method Details

#==(other) ⇒ Boolean

Comparator

Returns:

  • (Boolean)

    true if same as ‘other`



120
121
122
# File 'lib/redpear/store/hash.rb', line 120

def ==(other)
  other.respond_to?(:to_hash) && other.to_hash == to_hash
end

#allHash Also known as: to_hash, value

Returns all pairs.

Returns:

  • (Hash)

    all pairs



11
12
13
# File 'lib/redpear/store/hash.rb', line 11

def all
  conn.hgetall(key) || {}
end

#decrement(field, value = 1) ⇒ Object

Parameters:

  • field (String)

    The field to decrement

  • value (Integer) (defaults to: 1)

    The decrement value, defaults to 1



91
92
93
# File 'lib/redpear/store/hash.rb', line 91

def decrement(field, value = 1)
  increment(field, -value)
end

#delete(field) ⇒ Object

Parameters:

  • field (String)

    The field to delete



19
20
21
# File 'lib/redpear/store/hash.rb', line 19

def delete(field)
  conn.hdel key, field
end

#each {|field, value| ... } ⇒ Object

Yields:

  • over a field-value pair

Yield Parameters:

  • field (String)
  • value (String)


6
7
8
# File 'lib/redpear/store/hash.rb', line 6

def each(&block)
  all.each(&block)
end

#empty?Boolean

Returns true, if empty.

Returns:

  • (Boolean)

    true, if empty



32
33
34
35
36
37
38
39
# File 'lib/redpear/store/hash.rb', line 32

def empty?
  case value = length
  when Redis::Future
    value.instance_eval { @transformation = IS_ZERO }
  else
    value.zero?
  end
end

#fetch(field) ⇒ String Also known as: []

Returns value stored in field.

Parameters:

  • field (String)

    The field to fetch

Returns:

  • (String)

    value stored in field



44
45
46
# File 'lib/redpear/store/hash.rb', line 44

def fetch(field)
  conn.hget key, field
end

#increment(field, value = 1) ⇒ Object

Parameters:

  • field (String)

    The field to increment

  • value (Integer) (defaults to: 1)

    The increment value, defaults to 1



83
84
85
# File 'lib/redpear/store/hash.rb', line 83

def increment(field, value = 1)
  conn.hincrby key, field, value
end

#key?(field) ⇒ Boolean Also known as: has_key?, member?, include?

Returns true, if field exists.

Returns:

  • (Boolean)

    true, if field exists



24
25
26
# File 'lib/redpear/store/hash.rb', line 24

def key?(field)
  conn.hexists(key, field)
end

#keysArray

Returns all keys.

Returns:

  • (Array)

    all keys



65
66
67
# File 'lib/redpear/store/hash.rb', line 65

def keys
  conn.hkeys key
end

#lengthIntege

Returns the number of pairs in the hash.

Returns:

  • (Intege)

    the number of pairs in the hash



75
76
77
# File 'lib/redpear/store/hash.rb', line 75

def length
  conn.hlen key
end

#merge!(hash) ⇒ Object

Parameters:

  • hash (Hash)

    The pairs to merge



111
112
113
114
115
116
# File 'lib/redpear/store/hash.rb', line 111

def merge!(hash)
  hash = hash.reject do |field, value|
    delete(field) if value.nil?
  end
  conn.hmset key, *hash.flatten unless hash.empty?
end

#store(field, value, options = {}) ⇒ Object Also known as: []=

Parameters:

  • field (String)

    The field to store at

  • value (String)

    The value to store

  • options (Hash) (defaults to: {})

    The value to store



55
56
57
58
59
60
61
# File 'lib/redpear/store/hash.rb', line 55

def store(field, value, options = {})
  if value.nil?
    delete field
  else
    conn.hset key, field, value
  end
end

#update(hash) ⇒ Object

Parameters:

  • hash (Hash)

    The pairs to update



104
105
106
107
# File 'lib/redpear/store/hash.rb', line 104

def update(hash)
  result = merge!(hash)
  Redis::Future === result ? result : to_hash
end

#valuesArray

Returns all values.

Returns:

  • (Array)

    all values



70
71
72
# File 'lib/redpear/store/hash.rb', line 70

def values
  conn.hvals key
end

#values_at(*fields) ⇒ Array

Returns values.

Parameters:

  • fields (multiple)

    The field to return

Returns:

  • (Array)

    values



98
99
100
# File 'lib/redpear/store/hash.rb', line 98

def values_at(*fields)
  conn.hmget key, *fields
end