Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Compatibility class in between AS::Cache::Store and Bin::Store …

…that makes it so the API is the same for Rails 2 and 3.
  • Loading branch information...
commit 413bb85df2cede659d0a120b5dd67a77078d4d36 1 parent 7a67486
@jnunemaker authored
View
12 Rakefile
@@ -3,8 +3,16 @@ require 'rake'
require 'spec/rake/spectask'
require File.expand_path('../lib/bin/version', __FILE__)
-Spec::Rake::SpecTask.new do |t|
- t.ruby_opts << '-rubygems'
+namespace :spec do
+ Spec::Rake::SpecTask.new(:all) do |t|
+ t.ruby_opts << '-rubygems'
+ t.verbose = true
+ end
+end
+
+task :spec do
+ sh "ACTIVE_SUPPORT_VERSION='<= 2.3.8' rake spec:all"
+ sh "ACTIVE_SUPPORT_VERSION='>= 3.0.0.beta3' rake spec:all"
end
task :default => :spec
View
6 lib/bin.rb
@@ -1,7 +1,9 @@
# encoding: UTF-8
-require 'active_support'
+require 'active_support/all'
+require 'active_support/version'
require 'mongo'
module Bin
- autoload :Store, 'bin/store'
+ autoload :Compatibility, 'bin/compatibility'
+ autoload :Store, 'bin/store'
end
View
40 lib/bin/compatibility.rb
@@ -0,0 +1,40 @@
+module Bin
+ class Compatibility < ActiveSupport::Cache::Store
+ def increment(key, amount=1)
+ yield
+ end
+
+ def decrement(key, amount=1)
+ yield
+ end
+ end
+
+ if ActiveSupport::VERSION::STRING < '3'
+ class Compatibility
+ def write(key, value, options=nil, &block)
+ super(key, value, options)
+ yield
+ end
+
+ def read(key, options=nil, &block)
+ super
+ yield
+ end
+
+ def delete(key, options=nil, &block)
+ super
+ yield
+ end
+
+ def delete_matched(matcher, options=nil, &block)
+ super
+ yield
+ end
+
+ def exist?(key, options=nil, &block)
+ super
+ yield
+ end
+ end
+ end
+end
View
51 lib/bin/store.rb
@@ -1,6 +1,6 @@
# encoding: UTF-8
module Bin
- class Store < ActiveSupport::Cache::Store
+ class Store < Compatibility
attr_reader :collection
def initialize(collection)
@@ -8,46 +8,55 @@ def initialize(collection)
end
def write(key, value, options=nil)
- super
- doc = {:_id => key, :value => value}
- if options && options.key?(:expires_in)
- doc[:expires_at] = Time.now.utc + options[:expires_in]
+ super do
+ doc = {:_id => key, :value => value}
+ if options && options.key?(:expires_in)
+ doc[:expires_at] = Time.now.utc + options[:expires_in]
+ end
+ collection.save(doc)
end
- collection.save(doc)
end
def read(key, options=nil)
- super
- if doc = collection.find_one(:_id => key)
- doc['value'] if fresh?(doc)
+ super do
+ if doc = collection.find_one(:_id => key)
+ doc['value'] if fresh?(doc)
+ end
end
end
def delete(key, options=nil)
- super
- collection.remove(:_id => key)
+ super do
+ collection.remove(:_id => key)
+ end
end
def delete_matched(matcher, options=nil)
- super
- collection.remove(:_id => matcher)
+ super do
+ collection.remove(:_id => matcher)
+ end
end
def exist?(key, options=nil)
- super
- collection.find(:_id => key).count > 0
- end
-
- def clear
- collection.remove
+ super do
+ collection.find(:_id => key).count > 0
+ end
end
def increment(key, amount=1)
- collection.update({:_id => key}, {'$inc' => {:value => amount}}, :upsert => true)
+ super do
+ collection.update({:_id => key}, {'$inc' => {:value => amount}}, :upsert => true)
+ end
end
def decrement(key, amount=1)
- collection.update({:_id => key}, {'$inc' => {:value => -amount.abs}}, :upsert => true)
+ super do
+ collection.update({:_id => key}, {'$inc' => {:value => -amount.abs}}, :upsert => true)
+ end
+ end
+
+ def clear
+ collection.remove
end
def stats
View
6 spec/helper.rb
@@ -1,6 +1,6 @@
-$:.unshift(File.expand_path('../../lib', __FILE__))
+gem 'activesupport', ENV['ACTIVE_SUPPORT_VERSION']
-gem 'activesupport', '<= 2.3.8'
+$:.unshift(File.expand_path('../../lib', __FILE__))
require 'bin'
require 'spec'
@@ -8,3 +8,5 @@
connection = Mongo::Connection.new
DB = connection.db('bin-store-test')
+
+puts "\nRunning specs against Active Support version: #{ActiveSupport::VERSION::STRING}\n"
Please sign in to comment.
Something went wrong with that request. Please try again.