Permalink
Browse files

Updated for rails 3.1.

Now supports namespacing. Also, nested inside of AS::Cache so lookup store stuff works.

Top level Bin module just has version.
  • Loading branch information...
1 parent eb0f72d commit f5c7fe3f6ad857f8886b70cbb0daed8f4be20f3e @jnunemaker committed Sep 17, 2011
Showing with 359 additions and 410 deletions.
  1. +10 −0 Gemfile
  2. +40 −0 Gemfile.lock
  3. +4 −42 Rakefile
  4. +12 −9 bin.gemspec
  5. +82 −0 lib/active_support/cache/bin.rb
  6. +1 −10 lib/bin.rb
  7. +0 −41 lib/bin/compatibility.rb
  8. +0 −84 lib/bin/store.rb
  9. +0 −1 lib/bin/version.rb
  10. +0 −210 spec/bin/store_spec.rb
  11. +195 −7 spec/bin_spec.rb
  12. +14 −4 spec/helper.rb
  13. +1 −2 spec/spec.opts
View
10 Gemfile
@@ -0,0 +1,10 @@
+source "http://rubygems.org"
+
+gem 'bson_ext', :require => false
+
+gemspec
+
+gem 'i18n'
+gem 'rake'
+gem 'rspec'
+gem 'log_buddy'
View
@@ -0,0 +1,40 @@
+PATH
+ remote: .
+ specs:
+ bin (0.6.3)
+ activesupport (~> 3.1.0)
+ mongo (~> 1.3.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.1.0)
+ multi_json (~> 1.0)
+ bson (1.3.1)
+ bson_ext (1.3.1)
+ diff-lcs (1.1.3)
+ i18n (0.6.0)
+ log_buddy (0.6.0)
+ mongo (1.3.1)
+ bson (>= 1.3.1)
+ multi_json (1.0.3)
+ rake (0.9.2)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bin!
+ bson_ext
+ i18n
+ log_buddy
+ rake
+ rspec
View
@@ -1,45 +1,7 @@
-require 'rubygems'
-require 'rake'
-require 'spec/rake/spectask'
-require File.expand_path('../lib/bin/version', __FILE__)
+require 'bundler'
+Bundler::GemHelper.install_tasks
-namespace :spec do
- Spec::Rake::SpecTask.new(:all) do |t|
- t.ruby_opts << '-rubygems'
- t.verbose = true
- end
-
- task :as2 do
- sh 'ACTIVE_SUPPORT_VERSION="<= 2.3.8" rake spec:all'
- end
-
- task :as3 do
- sh 'ACTIVE_SUPPORT_VERSION=">= 3.0.0.beta3" rake spec:all'
- end
-end
-
-desc 'Runs all specs against Active Support 2 and 3'
-task :spec do
- Rake::Task['spec:as2'].invoke
- Rake::Task['spec:as3'].invoke
-end
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
task :default => :spec
-
-desc 'Builds the gem'
-task :build do
- sh "gem build bin.gemspec"
-end
-
-desc 'Builds and installs the gem'
-task :install => :build do
- sh "gem install bin-#{Bin::Version}"
-end
-
-desc 'Tags version, pushes to remote, and pushes gem'
-task :release => :build do
- sh "git tag v#{Bin::Version}"
- sh "git push origin master"
- sh "git push origin v#{Bin::Version}"
- sh "gem push bin-#{Bin::Version}.gem"
-end
View
@@ -3,16 +3,19 @@ require File.expand_path('../lib/bin/version', __FILE__)
Gem::Specification.new do |s|
s.name = 'bin'
- s.homepage = 'http://github.com/jnunemaker/bin'
- s.summary = 'ActiveSupport MongoDB Cache store.'
- s.require_path = 'lib'
- s.authors = ['John Nunemaker']
- s.email = ['nunemaker@gmail.com']
s.version = Bin::Version
s.platform = Gem::Platform::RUBY
- s.files = Dir.glob("{lib,spec}/**/*") + %w[LICENSE Rakefile README.rdoc]
+ s.authors = ['John Nunemaker']
+ s.email = ['nunemaker@gmail.com']
+ s.homepage = 'http://github.com/jnunemaker/bin'
+ s.summary = 'ActiveSupport MongoDB Cache store.'
+ s.description = 'ActiveSupport MongoDB Cache store.'
+
+ s.add_dependency 'mongo', '~> 1.3.0'
+ s.add_dependency 'activesupport', '~> 3.1.0'
- s.add_dependency 'mongo', '~> 1.0.1'
- s.add_dependency 'activesupport', '>= 2.3'
- s.add_development_dependency 'rspec', '~> 1.3.0'
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
end
@@ -0,0 +1,82 @@
+require 'mongo'
+
+module ActiveSupport
+ module Cache
+ class Bin < Store
+ attr_reader :collection, :options
+
+ def initialize(collection, options={})
+ @collection, @options = collection, options
+ end
+
+ def expires_in
+ @expires_in ||= options[:expires_in] || 1.year
+ end
+
+ def delete_matched(matcher, options=nil)
+ collection.remove(:_id => matcher)
+ end
+
+ def increment(key, amount=1, options=nil)
+ counter_key_upsert(:increment, key, amount, options)
+ end
+
+ def decrement(key, amount=1, options=nil)
+ counter_key_upsert(:decrement, key, -amount.abs, options)
+ end
+
+ def clear
+ collection.remove
+ end
+
+ def stats
+ collection.stats
+ end
+
+ private
+ def counter_key_upsert(action, key, amount, options)
+ options = merged_options(options)
+ instrument(action, key, :amount => amount) do
+ key = namespaced_key(key, options)
+ collection.update(
+ {:_id => key}, {
+ '$inc' => {:value => amount},
+ '$set' => {
+ :expires_at => Time.now.utc + 1.year,
+ :raw => true
+ },
+ }, :upsert => true)
+ end
+ end
+
+ def deserialize_doc(doc)
+ return nil if doc.nil?
+
+ entry = doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s)
+ entry.is_a?(Entry) ? entry : Entry.new(entry)
+ end
+
+ def read_entry(key, options)
+ query = {:_id => key, :expires_at => {'$gt' => Time.now.utc}}
+ deserialize_doc(collection.find_one(query))
+ end
+
+ def write_entry(key, entry, options)
+ expires = Time.now.utc + options.fetch(:expires_in, expires_in)
+ value = options[:raw] ? entry.value : BSON::Binary.new(Marshal.dump(entry.value))
+ query = {:_id => key}
+ updates = {'$set' => {
+ :value => value,
+ :expires_at => expires,
+ :raw => options[:raw],
+ }}
+
+ collection.update(query, updates, :upsert => true)
+ end
+
+ def delete_entry(key, options)
+ collection.remove(:_id => key)
+ end
+ end
+ end
+end
View
@@ -1,10 +1 @@
-# encoding: UTF-8
-require 'active_support/all'
-require 'active_support/version'
-require 'mongo'
-
-module Bin
- autoload :Compatibility, 'bin/compatibility'
- autoload :Store, 'bin/store'
- autoload :Version, 'bin/version'
-end
+require 'active_support/cache/bin'
View
@@ -1,41 +0,0 @@
-# encoding: UTF-8
-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
@@ -1,84 +0,0 @@
-# encoding: UTF-8
-module Bin
- class Store < Compatibility
- attr_reader :collection, :options
-
- def initialize(collection, options={})
- @collection, @options = collection, options
- end
-
- def expires_in
- @expires_in ||= options[:expires_in] || 1.year
- end
-
- def write(key, value, options={})
- key = key.to_s
- super do
- expires = Time.now.utc + ((options && options[:expires_in]) || expires_in)
- raw = !!options[:raw]
- value = raw ? value : BSON::Binary.new(Marshal.dump(value))
- doc = {:_id => key, :value => value, :expires_at => expires, :raw => raw}
- collection.save(doc)
- end
- end
-
- def read(key, options=nil)
- super do
- if doc = collection.find_one(:_id => key.to_s, :expires_at => {'$gt' => Time.now.utc})
- doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s)
- end
- end
- end
-
- def delete(key, options=nil)
- super do
- collection.remove(:_id => key.to_s)
- end
- end
-
- def delete_matched(matcher, options=nil)
- super do
- collection.remove(:_id => matcher)
- end
- end
-
- def exist?(key, options=nil)
- super do
- !read(key, options).nil?
- end
- end
-
- def increment(key, amount=1)
- super do
- counter_key_upsert(key, amount)
- end
- end
-
- def decrement(key, amount=1)
- super do
- counter_key_upsert(key, -amount.abs)
- end
- end
-
- def clear
- collection.remove
- end
-
- def stats
- collection.stats
- end
-
- private
- def counter_key_upsert(key, amount)
- key = key.to_s
- collection.update(
- {:_id => key}, {
- '$inc' => {:value => amount},
- '$set' => {
- :expires_at => Time.now.utc + 1.year,
- :raw => true
- },
- }, :upsert => true)
- end
- end
-end
View
@@ -1,4 +1,3 @@
-# encoding: UTF-8
module Bin
Version = '0.6.3'
end
Oops, something went wrong.

0 comments on commit f5c7fe3

Please sign in to comment.