Class: Blendris::RedisReferenceBase

Inherits:
Object
  • Object
show all
Extended by:
RedisAccessor
Includes:
RedisNode
Defined in:
lib/blendris/reference_base.rb

Overview

RedisReferenceBase holds the methods that are common to RedisReference objects and RedisReferenceSet objects.

Direct Known Subclasses

RedisReference, RedisReferenceSet

Instance Attribute Summary

Attributes included from RedisNode

#key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RedisAccessor

database=, flushdb, generate_key, in_temporary_set, redis, redis

Methods included from Utils

#blank, #camelize, #constantize, #sanitize_key

Methods included from RedisNode

#clear, #exists?, #get, #notify_changed, #rename, #set, #type

Constructor Details

#initialize(key, options = {}) ⇒ RedisReferenceBase

Returns a new instance of RedisReferenceBase.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/blendris/reference_base.rb', line 11

def initialize(key, options = {})
  @model = options[:model]
  @key = sanitize_key(key)
  @reverse = options[:reverse]
  @options = options
  @on_change = options[:on_change]

  @klass = options[:class] || Model
  @klass = constantize(camelize @klass) if @klass.kind_of? String

  unless @klass.ancestors.include? Model
    raise ArgumentError.new("#{klass.name} is not a model")
  end
end

Class Method Details

.cast_from_redis(refkey, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/blendris/reference_base.rb', line 54

def self.cast_from_redis(refkey, options = {})
  expect = options[:class] || Model
  expect = constantize(expect) if expect.kind_of? String
  expect = Model unless expect.ancestors.include? Model

  klass = constantize(redis.get(refkey)) if refkey

  if klass == nil
    nil
  elsif klass.ancestors.include? expect
    klass.new refkey
  else
    raise TypeError.new("#{klass.name} is not a #{expect.name}")
  end
end

.cast_to_redis(obj, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/blendris/reference_base.rb', line 40

def self.cast_to_redis(obj, options = {})
  expect = options[:class] || Model
  expect = constantize(expect) if expect.kind_of? String
  expect = Model unless expect.ancestors.include? Model

  if obj == nil
    nil
  elsif obj.kind_of? expect
    obj.key
  else
    raise TypeError.new("#{obj.class.name} is not a #{expect.name}")
  end
end

Instance Method Details

#apply_reverse_add(value) ⇒ Object



26
27
28
29
30
31
# File 'lib/blendris/reference_base.rb', line 26

def apply_reverse_add(value)
  if @reverse && value
    reverse = value.redis_symbol(@reverse)
    reverse.assign_ref(@model) if !reverse.references @model
  end
end

#apply_reverse_delete(value) ⇒ Object



33
34
35
36
37
38
# File 'lib/blendris/reference_base.rb', line 33

def apply_reverse_delete(value)
  if @reverse && value
    reverse = value.redis_symbol(@reverse)
    reverse.remove_ref(@model) if reverse.references @model
  end
end