Browse files

Added mongo adapter and mongo with ttl adapter.

  • Loading branch information...
1 parent 67478d3 commit b22c910571404c511514d060154cff37b31241af @jnunemaker committed Aug 2, 2012
View
18 .gitignore
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+log
View
20 Gemfile
@@ -0,0 +1,20 @@
+source 'https://rubygems.org'
+gemspec
+
+gem 'rake'
+gem 'flipper', :path => '/Users/jnunemaker/projects/flipper'
+
+group(:guard) do
+ gem 'guard'
+ gem 'guard-rspec'
+ gem 'guard-bundler'
+ gem 'growl'
+end
+
+group(:test) do
+ gem 'rspec'
+ gem 'timecop'
+ gem 'bson_ext'
+ gem 'mongo'
+end
+
View
18 Guardfile
@@ -0,0 +1,18 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'bundler' do
+ watch('Gemfile')
+ watch(/^.+\.gemspec/)
+end
+
+rspec_options = {
+ :all_after_pass => false,
+ :all_on_start => false,
+}
+
+guard 'rspec', rspec_options do
+ watch(%r{^spec/.+_spec\.rb$}) { "spec" }
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
+ watch('spec/helper.rb') { "spec" }
+end
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 John Nunemaker
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
25 README.md
@@ -0,0 +1,25 @@
+# Flipper Mongo
+
+A mongo adapter for [Flipper](https://github.com/jnunemaker/flipper), the feature flipping gems.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'flipper-mongo'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself with:
+
+ $ gem install flipper-mongo
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
2 Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
View
18 flipper-mongo.gemspec
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/flipper/adapters/mongo/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["John Nunemaker"]
+ gem.email = ["nunemaker@gmail.com"]
+ gem.description = %q{Mongo adapter for Flipper}
+ gem.summary = %q{Mongo adapter for Flipper}
+ gem.homepage = "http://jnunemaker.github.com/flipper-mongo"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "flipper-mongo"
+ gem.require_paths = ["lib"]
+ gem.version = Flipper::Adapters::Mongo::VERSION
+ gem.add_dependency 'flipper', '~> 0.1.0'
+end
View
1 lib/flipper-mongo.rb
@@ -0,0 +1 @@
+require 'flipper/adapters/mongo'
View
59 lib/flipper/adapters/mongo.rb
@@ -0,0 +1,59 @@
+require 'set'
+require 'mongo'
+
+module Flipper
+ module Adapters
+ class Mongo
+ def initialize(collection, id)
+ @collection = collection
+ @id = id
+ @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 load
+ @document = @collection.find_one(@mongo_criteria) || {}
+ end
+ end
+ end
+end
View
7 lib/flipper/adapters/mongo/version.rb
@@ -0,0 +1,7 @@
+module Flipper
+ module Adapters
+ class Mongo
+ VERSION = "0.1"
+ end
+ end
+end
View
42 lib/flipper/adapters/mongo_with_ttl.rb
@@ -0,0 +1,42 @@
+require 'set'
+require 'mongo'
+require 'flipper/adapters/mongo'
+
+module Flipper
+ module Adapters
+ class MongoWithTTL < Mongo
+
+ def initialize(collection, id, options = {})
+ super collection, id
+ @options = options
+ end
+
+ private
+
+ # Override Mongo adapters load
+ 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
+
+ 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
+ end
+ end
+end
View
39 spec/flipper/adapters/mongo_spec.rb
@@ -0,0 +1,39 @@
+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'
+end
View
44 spec/flipper/adapters/mongo_with_ttl_spec.rb
@@ -0,0 +1,44 @@
+require 'helper'
+require 'flipper/adapters/mongo_with_ttl'
+require 'flipper/spec/shared_adapter_specs'
+
+describe Flipper::Adapters::MongoWithTTL do
+ let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
+ let(:oid) { BSON::ObjectId.new }
+ let(:criteria) { {:_id => oid} }
+
+ subject { Flipper::Adapters::MongoWithTTL.new(collection, oid) }
+
+ before do
+ collection.remove(criteria)
+ 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::MongoWithTTL.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
29 spec/helper.rb
@@ -0,0 +1,29 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+
+require 'pathname'
+require 'logger'
+
+root_path = Pathname(__FILE__).dirname.join('..').expand_path
+lib_path = root_path.join('lib')
+log_path = root_path.join('log')
+log_path.mkpath
+
+require 'rubygems'
+require 'bundler'
+
+Bundler.require(:default, :test)
+
+require 'flipper-mongo'
+
+Logger.new(log_path.join('test.log'))
+
+require 'support/accessor_helpers'
+
+RSpec.configure do |config|
+ config.filter_run :focused => true
+ config.alias_example_to :fit, :focused => true
+ config.alias_example_to :xit, :pending => true
+ config.run_all_when_everything_filtered = true
+
+ config.include AccessorHelpers
+end
View
23 spec/support/accessor_helpers.rb
@@ -0,0 +1,23 @@
+module AccessorHelpers
+ 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
+end

0 comments on commit b22c910

Please sign in to comment.