lib/flapjack/data/entity.rb in flapjack-0.6.29 vs lib/flapjack/data/entity.rb in flapjack-0.6.30
- old
+ new
@@ -14,16 +14,18 @@
k =~ /^entity_id:(.+)$/; entity_name = $1
self.new(:name => entity_name, :id => redis.get("entity_id:#{entity_name}").to_i, :redis => redis)
}
end
+ # NB: should probably be called in the context of a Redis multi block; not doing so
+ # here as calling classes may well be adding/updating multiple records in the one
+ # operation
def self.add(entity, options = {})
raise "Redis connection not set" unless redis = options[:redis]
raise "Entity name not provided" unless entity['name'] && !entity['name'].empty?
if entity['id']
- redis.multi
existing_name = redis.hget("entity:#{entity['id']}", 'name')
redis.del("entity_id:#{existing_name}") unless existing_name == entity['name']
redis.set("entity_id:#{entity['name']}", entity['id'])
redis.hset("entity:#{entity['id']}", 'name', entity['name'])
@@ -31,10 +33,9 @@
if entity['contacts'] && entity['contacts'].respond_to?(:each)
entity['contacts'].each {|contact|
redis.sadd("contacts_for:#{entity['id']}", contact)
}
end
- redis.exec
else
# empty string is the redis equivalent of a Ruby nil, i.e. key with
# no value
redis.set("entity_id:#{entity['name']}", '')
end