Class: Blendris::RedisReferenceBase

Inherits:
RedisNode
  • Object
show all
Extended by:
RedisAccessor
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 inherited from RedisNode

#key

Class Method Summary collapse

Instance Method Summary collapse

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?, #get, #notify_changed, #rename, #set, #type

Constructor Details

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

Returns a new instance of RedisReferenceBase.



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

def initialize(key, options = {})
  super key, options

  @model = options[:model]
  @reverse = options[:reverse]

  @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



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

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



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

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



24
25
26
27
28
29
# File 'lib/blendris/reference_base.rb', line 24

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

#apply_reverse_delete(value) ⇒ Object



31
32
33
34
35
36
# File 'lib/blendris/reference_base.rb', line 31

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