Class: Blendris::RedisReferenceSet

Inherits:
RedisReferenceBase show all
Includes:
Enumerable
Defined in:
lib/blendris/reference_set.rb

Overview

RedisReferenceSet is a wrapper to a Redis set value and serves as a pointer to multiple other blendris objects.

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

#<<(*objs) ⇒ Object



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

def <<(*objs)
  objs.flatten!
  objs.compact!

  objs.each do |obj|
    if refkey = self.class.cast_to_redis(obj, @options)
      refs << refkey
      apply_reverse_add obj
    end
  end

  notify_changed if objs.count > 0

  self
end

#assign_ref(*values) ⇒ Object



77
78
79
# File 'lib/blendris/reference_set.rb', line 77

def assign_ref(*values)
  self << values
end

#countObject



89
90
91
# File 'lib/blendris/reference_set.rb', line 89

def count
  refs.count
end

#delete(obj) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/blendris/reference_set.rb', line 52

def delete(obj)
  if refkey = self.class.cast_to_redis(obj, @options)
    deleted = refs.delete(refkey)
    apply_reverse_delete(obj) if deleted
    notify_changed
    deleted
  end
end

#eachObject



65
66
67
68
69
# File 'lib/blendris/reference_set.rb', line 65

def each
  redis.smembers(key).each do |refkey|
    yield self.class.cast_from_redis(refkey, @options)
  end
end

#getObject



61
62
63
# File 'lib/blendris/reference_set.rb', line 61

def get
  self
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/blendris/reference_set.rb', line 71

def include?(obj)
  refkey = self.class.cast_to_redis(obj, @options)

  refs.include? refkey
end

#references(value) ⇒ Object



85
86
87
# File 'lib/blendris/reference_set.rb', line 85

def references(value)
  self.include? value
end

#refsObject



10
11
12
# File 'lib/blendris/reference_set.rb', line 10

def refs
  @refs ||= RedisSet.new(@key)
end

#remove_ref(value) ⇒ Object



81
82
83
# File 'lib/blendris/reference_set.rb', line 81

def remove_ref(value)
  self.delete value
end

#set(*objs) ⇒ Object



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

def set(*objs)
  objs.flatten!
  objs.compact!

  # Delete reference keys that were removed.
  self.each do |obj|
    unless objs.include? obj
      apply_reverse_delete obj
    end
  end

  # Clear the current set.
  self.refs.clear

  # Add the new objects to the set.
  self << objs

  self
ensure
  notify_changed
end