Module: RedisHash::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/redis_hash/concerns/accessors.rb

Instance Method Summary collapse

Instance Method Details

#[](field) ⇒ Object



13
14
15
# File 'lib/redis_hash/concerns/accessors.rb', line 13

def [](field)
  hget(redis_key, field) || default(field)
end

#fetch(field, default = nil) ⇒ Object

Raises:

  • (KeyError)


17
18
19
20
21
22
23
24
# File 'lib/redis_hash/concerns/accessors.rb', line 17

def fetch(field, default = nil)
  value = self[field]
  return value if value.present?
  return yield(field) if block_given?
  return default unless default.nil?

  raise KeyError, "key not found: \"#{field}\""
end

#keysObject



26
27
28
# File 'lib/redis_hash/concerns/accessors.rb', line 26

def keys
  hkeys(redis_key)
end

#lengthObject Also known as: size



30
31
32
# File 'lib/redis_hash/concerns/accessors.rb', line 30

def length
  hlen(redis_key)
end

#valuesObject



35
36
37
# File 'lib/redis_hash/concerns/accessors.rb', line 35

def values
  hvals(redis_key)
end

#values_at(*fields) ⇒ Object



39
40
41
# File 'lib/redis_hash/concerns/accessors.rb', line 39

def values_at(*fields)
  hmget(*fields.flatten.unshift(redis_key))
end