Module: Blendris::RedisAccessor

Includes:
Utils
Included in:
Model, Model, RedisNode, RedisReferenceBase
Defined in:
lib/blendris/accessor.rb

Overview

This module serves as a gateway to the Redis library. Any object that needs to access Redis directly should include it.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Class Method Details

.redisObject



56
57
58
# File 'lib/blendris/accessor.rb', line 56

def self.redis
  Blendris.redis
end

Instance Method Details

#generate_key(klass, values) ⇒ Object

Generate a key for the given model class with the given values list. This is used to determine a new object’s key in the Model.create method.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blendris/accessor.rb', line 62

def generate_key(klass, values)
  value_index = 0

  klass.local_parameters.map do |symbol|
    case symbol

    when String then symbol

    when Symbol
      value = values[value_index]
      value_index += 1
      raise ArgumentError.new("#{self.name} created without #{symbol}") unless value

      options = klass.redis_symbols[symbol.to_s]
      raise ArgumentError.new("#{self.name} is missing its #{symbol}") unless options

      subklass = options[:type]
      raise ArgumentError.new("#{symbol} (#{subklass.name}) cannot be used in the key") unless subklass.respond_to? :cast_to_redis

      subklass.cast_to_redis value, options

    else
      raise TypeError.new("only strings and symbols allowed in key definition for #{klass.name} (#{symbol.class.name})")

    end
  end.map do |segment|
    sanitize_key segment
  end.compact.join(":")
end

#in_temporary_set(*contents) ⇒ Object

Build a new temporary set with the given contents, yielding it to the passed block. After the block exits, destroy the temporary set.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/blendris/accessor.rb', line 94

def in_temporary_set(*contents)
  index = RedisInteger.new("blendris:temporary:index").increment

  temporary_set = RedisSet.new("blendris:temporary:set:#{index}")
  temporary_set << contents

  begin
    yield temporary_set
  ensure
    temporary_set.clear
  end

  self
end

#redisObject



52
53
54
# File 'lib/blendris/accessor.rb', line 52

def redis
  Blendris.redis
end