mongoid_monkey 0.1.2 → 0.1.3

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: 713cd432161aff92379202903badf76643f5c345
4
- data.tar.gz: 2a6da7db3fd0fdb03a8828bd761fd93dac98500b
3
+ metadata.gz: e5814ac5f6752cc4afee52c517ed3996b7d4373f
4
+ data.tar.gz: 5c42d28b26b8a8c5aa7d1ca71c29397d3392560c
5
5
  SHA512:
6
- metadata.gz: 417de743fe275b6dc78d88eec28ef8120505972e08e5096810f973cf862d9d2df9d150eb308f81c50041643fd4290fe90eae133ef47099a529a0c345cd6b6ca0
7
- data.tar.gz: 3cbddb3acdd988d96c61a1ed1557057895520288cee81f29fce9ad657bb4e973bae50174ca4f3c0c9fe5db8d6a82ed8b909f8a3f929539e054724834bb250852
6
+ metadata.gz: 858f9e0888815dcebe7974e7ddd3737e3c5ce9a5117d47dc0436c9e87626e68ad639722207760dda09202f56ec7a8180325799ba7bc39cd7aaf1dc76824bc40b
7
+ data.tar.gz: d0c827a916d331114e8b36a75a12d118c83284d491fb055ec80d670c3924856765621a514f1b4acfe7df63505ec42239488704e782bf5d4a9145158aa0b6d275
@@ -1,15 +1,7 @@
1
1
  require 'version'
2
2
 
3
- if Mongoid::VERSION =~ /\A3\./
4
- require 'patches/atomic'
5
- require 'patches/reorder'
6
- end
7
-
8
- if Mongoid::VERSION =~ /\A[345]\./
9
- require 'patches/big_decimal'
10
- end
11
-
12
- if defined?(Moped)
13
- require 'patches/instrument' if Moped::VERSION =~ /\A1\./
14
- require 'patches/db_commands'
15
- end
3
+ require 'patches/atomic'
4
+ require 'patches/big_decimal'
5
+ require 'patches/db_commands'
6
+ require 'patches/instrument'
7
+ require 'patches/reorder'
@@ -1,3 +1,7 @@
1
+ # Backport Mongoid 4 hash-based atomic method syntax to Mongoid 3.
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
1
5
  module Mongoid
2
6
  module Contextual
3
7
  module Atomic
@@ -107,3 +111,4 @@ module Atomic
107
111
  end
108
112
  end
109
113
  end
114
+ end
@@ -1,38 +1,41 @@
1
1
  # Fixes inconsistent behavior of BigDecimal. This can be removed after
2
2
  # https://github.com/mongodb/mongoid/pull/4164 is merged, planned for Mongoid 6.
3
3
 
4
- module MongoidMonkey
5
- module Mongoid
6
- module Extensions
7
- module BigDecimal
4
+ if Mongoid::VERSION =~ /\A[345]\./
8
5
 
9
- def numeric?
10
- true
11
- end
12
-
13
- module ClassMethods
6
+ module MongoidMonkey
7
+ module Mongoid
8
+ module Extensions
9
+ module BigDecimal
14
10
 
15
- def demongoize(object)
16
- object && object.numeric? ? ::BigDecimal.new(object.to_s) : nil
11
+ def numeric?
12
+ true
17
13
  end
18
14
 
19
- def mongoize(object)
20
- object && object.numeric? ? object.to_s : nil
15
+ module ClassMethods
16
+
17
+ def demongoize(object)
18
+ object && object.numeric? ? ::BigDecimal.new(object.to_s) : nil
19
+ end
20
+
21
+ def mongoize(object)
22
+ object && object.numeric? ? object.to_s : nil
23
+ end
21
24
  end
22
25
  end
23
- end
24
26
 
25
- module String
27
+ module String
26
28
 
27
- def numeric?
28
- true if Float(self) rescue (self =~ /^NaN|\-?Infinity$/)
29
+ def numeric?
30
+ true if Float(self) rescue (self =~ /^NaN|\-?Infinity$/)
31
+ end
29
32
  end
30
33
  end
31
34
  end
32
35
  end
33
- end
34
36
 
35
- ::BigDecimal.__send__(:include, MongoidMonkey::Mongoid::Extensions::BigDecimal)
36
- ::BigDecimal.extend(MongoidMonkey::Mongoid::Extensions::BigDecimal::ClassMethods)
37
+ ::BigDecimal.__send__(:include, MongoidMonkey::Mongoid::Extensions::BigDecimal)
38
+ ::BigDecimal.extend(MongoidMonkey::Mongoid::Extensions::BigDecimal::ClassMethods)
37
39
 
38
- ::String.__send__(:include, MongoidMonkey::Mongoid::Extensions::String)
40
+ ::String.__send__(:include, MongoidMonkey::Mongoid::Extensions::String)
41
+ end
@@ -4,49 +4,119 @@
4
4
  # - listIndexes
5
5
  # - createIndexes
6
6
 
7
- module Moped
8
- class Database
7
+ if defined?(Moped)
9
8
 
10
- def collection_names
11
- namespaces = command(listCollections: 1, filter: { name: { "$not" => /system\.|\$/ } })
12
- namespaces["cursor"]["firstBatch"].map do |doc|
13
- doc["name"]
9
+ module Moped
10
+ class Database
11
+
12
+ def collection_names
13
+ namespaces = command(listCollections: 1, filter: { name: { "$not" => /system\.|\$/ } })
14
+ namespaces["cursor"]["firstBatch"].map do |doc|
15
+ doc["name"]
16
+ end
14
17
  end
15
18
  end
16
19
  end
17
- end
18
20
 
19
- module Moped
20
- class Indexes
21
+ module Moped
22
+ class Indexes
21
23
 
22
- def [](key)
23
- list_indexes_command.detect do |index|
24
- (index['name'] == key) || (index['key'] == normalize_keys(key))
24
+ def [](key)
25
+ list_indexes_command.detect do |index|
26
+ (index['name'] == key) || (index['key'] == normalize_keys(key))
27
+ end
25
28
  end
26
- end
27
29
 
28
- def create(key, options = {})
29
- spec = options.merge(ns: namespace, key: key)
30
- spec[:name] ||= key.to_a.join("_")
31
- database.command(createIndexes: collection_name, indexes: [spec])
32
- end
30
+ def create(key, options = {})
31
+ spec = options.merge(ns: namespace, key: key)
32
+ spec[:name] ||= key.to_a.join("_")
33
+ database.command(createIndexes: collection_name, indexes: [spec])
34
+ end
33
35
 
34
- def each(&block)
35
- list_indexes_command.each(&block)
36
+ def each(&block)
37
+ list_indexes_command.each(&block)
38
+ end
39
+
40
+ protected
41
+
42
+ def list_indexes_command
43
+ database.command(listIndexes: collection_name)["cursor"]["firstBatch"]
44
+ end
45
+
46
+ def normalize_keys(spec)
47
+ return false if spec.is_a?(String)
48
+ spec.reduce({}) do |transformed, (key, value)|
49
+ transformed[key.to_s] = value
50
+ transformed
51
+ end
52
+ end
36
53
  end
54
+ end
55
+ end
56
+
57
+ if Mongoid::VERSION =~ /\A3\./ && defined?(Rails::Mongoid)
37
58
 
38
- protected
59
+ module Rails::Mongoid
39
60
 
40
- def list_indexes_command
41
- database.command(listIndexes: collection_name)["cursor"]["firstBatch"]
61
+ def remove_indexes(*globs)
62
+ models(*globs).each do |model|
63
+ next if model.embedded?
64
+ begin
65
+ indexes = model.collection.indexes.map{ |doc| doc["name"] }
66
+ indexes.delete_one("_id_")
67
+ model.remove_indexes
68
+ rescue Moped::Errors::OperationFailure
69
+ next
70
+ end
71
+ logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
72
+ model
73
+ end.compact
42
74
  end
75
+ end
76
+ end
77
+
78
+ if Mongoid::VERSION =~ /\A4\./
43
79
 
44
- def normalize_keys(spec)
45
- return false if spec.is_a?(String)
46
- spec.reduce({}) do |transformed, (key, value)|
47
- transformed[key.to_s] = value
48
- transformed
80
+ module Mongoid::Tasks::Database
81
+
82
+ def undefined_indexes(models = ::Mongoid.models)
83
+ undefined_by_model = {}
84
+
85
+ models.each do |model|
86
+ unless model.embedded?
87
+ begin
88
+ model.collection.indexes.each do |index|
89
+ # ignore default index
90
+ unless index['name'] == '_id_'
91
+ key = index['key'].symbolize_keys
92
+ spec = model.index_specification(key)
93
+ unless spec
94
+ # index not specified
95
+ undefined_by_model[model] ||= []
96
+ undefined_by_model[model] << index
97
+ end
98
+ end
99
+ end
100
+ rescue Moped::Errors::OperationFailure; end
101
+ end
49
102
  end
103
+
104
+ undefined_by_model
105
+ end
106
+
107
+ def remove_indexes(models = ::Mongoid.models)
108
+ models.each do |model|
109
+ next if model.embedded?
110
+ begin
111
+ indexes = model.collection.indexes.map{ |doc| doc["name"] }
112
+ indexes.delete_one("_id_")
113
+ model.remove_indexes
114
+ rescue Moped::Errors::OperationFailure
115
+ next
116
+ end
117
+ logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
118
+ model
119
+ end.compact
50
120
  end
51
121
  end
52
122
  end
@@ -1,45 +1,48 @@
1
1
  # Instrument Moped 1.x same as Moped 2.x.
2
2
  # Useful for integration with third-party services.
3
3
 
4
- module Moped
5
- module Instrumentable
6
- class Noop
4
+ if defined?(Moped) && Moped::VERSION =~ /\A1\./
7
5
 
8
- class << self
6
+ module Moped
7
+ module Instrumentable
8
+ class Noop
9
9
 
10
- # Do not instrument anything.
11
- def instrument(name, payload = {})
12
- yield payload if block_given?
10
+ class << self
11
+
12
+ # Do not instrument anything.
13
+ def instrument(name, payload = {})
14
+ yield payload if block_given?
15
+ end
13
16
  end
14
17
  end
15
18
  end
16
19
  end
17
- end
18
20
 
19
- module Moped
20
- module Instrumentable
21
+ module Moped
22
+ module Instrumentable
21
23
 
22
- TOPIC = "query.moped"
24
+ TOPIC = "query.moped"
23
25
 
24
- def instrumenter
25
- @instrumenter ||= Moped::Instrumentable::Noop
26
- end
26
+ def instrumenter
27
+ @instrumenter ||= Moped::Instrumentable::Noop
28
+ end
27
29
 
28
- def instrument(name, payload = {}, &block)
29
- instrumenter.instrument(name, payload, &block)
30
+ def instrument(name, payload = {}, &block)
31
+ instrumenter.instrument(name, payload, &block)
32
+ end
30
33
  end
31
34
  end
32
- end
33
35
 
34
- module Moped
35
- class Node
36
- include Moped::Instrumentable
36
+ module Moped
37
+ class Node
38
+ include Moped::Instrumentable
37
39
 
38
- def logging_with_instrument(operations, &block)
39
- instrument(TOPIC, prefix: " MOPED: #{resolved_address}", ops: operations) do
40
- logging_without_instrument(operations, &block)
40
+ def logging_with_instrument(operations, &block)
41
+ instrument(TOPIC, prefix: " MOPED: #{resolved_address}", ops: operations) do
42
+ logging_without_instrument(operations, &block)
43
+ end
41
44
  end
45
+ alias_method_chain :logging, :instrument
42
46
  end
43
- alias_method_chain :logging, :instrument
44
47
  end
45
48
  end
@@ -1,11 +1,14 @@
1
1
  # Backport of Criteria#reorder method from Mongoid 4 to Mongoid 3.
2
2
 
3
- module Origin
4
- module Optional
3
+ if Mongoid::VERSION =~ /\A3\./
5
4
 
6
- def reorder(*spec)
7
- options.delete(:sort)
8
- order_by(*spec)
5
+ module Origin
6
+ module Optional
7
+
8
+ def reorder(*spec)
9
+ options.delete(:sort)
10
+ order_by(*spec)
11
+ end
9
12
  end
10
13
  end
11
14
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MongoidMonkey
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -0,0 +1,4 @@
1
+
2
+ class Account
3
+ include Mongoid::Document
4
+ end
@@ -0,0 +1,7 @@
1
+
2
+ class Address
3
+ include Mongoid::Document
4
+ field :street
5
+ field :name, localize: true
6
+ embedded_in :addressable, polymorphic: true
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ class Draft
3
+ include Mongoid::Document
4
+ field :text
5
+ recursively_embeds_one
6
+ index text: 1
7
+ end
@@ -0,0 +1,11 @@
1
+
2
+ class Person
3
+ include Mongoid::Document
4
+
5
+ index age: 1
6
+ index addresses: 1
7
+ index dob: 1
8
+ index name: 1
9
+ index title: 1
10
+ index({ ssn: 1 }, { unique: true })
11
+ end
@@ -0,0 +1,6 @@
1
+
2
+ class User
3
+ include Mongoid::Document
4
+ field :name
5
+ index name: 1
6
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ MODELS = File.join(File.dirname(__FILE__), "app/models")
5
+ $LOAD_PATH.unshift(MODELS)
6
+
3
7
  $database_name = 'mongoid_monkey_test'
4
8
 
5
9
  require 'mongoid'
10
+ require 'rails/mongoid' if Mongoid::VERSION =~ /\A3\./
6
11
  require 'mongoid_monkey'
7
12
  require 'rspec'
8
13
 
@@ -13,6 +18,12 @@ end
13
18
  Mongoid.logger.level = Logger::INFO
14
19
  Mongo::Logger.logger.level = Logger::INFO unless Mongoid::VERSION =~ /\A[34]\./
15
20
 
21
+ # Autoload every model for the test suite that sits in spec/app/models.
22
+ Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
23
+ name = File.basename(file, ".rb")
24
+ autoload name.camelize.to_sym, name
25
+ end
26
+
16
27
  RSpec.configure do |config|
17
28
  config.after(:all) do
18
29
  if Mongoid::VERSION =~ /\A[34]\./