Class: Luca::Collection::RedisBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/luca/collection/redis_backend.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RedisBackend

Returns a new instance of RedisBackend.



27
28
29
30
31
32
33
34
# File 'lib/luca/collection/redis_backend.rb', line 27

def initialize options={}
  @namespace            = options[:namespace].to_s
  @redis                = options[:redis] || Luca::Collection::RedisBackend.default_redis
  @id_storage           = options[:id_storage] ||= "#{ @namespace }:ids"
  @required_attributes  = options[:required_attributes] || []

  validate_redis_connection
end

Instance Attribute Details

#id_storageObject

Returns the value of attribute id_storage.



21
22
23
# File 'lib/luca/collection/redis_backend.rb', line 21

def id_storage
  @id_storage
end

#namespaceObject

Returns the value of attribute namespace.



21
22
23
# File 'lib/luca/collection/redis_backend.rb', line 21

def namespace
  @namespace
end

#redisObject

Returns the value of attribute redis.



21
22
23
# File 'lib/luca/collection/redis_backend.rb', line 21

def redis
  @redis
end

#redis_databaseObject

Returns the value of attribute redis_database.



21
22
23
# File 'lib/luca/collection/redis_backend.rb', line 21

def redis_database
  @redis_database
end

#required_attributesObject

Returns the value of attribute required_attributes.



21
22
23
# File 'lib/luca/collection/redis_backend.rb', line 21

def required_attributes
  @required_attributes
end

Class Method Details

.default_redisObject



16
17
18
19
# File 'lib/luca/collection/redis_backend.rb', line 16

def self.default_redis
  require 'redis'
  @default_redis ||= Redis.new host: "localhost", port: 6379, db: 5
end

Instance Method Details

#clear!Object



74
75
76
77
78
# File 'lib/luca/collection/redis_backend.rb', line 74

def clear!
  return unless record_ids.length > 0
  redis.del( *record_ids )
  redis.del( id_storage ) 
end

#create(hash) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/luca/collection/redis_backend.rb', line 96

def create hash
  hash.symbolize_keys!

  if required_attributes.any? {|attribute| hash[attribute].nil?}
    return {success: false, error:"Missing required attributes."}
  end 

  next_id = hash[:id] ||= generate_id 

  serialized = JSON.generate(hash)
  response = redis.set "#{ namespace }/#{ next_id }", serialized 

  if response == "OK"
    redis.sadd id_storage, hash[:id]
    return {success: true, id: hash[:id], record: hash}
  else
    return {success: false, error:"Error adding record in data store."}
  end  
end

#destroy(hash) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/luca/collection/redis_backend.rb', line 116

def destroy hash
  hash.symbolize_keys!

  stored = redis.get("#{ namespace }/#{ hash[:id] }")  

  if stored
    redis.del "#{ namespace }/#{ hash[:id] }" 
    return {success: true}
  else
    return {success: false, error:"Could not find record with #{ hash[:id] }"}
  end
end

#generate_id(id_base = nil) ⇒ Object



80
81
82
83
# File 'lib/luca/collection/redis_backend.rb', line 80

def generate_id id_base=nil
  id_base ||= id_storage
  redis.incr("next:#{ id_base }:id")
end

#index(search = nil) ⇒ Object



85
86
87
88
89
# File 'lib/luca/collection/redis_backend.rb', line 85

def index search=nil
  return [] unless record_ids.length > 0
  
  redis.mget( *record_ids ).map {|serialized| JSON.parse(serialized) rescue nil }.compact
end

#record_idsObject



70
71
72
# File 'lib/luca/collection/redis_backend.rb', line 70

def record_ids
  Array(redis.smembers( id_storage )).map {|id| "#{ namespace }/#{ id }"}
end

#show(id) ⇒ Object



91
92
93
94
# File 'lib/luca/collection/redis_backend.rb', line 91

def show id
  record = redis.get("#{ namespace }/#{ id }")
  JSON.parse( record ) if record
end

#sync(method, hash = {}, options = {}) ⇒ Object Also known as: backbone_sync



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/luca/collection/redis_backend.rb', line 36

def sync method, hash={}, options={}
  if method == "read" and !hash[:id].nil?
    return index()
  end

  if method == "read" and !hash[:id].nil?
    return show( hash[:id] )
  end

  if method == "create"
    return create( hash )
  end

  if method == "update"
    return update( hash )
  end

  if method == "delete"
    return destroy( hash )
  end
end

#update(hash) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/luca/collection/redis_backend.rb', line 129

def update hash
  hash.symbolize_keys!

  if required_attributes.any? {|attribute| hash[attribute].nil?}
    return {success: false, error:"Missing required attributes."}
  end 

  stored = redis.get("#{ namespace }/#{ hash[:id] }")  

  if stored.nil?
    return {success: false, error:"Could not find record with #{ hash[:id] }"}
  end

  serialized = JSON.generate(hash)
  result = redis.set "#{ namespace }/#{ hash[:id] }", serialized

  if result == "OK"
    return {success: true}
  else
    return {success: false, error:"Error adding record in data store."}
  end        
end

#validate_redis_connectionObject



60
61
62
63
64
65
66
67
68
# File 'lib/luca/collection/redis_backend.rb', line 60

def validate_redis_connection
  unless @redis

  end

  unless @redis.respond_to?(:get) and @redis.respond_to?(:incr) and @redis.respond_to?(:mget)
    throw "Must specify a valid redis instance."      
  end
end