mongoid-store 0.4.3

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDAwODkyODM3MGQ3MmM0OTNkZmZlMjE0MzY1YWUzM2YwMzJiYTE1ZQ==
5
+ data.tar.gz: !binary |-
6
+ MmVlNWVhYTk2NGRlOWFkYTdjMTk3NTYyY2M0OTI2MmJlZTU0YTg1Ng==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YTNjZDNiNTkxYjIzZTFiMzNmYjFiZjEyODhhMDkxODYzYjlkOTk5ZjdjNjgx
10
+ YzFiZDRiMTAwMzAyOTVkOTk3MmFlY2JkMTQxZDA0YzI3OTVhZDBiN2RlYzg4
11
+ OTBmYjc0ODBhN2MyMDYyMjdlOWY3NGY0OTBiZTEwYWQ5NjZhZjI=
12
+ data.tar.gz: !binary |-
13
+ ZTExM2Q5ZjhiY2NjOTUwY2UzMGZmMTA3NTUxMzc1OGM3NTA5Mjg4OGZlOTVm
14
+ ZmEzNDhiNmI3MjRhMjg3NGYxMWRlZjk4YTM1Y2NjMDM1MDI4N2MwOTNiZmQz
15
+ OTRlMzlkYTY3ZTg0MDRhMmE3ZjFhYjU5OGU3Zjk4ZTdjNjMyNWU=
@@ -0,0 +1 @@
1
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', "~> 3.0"
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'pry'
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-store (0.4.2)
5
+ activesupport (~> 3.2)
6
+ mongoid (~> 3.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.13)
12
+ activesupport (= 3.2.13)
13
+ builder (~> 3.0.0)
14
+ activesupport (3.2.13)
15
+ i18n (= 0.6.1)
16
+ multi_json (~> 1.0)
17
+ builder (3.0.4)
18
+ coderay (1.0.9)
19
+ diff-lcs (1.2.4)
20
+ i18n (0.6.1)
21
+ method_source (0.8.1)
22
+ mongoid (3.1.4)
23
+ activemodel (~> 3.2)
24
+ moped (~> 1.4)
25
+ origin (~> 1.0)
26
+ tzinfo (~> 0.3.22)
27
+ moped (1.5.0)
28
+ multi_json (1.7.6)
29
+ origin (1.1.0)
30
+ pry (0.9.12.2)
31
+ coderay (~> 1.0.5)
32
+ method_source (~> 0.8)
33
+ slop (~> 3.4)
34
+ rake (10.0.4)
35
+ rspec (2.13.0)
36
+ rspec-core (~> 2.13.0)
37
+ rspec-expectations (~> 2.13.0)
38
+ rspec-mocks (~> 2.13.0)
39
+ rspec-core (2.13.1)
40
+ rspec-expectations (2.13.0)
41
+ diff-lcs (>= 1.1.3, < 2.0)
42
+ rspec-mocks (2.13.1)
43
+ slop (3.4.5)
44
+ tzinfo (0.3.37)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ mongoid (~> 3.0)
51
+ mongoid-store!
52
+ pry
53
+ rake
54
+ 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.
@@ -0,0 +1,60 @@
1
+ MongoidStore
2
+ ------------
3
+
4
+ ActiveSupport Mongoid 3 Cache store, strongly based on the 'mongoid_store'
5
+ gem by Andre Meij (see his orginial LICENSE)
6
+
7
+ unlike the the original gem this version works with both rails3 and rails4,
8
+ and handles any object in any encoding (remember mongo is utf8 only)
9
+
10
+
11
+
12
+ Supports
13
+ --------
14
+
15
+ * Mongoid 3+
16
+ * Ruby 1.9.2+
17
+ * ActiveSupport 3+
18
+
19
+
20
+ Installation and Usage
21
+ ----------------------
22
+
23
+ ```bash
24
+ # cli
25
+
26
+ gem install mongoid-store
27
+ ```
28
+
29
+ ```ruby
30
+ # Gemfile
31
+
32
+ gem 'mongoid-store'
33
+ ```
34
+
35
+ Direct usage
36
+
37
+ ```ruby
38
+ # direct usage
39
+
40
+ require 'mongoid-store'
41
+
42
+ store = ActiveSupport::Cache::MongoidStore.new
43
+ store.write('abc', 123)
44
+
45
+ store.read('abc')
46
+
47
+ store.read('def')
48
+ store.fetch('def'){ 456 }
49
+ store.read('def')
50
+ ```
51
+
52
+ Using MongoidStore with rails is as easy as:
53
+
54
+ ```ruby
55
+ # rails usage
56
+
57
+ config.cache_store = :mongoid_store
58
+
59
+ Rails.cache.write(:key, :val)
60
+ ```
@@ -0,0 +1,29 @@
1
+ = MongoidStore
2
+
3
+ ActiveSupport Mongoid 3 Cache store, strongly based on the 'mongoid_store' gem by Andre Meij
4
+
5
+
6
+
7
+ == Supports
8
+
9
+ * Mongoid 3+
10
+ * Ruby 1.9.2+
11
+ * ActiveSupport 3+
12
+
13
+ == Installation and Usage
14
+
15
+ gem install mongoid-store
16
+
17
+ require 'mongoid-store'
18
+
19
+ store = ActiveSupport::Cache::MongoidStore.new
20
+ store.write('abc', 123)
21
+ pp store.read('abc')
22
+
23
+ pp store.read('def')
24
+ pp store.fetch('def'){ 456 }
25
+ pp store.read('def')
26
+
27
+ Using MongoidStore with rails is as easy as:
28
+
29
+ config.cache_store = :mongoid_store
@@ -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/a.rb ADDED
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'mongoid'
4
+
5
+ string = Marshal.dump("\255")
6
+
7
+ marshaled = Marshal.dump(string)
8
+
9
+ binary = Moped::BSON::Binary.new(:generic, marshaled)
10
+
11
+ unmarshaled = Marshal.load(binary.to_s)
12
+
13
+
14
+ p(unmarshaled == string)
15
+ p(unmarshaled.encoding)
16
+ p(string.encoding)
17
+ p('foobar'.encoding)
@@ -0,0 +1,144 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'mongoid'
4
+ require 'active_support'
5
+
6
+ module ActiveSupport
7
+ module Cache
8
+ class MongoidStore < Store
9
+ attr_reader :collection_name
10
+
11
+ def initialize(options = {})
12
+ @collection_name = options[:collection] || :rails_cache
13
+ options[:expires_in] ||= 1.hour
14
+ super(options)
15
+ end
16
+
17
+ def clear(options = nil)
18
+ collection.find.remove_all
19
+ end
20
+
21
+ def cleanup(options = nil)
22
+ options = merged_options(options)
23
+ collection.find(expires_at: {'$lt' => Time.now.utc.to_i}).remove_all
24
+ end
25
+
26
+ def delete_matched(matcher, options = nil)
27
+ options = merged_options(options)
28
+ collection.find(_id: key_matcher(matcher, options)).remove_all
29
+ end
30
+
31
+ def delete_entry(key, options = nil)
32
+ collection.find(_id: key).remove
33
+ end
34
+
35
+ protected
36
+
37
+ def write_entry(key, entry, options)
38
+ data = Entry.data_for(entry)
39
+ expires_at = entry.expires_at.to_i
40
+ created_at = Time.now.utc.to_i
41
+
42
+ collection.find(_id: key).upsert(_id: key, data: data, expires_at: expires_at, created_at: created_at)
43
+
44
+ entry
45
+ end
46
+
47
+ def read_entry(key, options = {})
48
+ expires_at = Time.now.utc.to_i
49
+ doc = collection.find(_id: key, expires_at: {'$gt' => expires_at}).first
50
+
51
+ Entry.for(doc) if doc
52
+ end
53
+
54
+ # this class exists to normalize between rails3 and rails4, but also to
55
+ # repair totally broken interfaces in rails - especially in rails3 - that
56
+ # result in lots of extra serialization/deserialzation in a class which is
57
+ # supposed to be FAST
58
+ #
59
+ class Entry < ::ActiveSupport::Cache::Entry
60
+ def Entry.is_rails3?
61
+ unless defined?(@is_rails3)
62
+ @is_rails3 = new(nil).instance_variable_defined?('@value')
63
+ end
64
+
65
+ @is_rails3
66
+ end
67
+
68
+ # extract marshaled data from a cache entry without doing unnecessary
69
+ # marshal round trips. rails3 will have either a nil or pre-marshaled
70
+ # @value whereas rails4 will have either a marshaled or un-marshaled @v.
71
+ # in both cases we want to avoid calling the silly 'value' accessor
72
+ # since this will cause a potential Marshal.load call and require us to
73
+ # make a subsequent Marshal.dump call which is SLOOOWWW.
74
+ #
75
+ if is_rails3?
76
+
77
+ def Entry.data_for(entry)
78
+ value = entry.instance_variable_get('@value')
79
+ marshaled = value.nil? ? Marshal.dump(value) : value
80
+ Moped::BSON::Binary.new(:generic, marshaled)
81
+ end
82
+
83
+ else
84
+
85
+ def Entry.data_for(entry)
86
+ v = entry.instance_variable_get('@v')
87
+ marshaled = entry.send('compressed?') ? v : entry.send('compress', v)
88
+ Moped::BSON::Binary.new(:generic, marshaled)
89
+ end
90
+
91
+ end
92
+
93
+ # the intializer for rails' default Entry class will go ahead and
94
+ # perform and extraneous Marshal.dump on the data we just got from the
95
+ # db even though we don't need it here. rails3 has a factory to avoid
96
+ # this but rails4 does not so we just build the object we want and
97
+ # ensure to avoid any unnecessary calls to Marshal.dump/load... sigh.
98
+ #
99
+ if is_rails3?
100
+
101
+ def Entry.for(doc)
102
+ data = doc['data'].to_s
103
+ value = Marshal.load(data)
104
+ created_at = doc['created_at'].to_f
105
+
106
+ allocate.tap do |entry|
107
+ entry.instance_variable_set(:@value, value)
108
+ entry.instance_variable_set(:@compressed, false)
109
+ entry.instance_variable_set(:@created_at, created_at)
110
+ end
111
+ end
112
+
113
+ else
114
+
115
+ def Entry.for(doc)
116
+ data = doc['data'].to_s
117
+ value = Marshal.load(data)
118
+ created_at = doc['created_at'].to_f
119
+
120
+ allocate.tap do |entry|
121
+ entry.instance_variable_set(:@v, value)
122
+ entry.instance_variable_set(:@c, false)
123
+ end
124
+ end
125
+
126
+ end
127
+
128
+ def value
129
+ Entry.is_rails3? ? @value : @v
130
+ end
131
+
132
+ def raw_value
133
+ Entry.is_rails3? ? @value : @v
134
+ end
135
+ end
136
+
137
+ private
138
+
139
+ def collection
140
+ Mongoid.session(:default)[collection_name]
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_support/cache/mongoid-store'
2
+
3
+ module MongoidStore; end
@@ -0,0 +1,3 @@
1
+ module MongoidStore
2
+ Version = '0.4.3'
3
+ end
@@ -0,0 +1,22 @@
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 = ["Ara Howard"]
9
+ s.email = ["ara.t.howard@gmail.com"]
10
+ s.homepage = "http://github.com/ahoward/mongoid-store"
11
+ s.description = "Rails Mongoid 3 Cache store."
12
+ s.summary = "Rails Mongoid 3 Cache store"
13
+ s.license = "Same as Ruby's"
14
+
15
+ s.add_dependency 'mongoid', '~> 3.0'
16
+ s.add_dependency 'activesupport', '~> 3.2'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ #
3
+ $:.unshift(File.expand_path('../../lib', __FILE__))
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ require 'active_support/all'
8
+
9
+ Bundler.require(:default)
10
+
11
+ require 'mongoid-store'
12
+
13
+ Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test)
14
+
15
+ RSpec.configure do |config|
16
+ config.before(:each) do
17
+ Mongoid.session(:default).collections.each do |collection|
18
+ collection.find.remove_all
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,153 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'helper'
4
+
5
+ describe ActiveSupport::Cache::MongoidStore do
6
+ let(:collection) { Mongoid.session(:default)[:rails_cache] }
7
+ let(:store) { ActiveSupport::Cache::MongoidStore.new }
8
+
9
+ describe "#write" do
10
+ before(:each) do
11
+ store.write('foo', 'bar')
12
+ end
13
+ let(:document) { collection.find(_id: 'foo').first }
14
+
15
+ it "sets _id to key" do
16
+ document['_id'].should == 'foo'
17
+ end
18
+
19
+ it "sets value key to value" do
20
+ store.read('foo').should == 'bar'
21
+ end
22
+
23
+ it "sets expires in to default if not provided" do
24
+ document['expires_at'].to_i.should == (Time.now.utc + 1.hour).to_i
25
+ end
26
+
27
+ it "sets expires_at if expires_in provided" do
28
+ store.write('foo', 'bar', expires_in: 5.seconds)
29
+ document['expires_at'].to_i.should == (Time.now.utc + 5.seconds).to_i
30
+ end
31
+
32
+ it "always sets key as string" do
33
+ store.write(:baz, 'wick')
34
+ doc = collection.find(_id: 'baz').first
35
+ doc.should_not be_nil
36
+ doc['_id'].should be_instance_of(String)
37
+ end
38
+ end
39
+
40
+ describe "#read" do
41
+ before(:each) do
42
+ store.write('foo', 'bar')
43
+ end
44
+ let(:document) { collection.find(_id: 'foo').first }
45
+
46
+ it "returns nil for key not found" do
47
+ store.read('non:existent:key').should be_nil
48
+ end
49
+
50
+ it "returns nil for existing but expired key" do
51
+ collection.find(_id: 'foo').upsert(_id: 'foo', value: 'bar', expires_at: 5.seconds.ago)
52
+ store.read('foo').should be_nil
53
+ end
54
+
55
+ it "return value for existing and not expired key" do
56
+ store.write('foo', 'bar', :expires_in => 20.seconds)
57
+ store.read('foo').should == 'bar'
58
+ end
59
+
60
+ it "works with symbol" do
61
+ store.read(:foo).should == 'bar'
62
+ end
63
+ end
64
+
65
+ describe "encoding" do
66
+ before(:each) do
67
+ store.write('non_utf8', "\255")
68
+ end
69
+ let(:document) { collection.find(_id: 'non_utf8').first }
70
+
71
+ it "can write non-utf data" do
72
+ document.should_not be_nil
73
+ document['data'].should be_instance_of(Moped::BSON::Binary)
74
+ end
75
+
76
+ it "can read non-utf data" do
77
+ value = store.read('non_utf8')
78
+ value.should_not be_nil
79
+ value.should eq("\255")
80
+ end
81
+ end
82
+
83
+ describe "#delete" do
84
+ before(:each) do
85
+ store.write('foo', 'bar')
86
+ end
87
+
88
+ it "delete key from cache" do
89
+ store.read('foo').should_not be_nil
90
+ store.delete('foo')
91
+ store.read('foo').should be_nil
92
+ end
93
+
94
+ it "works with symbol" do
95
+ store.read(:foo).should_not be_nil
96
+ store.delete(:foo)
97
+ store.read(:foo).should be_nil
98
+ end
99
+ end
100
+
101
+ describe "#delete_matched" do
102
+ before(:each) do
103
+ store.write('foo1', 'bar')
104
+ store.write('foo2', 'bar')
105
+ store.write('baz', 'wick')
106
+ end
107
+
108
+ it "deletes matching keys" do
109
+ store.read('foo1').should_not be_nil
110
+ store.read('foo2').should_not be_nil
111
+ store.delete_matched(/foo/)
112
+ store.read('foo1').should be_nil
113
+ store.read('foo2').should be_nil
114
+ end
115
+
116
+ it "does not delete unmatching keys" do
117
+ store.delete_matched('foo')
118
+ store.read('baz').should_not be_nil
119
+ end
120
+ end
121
+
122
+ describe "#exist?" do
123
+ before(:each) do
124
+ store.write('foo', 'bar')
125
+ end
126
+
127
+ it "returns true if key found" do
128
+ store.exist?('foo').should be_true
129
+ end
130
+
131
+ it "returns false if key not found" do
132
+ store.exist?('not:found:key').should be_false
133
+ end
134
+
135
+ it "works with symbol" do
136
+ store.exist?(:foo).should be_true
137
+ store.exist?(:notfoundkey).should be_false
138
+ end
139
+ end
140
+
141
+ describe "#clear" do
142
+ before(:each) do
143
+ store.write('foo', 'bar')
144
+ store.write('baz', 'wick')
145
+ end
146
+
147
+ it "clear all keys" do
148
+ collection.find.count.should == 2
149
+ store.clear
150
+ collection.find.count.should == 0
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid-store
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --timeout
3
+ 20
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.3
5
+ platform: ruby
6
+ authors:
7
+ - Ara Howard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ description: Rails Mongoid 3 Cache store.
42
+ email:
43
+ - ara.t.howard@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE
52
+ - README.md
53
+ - README.rdoc
54
+ - Rakefile
55
+ - a.rb
56
+ - lib/active_support/cache/mongoid-store.rb
57
+ - lib/mongoid-store.rb
58
+ - lib/mongoid-store/version.rb
59
+ - mongoid-store.gemspec
60
+ - pkg/mongoid-store-0.4.2.gem
61
+ - spec/helper.rb
62
+ - spec/mongoid-store_spec.rb
63
+ - spec/mongoid.yml
64
+ - spec/spec.opts
65
+ homepage: http://github.com/ahoward/mongoid-store
66
+ licenses:
67
+ - Same as Ruby's
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Rails Mongoid 3 Cache store
89
+ test_files:
90
+ - spec/helper.rb
91
+ - spec/mongoid-store_spec.rb
92
+ - spec/mongoid.yml
93
+ - spec/spec.opts