Module: RedisHash::Insertions

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

Instance Method Summary collapse

Instance Method Details

#merge!(other_hash) ⇒ Object Also known as: update



13
14
15
16
17
18
# File 'lib/redis_hash/concerns/insertions.rb', line 13

def merge!(other_hash)
  assert_keys_allowed(other_hash.keys)
  run_callbacks(:insertion) { hmset(*other_hash.to_a.unshift(redis_key)) }

  self
end

#setnx(field, value) ⇒ Object



38
39
40
41
42
# File 'lib/redis_hash/concerns/insertions.rb', line 38

def setnx(field, value)
  setnx!(field, value)
rescue RedisHash::AlreadyDefinedError
  false
end

#setnx!(field, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/redis_hash/concerns/insertions.rb', line 27

def setnx!(field, value)
  assert_keys_allowed(field)
  run_callbacks(:insertion) do
    success = hsetnx(redis_key, field, value)

    raise RedisHash::AlreadyDefinedError, "#{field} already defined" unless success
  end

  true
end

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



21
22
23
24
# File 'lib/redis_hash/concerns/insertions.rb', line 21

def store(field, value)
  assert_keys_allowed(field)
  run_callbacks(:insertion) { hset(redis_key, field, value) }
end