mongoid_monkey 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35ce26c7c1fa842774c1c8724d3060e63d14bcc3
4
- data.tar.gz: 73b16e660f5088a4ef8f7a07b0ad37a1731ead1c
3
+ metadata.gz: 56af6b17c89f409a9fa4995c2bcdf09bead1960b
4
+ data.tar.gz: f62b1170ba699b56cbd3eb9ea08019b447fbb176
5
5
  SHA512:
6
- metadata.gz: 1b98576cffc2d498853d06b1dbffaef9a5ea53d5c184c6884e7fe146261a5da5cea89a90b2d633d0d7d0264edea9cfd1b3d2a49bcf63a2fa34319e1e045cad55
7
- data.tar.gz: f07e78cc1905d110a4042dac8f8f15448dc5d8d39dd7412c1ad1f8a693c59d5f39e12ffebe449a0da7030651fad95500308bd156970f5fec3e07b4846b6fc38b
6
+ metadata.gz: e78fc8da08082c3fd0b123614ee7c296d0aebf133523ebe5acf088ec9f9fb84951bac88a867dfa9fcc4af90b51e8d9138ce86094ce58f097ed3a708db41fe8de
7
+ data.tar.gz: cc43215fe1cf22059156da5a002ebac8815aeeaf4997f8a053d48dcf1ceac97df04def42acdd666081ca33eecd4488594b6c5c58e6ac8df0f7af1401075a642e
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Mongoid Monkey
1
+ # Mongoid Monkey [![Build Status](https://travis-ci.org/johnnyshields/mongoid_monkey.svg?branch=master)](https://travis-ci.org/johnnyshields/mongoid_monkey)
2
2
 
3
3
  ### Monkey Patches for Mongoid
4
4
 
@@ -29,8 +29,8 @@ If you would only like some of the patches, please copy and paste the code to yo
29
29
  | --- | --- | --- | --- | --- |
30
30
  | `atomic.rb` | Backport syntax change of atomic query methods. | ● | | |
31
31
  | `big_decimal.rb` | Fixes buggy BigDecimal behavior. | ● | ● | ● |
32
+ | `db_commands.rb` | Use MongoDB 3.0+ command syntax; required for WiredTiger. | ● | ● | |
32
33
  | `instrument.rb` | Backport instrumentation change to Moped 1. | ● | | |
33
- | `list_collections.rb` | Fetch collections via `listCollections` command; required for WiredTiger. | ● | ● | |
34
34
  | `reorder.rb` | Backport `Criteria#reorder` method. | ● | | |
35
35
 
36
36
  ### License
@@ -11,5 +11,5 @@ end
11
11
 
12
12
  if defined?(Moped)
13
13
  require 'patches/instrument' if Moped::VERSION =~ /\A1\./
14
- require 'patches/list_collections'
14
+ require 'patches/db_commands'
15
15
  end
@@ -0,0 +1,45 @@
1
+ # Use MongoDB 3.0+ syntax for a few database-level commands.
2
+ # Required to use Moped (Mongoid 3/4) with WiredTiger:
3
+ # - listCollections
4
+ # - listIndexes
5
+ # - createIndexes
6
+
7
+ module Moped
8
+ class Database
9
+
10
+ def collection_names
11
+ namespaces = command(listCollections: 1, filter: { name: { "$not" => /system\.|\$/ } })
12
+ namespaces["cursor"]["firstBatch"].map do |doc|
13
+ doc["name"]
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module Moped
20
+ class Indexes
21
+
22
+ def [](key)
23
+ result = database.command(listIndexes: collection_name)
24
+ result["cursor"]["firstBatch"].detect do |index|
25
+ (index['name'] == key) || (index['key'] == normalize_keys(key))
26
+ end
27
+ end
28
+
29
+ def create(key, options = {})
30
+ spec = options.merge(ns: namespace, key: key)
31
+ spec[:name] ||= key.to_a.join("_")
32
+ database.command(createIndexes: collection_name, indexes: [spec])
33
+ end
34
+
35
+ protected
36
+
37
+ def normalize_keys(spec)
38
+ return false if spec.is_a?(String)
39
+ spec.reduce({}) do |transformed, (key, value)|
40
+ transformed[key.to_s] = value
41
+ transformed
42
+ end
43
+ end
44
+ end
45
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MongoidMonkey
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $database_name = 'mongoid_monkey_test'
3
4
 
4
5
  require 'mongoid'
5
6
  require 'mongoid_monkey'
6
7
  require 'rspec'
7
8
 
8
9
  Mongoid.configure do |config|
9
- config.connect_to 'mongoid_monkey_test'
10
+ config.connect_to $database_name
10
11
  end
11
12
 
12
13
  Mongoid.logger.level = Logger::INFO
@@ -0,0 +1,135 @@
1
+ require "spec_helper"
2
+
3
+ if defined?(Moped)
4
+
5
+ describe Moped::Database do
6
+
7
+ describe "#collection_names" do
8
+
9
+ let(:session) do
10
+ Moped::Session.new([ "127.0.0.1:27017" ], database: $database_name)
11
+ end
12
+
13
+ let(:database) do
14
+ described_class.new(session, $database_name)
15
+ end
16
+
17
+ let(:collection_names) do
18
+ database.collection_names
19
+ end
20
+
21
+ before do
22
+ session.drop
23
+ names.map do |name|
24
+ session.command(create: name)
25
+ end
26
+ end
27
+
28
+ context "when name doesn't include system" do
29
+
30
+ let(:names) do
31
+ %w[ users comments ]
32
+ end
33
+
34
+ it "returns the name of all non system collections" do
35
+ expect(collection_names.sort).to eq([ "comments", "users" ])
36
+ end
37
+ end
38
+
39
+ context "when name includes system not at the beginning" do
40
+
41
+ let(:names) do
42
+ %w[ users comments_system_fu ]
43
+ end
44
+
45
+ it "returns the name of all non system collections" do
46
+ expect(collection_names.sort).to eq([ "comments_system_fu", "users" ])
47
+ end
48
+ end
49
+
50
+ context "when name includes system at the beginning" do
51
+
52
+ let(:names) do
53
+ %w[ users system_comments_fu ]
54
+ end
55
+
56
+ it "returns the name of all non system collections" do
57
+ expect(collection_names.sort).to eq([ "system_comments_fu", "users" ])
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ describe Moped::Indexes do
64
+
65
+ let(:session) do
66
+ Moped::Session.new %w[127.0.0.1:27017], database: $database_name
67
+ end
68
+
69
+ let(:indexes) do
70
+ session[:users].indexes
71
+ end
72
+
73
+ before do
74
+ begin
75
+ indexes.drop
76
+ rescue Exception
77
+ end
78
+ end
79
+
80
+ describe "#create" do
81
+
82
+ context "when called without extra options" do
83
+
84
+ it "creates an index with no options" do
85
+ indexes.create name: 1
86
+ indexes[name: 1].should_not eq nil
87
+ end
88
+ end
89
+
90
+ context "when called with extra options" do
91
+
92
+ it "creates an index with the extra options" do
93
+ indexes.create({name: 1}, {unique: true})
94
+ index = indexes[name: 1]
95
+ index["unique"].should eq true
96
+ end
97
+ end
98
+
99
+ context "when there is existent data" do
100
+
101
+ before do
102
+ 3.times { session[:users].insert(name: 'John') }
103
+ end
104
+
105
+ it "raises an error" do
106
+ expect {
107
+ indexes.create({name: 1}, {unique: true})
108
+ }.to raise_error(Moped::Errors::OperationFailure)
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#drop" do
114
+
115
+ context "when provided a key" do
116
+
117
+ it "drops the index" do
118
+ indexes.create name: 1
119
+ indexes.drop(name: 1).should eq true
120
+ end
121
+ end
122
+
123
+ context "when not provided a key" do
124
+
125
+ it "drops all indexes" do
126
+ indexes.create name: 1
127
+ indexes.create age: 1
128
+ indexes.drop
129
+ indexes[name: 1].should eq nil
130
+ indexes[age: 1].should eq nil
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_monkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnnyshields
@@ -65,8 +65,8 @@ files:
65
65
  - lib/mongoid_monkey.rb
66
66
  - lib/patches/atomic.rb
67
67
  - lib/patches/big_decimal.rb
68
+ - lib/patches/db_commands.rb
68
69
  - lib/patches/instrument.rb
69
- - lib/patches/list_collections.rb
70
70
  - lib/patches/reorder.rb
71
71
  - lib/version.rb
72
72
  - spec/config/mongodb-mmapv1.conf
@@ -77,8 +77,8 @@ files:
77
77
  - spec/spec_helper.rb
78
78
  - spec/unit/atomic_spec.rb
79
79
  - spec/unit/big_decimal_spec.rb
80
+ - spec/unit/db_commands_spec.rb
80
81
  - spec/unit/instrument_spec.rb
81
- - spec/unit/list_collections_spec.rb
82
82
  - spec/unit/reorder_spec.rb
83
83
  homepage: https://github.com/johnnyshields/mongoid_monkey
84
84
  licenses:
@@ -113,7 +113,7 @@ test_files:
113
113
  - spec/spec_helper.rb
114
114
  - spec/unit/atomic_spec.rb
115
115
  - spec/unit/big_decimal_spec.rb
116
+ - spec/unit/db_commands_spec.rb
116
117
  - spec/unit/instrument_spec.rb
117
- - spec/unit/list_collections_spec.rb
118
118
  - spec/unit/reorder_spec.rb
119
119
  has_rdoc:
@@ -1,14 +0,0 @@
1
- # Get collection_names via for MongoDB 3.0+ listCollections command.
2
- # Required to use Moped (Mongoid 3/4) with WiredTiger.
3
-
4
- module Moped
5
- class Database
6
-
7
- def collection_names
8
- namespaces = Collection.new(self, "$cmd").find(listCollections: 1, filter: { name: { "$not" => /system\.|\$/ } }).first
9
- namespaces["cursor"]["firstBatch"].map do |doc|
10
- doc["name"]
11
- end
12
- end
13
- end
14
- end
@@ -1,62 +0,0 @@
1
- require "spec_helper"
2
-
3
- if defined?(Moped)
4
-
5
- describe Moped::Database do
6
-
7
- describe "#collection_names" do
8
-
9
- let(:session) do
10
- Moped::Session.new([ "127.0.0.1:27017" ], database: "moped_test")
11
- end
12
-
13
- let(:database) do
14
- described_class.new(session, :moped_test)
15
- end
16
-
17
- let(:collection_names) do
18
- database.collection_names
19
- end
20
-
21
- before do
22
- session.drop
23
- names.map do |name|
24
- session.command(create: name)
25
- end
26
- end
27
-
28
- context "when name doesn't include system" do
29
-
30
- let(:names) do
31
- %w[ users comments ]
32
- end
33
-
34
- it "returns the name of all non system collections" do
35
- expect(collection_names.sort).to eq([ "comments", "users" ])
36
- end
37
- end
38
-
39
- context "when name includes system not at the beginning" do
40
-
41
- let(:names) do
42
- %w[ users comments_system_fu ]
43
- end
44
-
45
- it "returns the name of all non system collections" do
46
- expect(collection_names.sort).to eq([ "comments_system_fu", "users" ])
47
- end
48
- end
49
-
50
- context "when name includes system at the beginning" do
51
-
52
- let(:names) do
53
- %w[ users system_comments_fu ]
54
- end
55
-
56
- it "returns the name of all non system collections" do
57
- expect(collection_names.sort).to eq([ "system_comments_fu", "users" ])
58
- end
59
- end
60
- end
61
- end
62
- end