Class: Blendris::RedisReference

Inherits:
RedisReferenceBase show all
Defined in:
lib/blendris/reference.rb

Overview

RedisReference is a wrapper to a Redis string value and serves as a pointer to another blendris object.

Instance Attribute Summary

Attributes inherited from RedisNode

#key

Instance Method Summary collapse

Methods inherited from RedisReferenceBase

#apply_reverse_add, #apply_reverse_delete, cast_from_redis, cast_to_redis, #initialize

Methods included from RedisAccessor

#generate_key, #in_temporary_set, redis, #redis

Methods included from Utils

#blank, #camelize, #constantize, #pairify, #sanitize_key

Methods inherited from RedisNode

#clear, #exists?, #initialize, #notify_changed, #rename, #type

Constructor Details

This class inherits a constructor from Blendris::RedisReferenceBase

Instance Method Details

#assign_ref(value) ⇒ Object



38
39
40
# File 'lib/blendris/reference.rb', line 38

def assign_ref(value)
  self.set value
end

#getObject



34
35
36
# File 'lib/blendris/reference.rb', line 34

def get
  self.class.cast_from_redis ref.get
end

#refObject



8
9
10
# File 'lib/blendris/reference.rb', line 8

def ref
  @ref ||= RedisString.new(@key)
end

#references(value) ⇒ Object



46
47
48
49
50
51
# File 'lib/blendris/reference.rb', line 46

def references(value)
  refval = ref.get

  return true if refval.nil? && value.nil?
  return refval == value.key
end

#remove_ref(value) ⇒ Object



42
43
44
# File 'lib/blendris/reference.rb', line 42

def remove_ref(value)
  self.set nil
end

#set(obj) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/blendris/reference.rb', line 12

def set(obj)
  old_obj = self.get if @reverse
  modified = false
  refkey = self.class.cast_to_redis(obj, @options)

  if refkey == nil
    ref.set nil
    modified = true
  elsif refkey != ref.get
    ref.set refkey
    apply_reverse_add obj
    modified = true
  end

  if modified
    apply_reverse_delete(old_obj)
    notify_changed
  end

  obj
end