Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove redis and mongo adapters.

Moving them to their own repositories.
  • Loading branch information...
commit ff7efcbe7f0fb6c8ecf50655ad1229965fc4c4af 1 parent 491afa8
@jnunemaker authored
View
80 lib/flipper/adapters/mongo.rb
@@ -1,80 +0,0 @@
-require 'set'
-require 'mongo'
-
-module Flipper
- module Adapters
- class Mongo
- def initialize(collection, id, options = {})
- @collection = collection
- @id = id
- @options = options
- @mongo_criteria = {:_id => @id}
- @mongo_options = {:upsert => true, :safe => true}
- end
-
- def read(key)
- read_key(key)
- end
-
- def write(key, value)
- update '$set' => {key => value}
- end
-
- def delete(key)
- update '$unset' => {key => 1}
- end
-
- def set_add(key, value)
- update '$addToSet' => {key => value}
- end
-
- def set_delete(key, value)
- update '$pull' => {key => value}
- end
-
- def set_members(key)
- read_key(key) { Set.new }.to_set
- end
-
- private
-
- def update(updates)
- @collection.update(@mongo_criteria, updates, @mongo_options)
- end
-
- def read_key(key, &block)
- load
-
- if block_given?
- @document.fetch(key, &block)
- else
- @document[key]
- end
- end
-
- def ttl
- @options.fetch(:ttl) { 0 }
- end
-
- def expired?
- return true if never_loaded?
- Time.now.to_i >= (@last_load_at + ttl)
- end
-
- def never_loaded?
- @last_load_at.nil?
- end
-
- def load
- if expired?
- @document = fresh_load
- end
- end
-
- def fresh_load
- @last_load_at = Time.now.to_i
- @collection.find_one(@mongo_criteria) || {}
- end
- end
- end
-end
View
36 lib/flipper/adapters/redis.rb
@@ -1,36 +0,0 @@
-require 'set'
-require 'redis'
-
-module Flipper
- module Adapters
- class Redis
- def initialize(client)
- @client = client
- end
-
- def read(key)
- @client.get key
- end
-
- def write(key, value)
- @client.set key, value
- end
-
- def delete(key)
- @client.del key
- end
-
- def set_add(key, value)
- @client.sadd(key, value)
- end
-
- def set_delete(key, value)
- @client.srem(key, value)
- end
-
- def set_members(key)
- @client.smembers(key).map { |member| member.to_i }.to_set
- end
- end
- end
-end
View
66 spec/flipper/adapters/mongo_spec.rb
@@ -1,66 +0,0 @@
-require 'helper'
-require 'flipper/adapters/mongo'
-require 'flipper/spec/shared_adapter_specs'
-
-describe Flipper::Adapters::Mongo do
- let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
- let(:oid) { BSON::ObjectId.new }
- let(:criteria) { {:_id => oid} }
-
- subject { Flipper::Adapters::Mongo.new(collection, oid) }
-
- before do
- collection.remove(criteria)
- end
-
- def read_key(key)
- if (doc = collection.find_one(criteria))
- value = doc[key]
-
- if value.is_a?(::Array)
- value = value.to_set
- end
-
- value
- end
- end
-
- def write_key(key, value)
- if value.is_a?(::Set)
- value = value.to_a
- end
-
- options = {:upsert => true}
- updates = {'$set' => {key => value}}
- collection.update criteria, updates, options
- end
-
- it_should_behave_like 'a flipper adapter'
-
- it "can cache document in process for a number of seconds" do
- options = {:ttl => 10}
- adapter = Flipper::Adapters::Mongo.new(collection, oid, options)
- adapter.write('foo', 'bar')
- now = Time.now
- Timecop.freeze(now)
-
- collection.should_receive(:find_one).with(:_id => oid)
- adapter.read('foo')
-
- adapter.read('foo')
- adapter.read('bar')
-
- Timecop.travel(3)
- adapter.read('foo')
-
- Timecop.travel(6)
- adapter.read('foo')
-
- collection.should_receive(:find_one).with(:_id => oid)
- Timecop.travel(1)
- adapter.read('foo')
-
- Timecop.travel(4)
- adapter.read('foo')
- end
-end
View
36 spec/flipper/adapters/redis_spec.rb
@@ -1,36 +0,0 @@
-require 'helper'
-require 'flipper/adapters/redis'
-require 'flipper/spec/shared_adapter_specs'
-
-describe Flipper::Adapters::Memory do
- let(:client) { Redis.new }
-
- subject { Flipper::Adapters::Redis.new(client) }
-
- before do
- client.flushdb
- end
-
- def read_key(key)
- client.get key
- rescue RuntimeError => e
- if e.message =~ /wrong kind of value/
- client.smembers(key).map { |member| member.to_i }.to_set
- else
- raise
- end
- end
-
- def write_key(key, value)
- case value
- when Array, Set
- value.each do |member|
- client.sadd key, member
- end
- else
- client.set key, value
- end
- end
-
- it_should_behave_like 'a flipper adapter'
-end
Please sign in to comment.
Something went wrong with that request. Please try again.