Class: Authorize::Redis::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/authorize/redis/factory.rb

Overview

A Factory is designed to help build relevant test fixtures in the context of a test. In order to preserve a high signal-to-noise ratio in the test, a factory needs to concisely build fixtures even at the expense of supporting cool features. As a result, index management and references to other values are the responsibility of the programmer.

Direct Known Subclasses

Graph::Factory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db = Base.db) ⇒ Factory

Returns a new instance of Factory.



15
16
17
18
# File 'lib/authorize/redis/factory.rb', line 15

def initialize(db = Base.db)
  @namespace = nil
  @db = db
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



7
8
9
# File 'lib/authorize/redis/factory.rb', line 7

def db
  @db
end

Class Method Details

.build(ns = nil, &block) ⇒ Object



9
10
11
12
13
# File 'lib/authorize/redis/factory.rb', line 9

def self.build(ns = nil, &block)
  self.new.tap do |f|
    f.namespace(ns, &block) if ns
  end
end

Instance Method Details

#array(name, value = []) ⇒ Object



51
52
53
54
55
56
# File 'lib/authorize/redis/factory.rb', line 51

def array(name, value = [])
  key = subordinate_key(name)
  value.each {|v| db.rpush(key, v)}
  namespace(name){yield} if block_given?
  Redis::Array.load(key)
end

#hash(name, value = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/authorize/redis/factory.rb', line 37

def hash(name, value = {})
  key = subordinate_key(name)
  value.each {|k, v| db.hset(key, k, v)}
  namespace(name){yield} if block_given?
  Redis::Hash.load(key)
end

#namespace(name, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/authorize/redis/factory.rb', line 20

def namespace(name, &block)
  @old_namespace, @namespace = @namespace, subordinate_key(name)
  if block_given?
    self.instance_eval(&block)
    @namespace = @old_namespace
  else
    self
  end
end

#set(name, value = ::Set[]) ⇒ Object



44
45
46
47
48
49
# File 'lib/authorize/redis/factory.rb', line 44

def set(name, value = ::Set[])
  key = subordinate_key(name)
  value.each {|v| db.sadd(key, v)}
  namespace(name){yield} if block_given?
  Redis::Set.load(key)
end

#string(name, value = "") ⇒ Object



30
31
32
33
34
35
# File 'lib/authorize/redis/factory.rb', line 30

def string(name, value = "")
  key = subordinate_key(name)
  db.set(key, value)
  namespace(name){yield} if block_given?
  Redis::String.load(key)
end