mongoid_store 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.DS_Store ADDED
Binary file
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', "~> 3.0"
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid_store (0.0.1)
5
+ activesupport (~> 3.2)
6
+ mongoid (~> 3.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.8)
12
+ activesupport (= 3.2.8)
13
+ builder (~> 3.0.0)
14
+ activesupport (3.2.8)
15
+ i18n (~> 0.6)
16
+ multi_json (~> 1.0)
17
+ builder (3.0.4)
18
+ diff-lcs (1.1.3)
19
+ i18n (0.6.1)
20
+ mongoid (3.0.10)
21
+ activemodel (~> 3.1)
22
+ moped (~> 1.1)
23
+ origin (~> 1.0)
24
+ tzinfo (~> 0.3.22)
25
+ moped (1.2.7)
26
+ multi_json (1.3.6)
27
+ origin (1.0.10)
28
+ rake (0.9.2)
29
+ rspec (2.6.0)
30
+ rspec-core (~> 2.6.0)
31
+ rspec-expectations (~> 2.6.0)
32
+ rspec-mocks (~> 2.6.0)
33
+ rspec-core (2.6.4)
34
+ rspec-expectations (2.6.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.6.0)
37
+ tzinfo (0.3.34)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ mongoid (~> 3.0)
44
+ mongoid_store!
45
+ rake
46
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Andre Meij
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,39 @@
1
+ = MongoidStore
2
+
3
+ ActiveSupport Mongoid 3 Cache store.
4
+
5
+ == Supports
6
+
7
+ * Mongoid 3+
8
+ * Ruby 1.9.2+
9
+ * ActiveSupport 3+
10
+
11
+ == Installation and Usage
12
+
13
+ gem install mongoid_store
14
+
15
+ require 'mongoid_store'
16
+
17
+ store = MongoidStore.new
18
+ store.write('abc', 123)
19
+ pp store.read('abc')
20
+
21
+ pp store.read('def')
22
+ pp store.fetch('def') { 456 }
23
+ pp store.read('def')
24
+
25
+ Using MongoidStore with rails is as easy as:
26
+
27
+ config.cache_store = :mongoid_store
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
34
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
35
+ * Send me a pull request. Bonus points for topic branches.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2012 Andre Meij. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,51 @@
1
+ require 'mongoid'
2
+ require 'active_support'
3
+
4
+ module ActiveSupport
5
+ module Cache
6
+ class MongoidStore < Store
7
+ attr_reader :collection_name
8
+
9
+ def initialize(options = {})
10
+ @collection_name = options[:collection] || :cache
11
+ options[:expires_in] ||= 1.hour
12
+ super(options)
13
+ end
14
+
15
+ def clear(options = nil)
16
+ collection.find.remove_all
17
+ end
18
+
19
+ def cleanup(options = nil)
20
+ options = merged_options(options)
21
+ collection.find(expires_at: {'$lt' => Time.now.utc.to_i}).remove_all
22
+ end
23
+
24
+ def delete_matched(matcher, options = nil)
25
+ options = merged_options(options)
26
+ collection.find(_id: key_matcher(matcher, options)).remove_all
27
+ end
28
+
29
+ protected
30
+
31
+ def write_entry(key, entry, options)
32
+ collection.find(_id: key).upsert(_id: key, value: entry.value, expires_at: entry.expires_at.to_i, created_at: Time.now.utc.to_i)
33
+ end
34
+
35
+ def read_entry(key, options = nil)
36
+ document = collection.find(_id: key, expires_at: {'$gt' => Time.now.utc.to_i}).first
37
+ ActiveSupport::Cache::Entry.new(document['value']) if document
38
+ end
39
+
40
+ def delete_entry(key, options = nil)
41
+ collection.find(_id: key).remove
42
+ end
43
+
44
+ private
45
+
46
+ def collection
47
+ Mongoid.session(:default)[collection_name]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidStore
2
+ Version = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_support/cache/mongoid_store'
2
+
3
+ module MongoidStore
4
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path('../lib/mongoid_store/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'mongoid_store'
6
+ s.version = MongoidStore::Version
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Andre Meij']
9
+ s.email = ['andre@socialreferral.com']
10
+ s.homepage = 'http://github.com/socialreferral/mongoid_store'
11
+ s.summary = 'ActiveSupport Mongoid 3 Cache store.'
12
+ s.description = 'ActiveSupport Mongoid 3 Cache store.'
13
+
14
+ s.add_dependency 'mongoid', '~> 3.0'
15
+ s.add_dependency 'activesupport', '~> 3.2'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'active_support/all'
6
+
7
+ Bundler.require(:default)
8
+
9
+ require 'mongoid_store'
10
+
11
+ Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test)
12
+
13
+ RSpec.configure do |config|
14
+ config.before(:each) do
15
+ Mongoid.session(:default).collections.each do |collection|
16
+ collection.find.remove_all
17
+ end
18
+ end
19
+ end
data/spec/mongoid.yml ADDED
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_store_test
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,133 @@
1
+ require 'helper'
2
+
3
+ describe ActiveSupport::Cache::MongoidStore do
4
+ let(:collection) { Mongoid.session(:default)[:cache] }
5
+ let(:store) { ActiveSupport::Cache::MongoidStore.new }
6
+
7
+ describe "#write" do
8
+ before(:each) do
9
+ store.write('foo', 'bar')
10
+ end
11
+ let(:document) { collection.find(_id: 'foo').first }
12
+
13
+ it "sets _id to key" do
14
+ document['_id'].should == 'foo'
15
+ end
16
+
17
+ it "sets value key to value" do
18
+ store.read('foo').should == 'bar'
19
+ end
20
+
21
+ it "sets expires in to default if not provided" do
22
+ document['expires_at'].to_i.should == (Time.now.utc + 1.hour).to_i
23
+ end
24
+
25
+ it "sets expires_at if expires_in provided" do
26
+ store.write('foo', 'bar', expires_in: 5.seconds)
27
+ document['expires_at'].to_i.should == (Time.now.utc + 5.seconds).to_i
28
+ end
29
+
30
+ it "always sets key as string" do
31
+ store.write(:baz, 'wick')
32
+ doc = collection.find(_id: 'baz').first
33
+ doc.should_not be_nil
34
+ doc['_id'].should be_instance_of(String)
35
+ end
36
+ end
37
+
38
+ describe "#read" do
39
+ before(:each) do
40
+ store.write('foo', 'bar')
41
+ end
42
+ let(:document) { collection.find(_id: 'foo').first }
43
+
44
+ it "returns nil for key not found" do
45
+ store.read('non:existent:key').should be_nil
46
+ end
47
+
48
+ it "returns nil for existing but expired key" do
49
+ collection.find(_id: 'foo').upsert(_id: 'foo', value: 'bar', expires_at: 5.seconds.ago)
50
+ store.read('foo').should be_nil
51
+ end
52
+
53
+ it "return value for existing and not expired key" do
54
+ store.write('foo', 'bar', :expires_in => 20.seconds)
55
+ store.read('foo').should == 'bar'
56
+ end
57
+
58
+ it "works with symbol" do
59
+ store.read(:foo).should == 'bar'
60
+ end
61
+ end
62
+
63
+ describe "#delete" do
64
+ before(:each) do
65
+ store.write('foo', 'bar')
66
+ end
67
+
68
+ it "delete key from cache" do
69
+ store.read('foo').should_not be_nil
70
+ store.delete('foo')
71
+ store.read('foo').should be_nil
72
+ end
73
+
74
+ it "works with symbol" do
75
+ store.read(:foo).should_not be_nil
76
+ store.delete(:foo)
77
+ store.read(:foo).should be_nil
78
+ end
79
+ end
80
+
81
+ describe "#delete_matched" do
82
+ before(:each) do
83
+ store.write('foo1', 'bar')
84
+ store.write('foo2', 'bar')
85
+ store.write('baz', 'wick')
86
+ end
87
+
88
+ it "deletes matching keys" do
89
+ store.read('foo1').should_not be_nil
90
+ store.read('foo2').should_not be_nil
91
+ store.delete_matched(/foo/)
92
+ store.read('foo1').should be_nil
93
+ store.read('foo2').should be_nil
94
+ end
95
+
96
+ it "does not delete unmatching keys" do
97
+ store.delete_matched('foo')
98
+ store.read('baz').should_not be_nil
99
+ end
100
+ end
101
+
102
+ describe "#exist?" do
103
+ before(:each) do
104
+ store.write('foo', 'bar')
105
+ end
106
+
107
+ it "returns true if key found" do
108
+ store.exist?('foo').should be_true
109
+ end
110
+
111
+ it "returns false if key not found" do
112
+ store.exist?('not:found:key').should be_false
113
+ end
114
+
115
+ it "works with symbol" do
116
+ store.exist?(:foo).should be_true
117
+ store.exist?(:notfoundkey).should be_false
118
+ end
119
+ end
120
+
121
+ describe "#clear" do
122
+ before(:each) do
123
+ store.write('foo', 'bar')
124
+ store.write('baz', 'wick')
125
+ end
126
+
127
+ it "clear all keys" do
128
+ collection.find.count.should == 2
129
+ store.clear
130
+ collection.find.count.should == 0
131
+ end
132
+ end
133
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --timeout
3
+ 20
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andre Meij
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.2'
46
+ description: ActiveSupport Mongoid 3 Cache store.
47
+ email:
48
+ - andre@socialreferral.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .DS_Store
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - lib/.DS_Store
60
+ - lib/active_support/.DS_Store
61
+ - lib/active_support/cache/mongoid_store.rb
62
+ - lib/mongoid_store.rb
63
+ - lib/mongoid_store/version.rb
64
+ - mongoid_store.gemspec
65
+ - spec/helper.rb
66
+ - spec/mongoid.yml
67
+ - spec/mongoid_store_spec.rb
68
+ - spec/spec.opts
69
+ homepage: http://github.com/socialreferral/mongoid_store
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.23
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: ActiveSupport Mongoid 3 Cache store.
93
+ test_files: []
94
+ has_rdoc: