mongoid 4.0.2 → 5.0.0

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.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +11 -19
  4. data/lib/config/locales/en.yml +33 -30
  5. data/lib/mongoid/atomic.rb +2 -4
  6. data/lib/mongoid/attributes/processing.rb +4 -1
  7. data/lib/mongoid/attributes/readonly.rb +2 -2
  8. data/lib/mongoid/attributes.rb +7 -7
  9. data/lib/mongoid/changeable.rb +4 -6
  10. data/lib/mongoid/clients/factory.rb +78 -0
  11. data/lib/mongoid/{sessions → clients}/options.rb +30 -19
  12. data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
  13. data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
  14. data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
  15. data/lib/mongoid/clients/validators.rb +2 -0
  16. data/lib/mongoid/clients.rb +141 -0
  17. data/lib/mongoid/composable.rb +3 -3
  18. data/lib/mongoid/config/environment.rb +1 -1
  19. data/lib/mongoid/config/options.rb +1 -1
  20. data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
  21. data/lib/mongoid/config/validators/option.rb +1 -1
  22. data/lib/mongoid/config/validators.rb +1 -1
  23. data/lib/mongoid/config.rb +39 -41
  24. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  25. data/lib/mongoid/contextual/atomic.rb +11 -11
  26. data/lib/mongoid/contextual/command.rb +9 -6
  27. data/lib/mongoid/contextual/geo_near.rb +17 -1
  28. data/lib/mongoid/contextual/map_reduce.rb +12 -11
  29. data/lib/mongoid/contextual/memory.rb +2 -5
  30. data/lib/mongoid/contextual/mongo.rb +92 -82
  31. data/lib/mongoid/contextual/none.rb +13 -0
  32. data/lib/mongoid/copyable.rb +6 -1
  33. data/lib/mongoid/criteria.rb +37 -4
  34. data/lib/mongoid/document.rb +3 -4
  35. data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
  36. data/lib/mongoid/errors/mongoid_error.rb +15 -9
  37. data/lib/mongoid/errors/no_client_config.rb +22 -0
  38. data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
  39. data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
  40. data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
  41. data/lib/mongoid/errors/no_default_client.rb +23 -0
  42. data/lib/mongoid/errors.rb +6 -6
  43. data/lib/mongoid/extensions/hash.rb +5 -1
  44. data/lib/mongoid/extensions/object.rb +3 -2
  45. data/lib/mongoid/extensions/set.rb +5 -5
  46. data/lib/mongoid/factory.rb +4 -2
  47. data/lib/mongoid/fields.rb +7 -2
  48. data/lib/mongoid/findable.rb +4 -1
  49. data/lib/mongoid/indexable.rb +15 -9
  50. data/lib/mongoid/persistable/creatable.rb +2 -2
  51. data/lib/mongoid/persistable/deletable.rb +3 -3
  52. data/lib/mongoid/persistable/incrementable.rb +1 -1
  53. data/lib/mongoid/persistable/logical.rb +1 -1
  54. data/lib/mongoid/persistable/poppable.rb +1 -1
  55. data/lib/mongoid/persistable/pullable.rb +2 -2
  56. data/lib/mongoid/persistable/pushable.rb +2 -2
  57. data/lib/mongoid/persistable/renamable.rb +1 -1
  58. data/lib/mongoid/persistable/settable.rb +1 -1
  59. data/lib/mongoid/persistable/unsettable.rb +1 -1
  60. data/lib/mongoid/persistable/updatable.rb +2 -2
  61. data/lib/mongoid/persistable/upsertable.rb +1 -1
  62. data/lib/mongoid/persistable.rb +1 -2
  63. data/lib/mongoid/query_cache.rb +98 -104
  64. data/lib/mongoid/railtie.rb +4 -25
  65. data/lib/mongoid/railties/database.rake +1 -1
  66. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +3 -2
  67. data/lib/mongoid/relations/builders.rb +3 -1
  68. data/lib/mongoid/relations/counter_cache.rb +1 -1
  69. data/lib/mongoid/relations/embedded/batchable.rb +3 -10
  70. data/lib/mongoid/relations/embedded/many.rb +4 -2
  71. data/lib/mongoid/relations/embedded/one.rb +1 -0
  72. data/lib/mongoid/relations/many.rb +1 -0
  73. data/lib/mongoid/relations/metadata.rb +14 -0
  74. data/lib/mongoid/relations/proxy.rb +6 -6
  75. data/lib/mongoid/relations/referenced/many.rb +2 -1
  76. data/lib/mongoid/relations/targets/enumerable.rb +11 -11
  77. data/lib/mongoid/relations/touchable.rb +1 -1
  78. data/lib/mongoid/reloadable.rb +2 -2
  79. data/lib/mongoid/scopable.rb +6 -17
  80. data/lib/mongoid/selectable.rb +1 -36
  81. data/lib/mongoid/serializable.rb +2 -2
  82. data/lib/mongoid/stateful.rb +0 -1
  83. data/lib/mongoid/tasks/database.rake +2 -2
  84. data/lib/mongoid/tasks/database.rb +24 -17
  85. data/lib/mongoid/threaded/lifecycle.rb +21 -16
  86. data/lib/mongoid/threaded.rb +54 -33
  87. data/lib/mongoid/traversable.rb +17 -1
  88. data/lib/mongoid/validatable/queryable.rb +1 -1
  89. data/lib/mongoid/validatable/uniqueness.rb +3 -20
  90. data/lib/mongoid/validatable.rb +1 -1
  91. data/lib/mongoid/version.rb +1 -1
  92. data/lib/mongoid.rb +28 -21
  93. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
  94. data/lib/rails/mongoid.rb +2 -2
  95. data/spec/app/models/audio.rb +1 -1
  96. data/spec/app/models/band.rb +1 -0
  97. data/spec/app/models/code.rb +6 -0
  98. data/spec/app/models/company.rb +5 -0
  99. data/spec/app/models/label.rb +7 -0
  100. data/spec/app/models/pub.rb +6 -0
  101. data/spec/app/models/staff.rb +7 -0
  102. data/spec/app/models/store_as_dup_test1.rb +5 -0
  103. data/spec/app/models/store_as_dup_test2.rb +5 -0
  104. data/spec/config/mongoid.yml +7 -25
  105. data/spec/mongoid/atomic/paths_spec.rb +3 -11
  106. data/spec/mongoid/attributes/nested_spec.rb +16 -16
  107. data/spec/mongoid/attributes/readonly_spec.rb +80 -18
  108. data/spec/mongoid/attributes_spec.rb +3 -3
  109. data/spec/mongoid/changeable_spec.rb +156 -0
  110. data/spec/mongoid/clients/factory_spec.rb +284 -0
  111. data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
  112. data/spec/mongoid/clients_spec.rb +754 -0
  113. data/spec/mongoid/config/environment_spec.rb +14 -11
  114. data/spec/mongoid/config_spec.rb +33 -48
  115. data/spec/mongoid/contextual/atomic_spec.rb +1 -17
  116. data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
  117. data/spec/mongoid/contextual/mongo_spec.rb +26 -83
  118. data/spec/mongoid/contextual/none_spec.rb +15 -0
  119. data/spec/mongoid/copyable_spec.rb +35 -1
  120. data/spec/mongoid/criteria/findable_spec.rb +197 -0
  121. data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
  122. data/spec/mongoid/criteria_spec.rb +74 -91
  123. data/spec/mongoid/document_spec.rb +1 -1
  124. data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
  125. data/spec/mongoid/errors/mongoid_error_spec.rb +19 -3
  126. data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
  127. data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
  128. data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
  129. data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
  130. data/spec/mongoid/fields/localized_spec.rb +1 -0
  131. data/spec/mongoid/fields_spec.rb +1 -0
  132. data/spec/mongoid/findable_spec.rb +2 -23
  133. data/spec/mongoid/indexable_spec.rb +12 -8
  134. data/spec/mongoid/interceptable_spec.rb +15 -0
  135. data/spec/mongoid/persistable/settable_spec.rb +16 -0
  136. data/spec/mongoid/persistable/updatable_spec.rb +3 -2
  137. data/spec/mongoid/persistable_spec.rb +4 -4
  138. data/spec/mongoid/query_cache_spec.rb +13 -8
  139. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  140. data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
  141. data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
  142. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
  143. data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
  144. data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
  145. data/spec/mongoid/relations/proxy_spec.rb +28 -0
  146. data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
  147. data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
  148. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
  149. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  150. data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
  151. data/spec/mongoid/reloadable_spec.rb +6 -6
  152. data/spec/mongoid/scopable_spec.rb +41 -28
  153. data/spec/mongoid/selectable_spec.rb +6 -16
  154. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  155. data/spec/mongoid/tasks/database_spec.rb +2 -2
  156. data/spec/mongoid/threaded_spec.rb +0 -7
  157. data/spec/mongoid/traversable_spec.rb +2 -2
  158. data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
  159. data/spec/mongoid_spec.rb +13 -15
  160. data/spec/rails/mongoid_spec.rb +13 -4
  161. data/spec/spec_helper.rb +44 -27
  162. data/spec/support/authorization.rb +12 -0
  163. data/spec/support/expectations.rb +14 -0
  164. metadata +50 -57
  165. data/lib/mongoid/contextual/find_and_modify.rb +0 -69
  166. data/lib/mongoid/contextual/text_search.rb +0 -178
  167. data/lib/mongoid/criteria/#findable.rb# +0 -141
  168. data/lib/mongoid/errors/no_default_session.rb +0 -23
  169. data/lib/mongoid/errors/no_session_config.rb +0 -22
  170. data/lib/mongoid/log_subscriber.rb +0 -55
  171. data/lib/mongoid/positional.rb +0 -71
  172. data/lib/mongoid/sessions/factory.rb +0 -131
  173. data/lib/mongoid/sessions/mongo_uri.rb +0 -93
  174. data/lib/mongoid/sessions/validators.rb +0 -2
  175. data/lib/mongoid/sessions.rb +0 -125
  176. data/lib/mongoid/support/query_counter.rb +0 -23
  177. data/spec/helpers.rb +0 -18
  178. data/spec/mongoid/#atomic_spec.rb# +0 -365
  179. data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
  180. data/spec/mongoid/contextual/text_search_spec.rb +0 -209
  181. data/spec/mongoid/log_subscriber_spec.rb +0 -75
  182. data/spec/mongoid/positional_spec.rb +0 -222
  183. data/spec/mongoid/sessions/factory_spec.rb +0 -333
  184. data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
  185. data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -1,178 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Contextual
4
-
5
- # Wraps behaviour around a lazy text search command.
6
- #
7
- # @since 4.0.0
8
- class TextSearch
9
- include Enumerable
10
- include Command
11
-
12
- delegate :[], to: :results
13
- delegate :==, :empty?, to: :entries
14
-
15
- # Iterate over the results of the text search command.
16
- #
17
- # @example Iterate over the results.
18
- # text_search.each do |doc|
19
- # #...
20
- # end
21
- #
22
- # @return [ Enumerator ] The enumerator.
23
- #
24
- # @since 4.0.0
25
- def each
26
- if block_given?
27
- documents.each do |doc|
28
- yield doc
29
- end
30
- else
31
- to_enum
32
- end
33
- end
34
-
35
- # Instantiate a new text search lazy proxy.
36
- #
37
- # @example Instantiate the text search.
38
- # TextSearch.new(collection, criteria, "test")
39
- #
40
- # @param [ Moped::Collection ] collection The collection to execute on.
41
- # @param [ Criteria ] criteria The criteria to filter results.
42
- # @param [ String ] search_string The search string.
43
- #
44
- # @since 4.0.0
45
- def initialize(collection, criteria, search_string)
46
- @collection, @criteria = collection, criteria
47
- command[:text] = collection.name.to_s
48
- command[:search] = search_string
49
- apply_criteria_options
50
- end
51
-
52
- # Inspect the text search object.
53
- #
54
- # @example Inspect the text search.
55
- # text_search.inspect
56
- #
57
- # @return [ String ] The inspection.
58
- #
59
- # @since 4.0.0
60
- def inspect
61
- %Q{#<Mongoid::Contextual::TextSearch
62
- selector: #{criteria.selector.inspect}
63
- class: #{criteria.klass}
64
- search: #{command[:search]}
65
- filter: #{command[:filter] || "N/A"}
66
- project: #{command[:project] || "N/A"}
67
- limit: #{command[:limit] || "N/A"}
68
- language: #{command[:language] || "default"}>
69
- }
70
- end
71
-
72
- # Execute the text search command, and return the raw results (in hash
73
- # form).
74
- #
75
- # @example Execute the command.
76
- # text_search.execute
77
- #
78
- # @return [ Hash ] The raw results.
79
- #
80
- # @since 4.0.0
81
- def execute
82
- results
83
- end
84
-
85
- # Set the language of the text search.
86
- #
87
- # @example Set the text search language.
88
- # text_search.language("deutsch")
89
- #
90
- # @param [ String ] value The name of the language.
91
- #
92
- # @return [ TextSearch ] The modified text search.
93
- #
94
- # @since 4.0.0
95
- def language(value)
96
- command[:language] = value
97
- self
98
- end
99
-
100
- # Limits the fields returned by the text search for each document. By
101
- # default, _id is always included.
102
- #
103
- # @example Limit the returned fields.
104
- # text_search.project(name: 1, title: 1)
105
- #
106
- # @param [ Hash ] value The fields to project.
107
- #
108
- # @return [ TextSearch ] The modified text search.
109
- #
110
- # @since 4.0.0
111
- def project(value)
112
- command[:project] = value
113
- self
114
- end
115
-
116
- # Get the raw statistics returned from the text search.
117
- #
118
- # @example Get the stats.
119
- # text_search.stats
120
- #
121
- # @return [ Hash ] The raw statistics.
122
- #
123
- # @since 4.0.0
124
- def stats
125
- results["stats"]
126
- end
127
-
128
- private
129
-
130
- # Apply the options from the criteria to the text search command.
131
- #
132
- # @api private
133
- #
134
- # @example Apply the criteria options, filter and limit only.
135
- # text_search.apply_criteria_options
136
- #
137
- # @return [ nil ] Nothing.
138
- #
139
- # @since 4.0.0
140
- def apply_criteria_options
141
- command[:filter] = criteria.selector
142
- if limit = criteria.options[:limit]
143
- command[:limit] = limit
144
- end
145
- end
146
-
147
- # Get the results of the text search as documents.
148
- #
149
- # @api private
150
- #
151
- # @example Get the results as documents.
152
- # text_search.documents
153
- #
154
- # @return [ Array<Document> ] The documents.
155
- #
156
- # @since 4.0.0
157
- def documents
158
- results["results"].map do |attributes|
159
- Factory.from_db(criteria.klass, attributes["obj"], command[:project])
160
- end
161
- end
162
-
163
- # Get the raw results.
164
- #
165
- # @api private
166
- #
167
- # @example Get the raw results.
168
- # text_search.results
169
- #
170
- # @return [ Hash ] The raw results.
171
- #
172
- # @since 4.0.0
173
- def results
174
- @results ||= session.command(command)
175
- end
176
- end
177
- end
178
- end
@@ -1,141 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- class Criteria
4
- module Findable
5
-
6
- # Execute the criteria or raise an error if no documents found.
7
- #
8
- # @example Execute or raise
9
- # criteria.execute_or_raise(id)
10
- #
11
- # @param [ Object ] args The arguments passed.
12
- #
13
- # @raise [ Errors::DocumentNotFound ] If nothing returned.
14
- #
15
- # @return [ Document, Array<Document> ] The document(s).
16
- #
17
- # @since 2.0.0
18
- def execute_or_raise(ids, multi)
19
- result = multiple_from_db(ids)
20
- check_for_missing_documents!(result, ids)
21
- multi ? result : result.first
22
- end
23
-
24
- # Find the matchind document(s) in the criteria for the provided ids.
25
- #
26
- # @example Find by an id.
27
- # criteria.find(BSON::ObjectId.new)
28
- #
29
- # @example Find by multiple ids.
30
- # criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
31
- #
32
- # @param [ Array<BSON::ObjectId> ] args The ids to search for.
33
- #
34
- # @return [ Array<Document>, Document ] The matching document(s).
35
- #
36
- # @since 1.0.0
37
- def find(*args)
38
- ids = args.__find_args__
39
- raise_invalid if ids.any?(&:nil?)
40
- p "YOOO"
41
- p ids
42
- for_ids(ids).execute_or_raise(ids, args.multi_arged?)
43
- end
44
-
45
- # Adds a criterion to the +Criteria+ that specifies an id that must be matched.
46
- #
47
- # @example Add a single id criteria.
48
- # criteria.for_ids([ 1 ])
49
- #
50
- # @example Add multiple id criteria.
51
- # criteria.for_ids([ 1, 2 ])
52
- #
53
- # @param [ Array ] ids The array of ids.
54
- #
55
- # @return [ Criteria ] The cloned criteria.
56
- def for_ids(ids)
57
- ids = mongoize_ids(ids)
58
- if ids.size > 1
59
- send(id_finder, { _id: { "$in" => ids }})
60
- else
61
- send(id_finder, { _id: ids.first })
62
- end
63
- end
64
-
65
- # Get the documents from the identity map, and if not found hit the
66
- # database.
67
- #
68
- # @example Get the documents from the map or criteria.
69
- # criteria.multiple_from_map_or_db(ids)
70
- #
71
- # @param [ ids ] The searched ids.
72
- #
73
- # @return [ Array<Document> ] The found documents.
74
- def multiple_from_db(ids)
75
- return entries if embedded?
76
- ids = mongoize_ids(ids)
77
- ids.empty? ? [] : from_database(ids)
78
- end
79
-
80
- private
81
-
82
- # Get the finder used to generate the id query.
83
- #
84
- # @api private
85
- #
86
- # @example Get the id finder.
87
- # criteria.id_finder
88
- #
89
- # @return [ Symbol ] The name of the finder method.
90
- #
91
- # @since 3.1.0
92
- def id_finder
93
- @id_finder ||= extract_id ? :all_of : :where
94
- end
95
-
96
- # Get documents from the database only.
97
- #
98
- # @api private
99
- #
100
- # @example Get documents from the database.
101
- # criteria.from_database(ids)
102
- #
103
- # @param [ Array<Object> ] ids The ids to fetch with.
104
- #
105
- # @return [ Array<Document> ] The matching documents.
106
- #
107
- # @since 3.0.0
108
- def from_database(ids)
109
- (ids.size > 1 ? any_in(id: ids) : where(id: ids.first)).entries
110
- end
111
-
112
- # Convert all the ids to their proper types.
113
- #
114
- # @api private
115
- #
116
- # @example Convert the ids.
117
- # criteria.mongoize_ids(ids)
118
- #
119
- # @param [ Array<Object> ] ids The ids to convert.
120
- #
121
- # @return [ Array<Object> ] The converted ids.
122
- #
123
- # @since 3.0.0
124
- def mongoize_ids(ids)
125
- ids.map{ |id| klass.fields["_id"].mongoize(id) }
126
- end
127
-
128
- # Convenience method of raising an invalid options error.
129
- #
130
- # @example Raise the error.
131
- # criteria.raise_invalid
132
- #
133
- # @raise [ Errors::InvalidOptions ] The error.
134
- #
135
- # @since 2.0.0
136
- def raise_invalid
137
- raise Errors::InvalidFind.new
138
- end
139
- end
140
- end
141
- end
@@ -1,23 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Errors
4
-
5
- # This error is raised when a default session is not defined.
6
- class NoDefaultSession < MongoidError
7
-
8
- # Create the new error with the defined session names.
9
- #
10
- # @example Create the new error.
11
- # NoDefaultSession.new([ :secondary ])
12
- #
13
- # @param [ Array<Symbol> ] keys The defined sessions.
14
- #
15
- # @since 3.0.0
16
- def initialize(keys)
17
- super(
18
- compose_message("no_default_session", { keys: keys.join(", ") })
19
- )
20
- end
21
- end
22
- end
23
- end
@@ -1,22 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Errors
4
-
5
- # This error is raised when attempting to create a new session that does
6
- # not have a named configuration.
7
- class NoSessionConfig < MongoidError
8
-
9
- # Create the new error.
10
- #
11
- # @example Create the error.
12
- # NoSessionConfig.new(:secondary)
13
- #
14
- # @param [ String, Symbol ] name The name of the session.
15
- #
16
- # @since 3.0.0
17
- def initialize(name)
18
- super(compose_message("no_session_config", { name: name }))
19
- end
20
- end
21
- end
22
- end
@@ -1,55 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- # A Log subscriber to the moped queries
4
- #
5
- # @since 4.0.0
6
- class LogSubscriber < ActiveSupport::LogSubscriber
7
-
8
- # Log the query operation on moped
9
- #
10
- # @since 4.0.0
11
- def query(event)
12
- return unless logger.debug?
13
-
14
- payload = event.payload
15
- runtime = ("%.4fms" % event.duration)
16
- debug(payload[:prefix], payload[:ops], runtime)
17
- end
18
-
19
- def query_cache(event)
20
- return unless logger.debug?
21
-
22
- database, collection, selector = event.payload[:key]
23
- operation = "%-12s database=%s collection=%s selector=%s" % ["QUERY CACHE", database, collection, selector.inspect]
24
- logger.debug operation
25
- end
26
- # Log the provided operations.
27
- #
28
- # @example Delegates the operation to moped so it can log it.
29
- # subscriber.debug("MOPED", {}, 30)
30
- #
31
- # @param [ String ] prefix The prefix for all operations in the log.
32
- # @param [ Array ] ops The operations.
33
- # @param [ String ] runtime The runtime in formatted ms.
34
- #
35
- # @since 4.0.0
36
- def debug(prefix, operations, runtime)
37
- Moped::Loggable.log_operations(prefix, operations, runtime)
38
- end
39
-
40
- # Get the logger.
41
- #
42
- # @example Get the logger.
43
- # subscriber.logger
44
- #
45
- # @return [ Logger ] The logger.
46
- #
47
- # @since 4.0.0
48
- def logger
49
- Moped.logger
50
- end
51
- end
52
- end
53
-
54
- Mongoid::LogSubscriber.attach_to :moped
55
- Mongoid::LogSubscriber.attach_to :mongoid
@@ -1,71 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
-
4
- # This module is responsible for taking update selectors and switching out
5
- # the indexes for the $ positional operator where appropriate.
6
- #
7
- # @since 4.0.0
8
- module Positional
9
-
10
- # Takes the provided selector and atomic operations and replaces the
11
- # indexes of the embedded documents with the positional operator when
12
- # needed.
13
- #
14
- # @note The only time we can accurately know when to use the positional
15
- # operator is at the exact time we are going to persist something. So
16
- # we can tell by the selector that we are sending if it is actually
17
- # possible to use the positional operator at all. For example, if the
18
- # selector is: { "_id" => 1 }, then we could not use the positional
19
- # operator for updating embedded documents since there would never be a
20
- # match - we base whether we can based on the number of levels deep the
21
- # selector goes, and if the id values are not nil.
22
- #
23
- # @example Process the operations.
24
- # positionally(
25
- # { "_id" => 1, "addresses._id" => 2 },
26
- # { "$set" => { "addresses.0.street" => "hobrecht" }}
27
- # )
28
- #
29
- # @param [ Hash ] selector The selector.
30
- # @param [ Hash ] operations The update operations.
31
- # @param [ Hash ] processed The processed update operations.
32
- #
33
- # @return [ Hash ] The new operations.
34
- #
35
- # @since 3.1.0
36
- def positionally(selector, operations, processed = {})
37
- if selector.size == 1 || selector.values.any? { |val| val.nil? }
38
- return operations
39
- end
40
- keys = selector.keys.map{ |m| m.sub('._id','') } - ['_id']
41
- keys = keys.sort_by { |s| s.length*-1 }
42
- process_operations(keys, operations, processed)
43
- end
44
-
45
- private
46
-
47
- def process_operations(keys, operations, processed)
48
- operations.each_pair do |operation, update|
49
- processed[operation] = process_updates(keys, update)
50
- end
51
- processed
52
- end
53
-
54
- def process_updates(keys, update, updates = {})
55
- update.each_pair do |position, value|
56
- updates[replace_index(keys, position)] = value
57
- end
58
- updates
59
- end
60
-
61
- def replace_index(keys, position)
62
- # replace to $ only if that key is on the selector
63
- keys.each do |kk|
64
- if position =~ /^#{kk}\.\d+\.(.*)/
65
- return "#{kk}.$.#{$1}"
66
- end
67
- end
68
- position
69
- end
70
- end
71
- end
@@ -1,131 +0,0 @@
1
- # encoding: utf-8
2
- require "mongoid/sessions/mongo_uri"
3
-
4
- module Mongoid
5
- module Sessions
6
- module Factory
7
- extend self
8
-
9
- # Create a new session given the named configuration. If no name is
10
- # provided, return a new session with the default configuration. If a
11
- # name is provided for which no configuration exists, an error will be
12
- # raised.
13
- #
14
- # @example Create the session.
15
- # Factory.create(:secondary)
16
- #
17
- # @param [ String, Symbol ] name The named session configuration.
18
- #
19
- # @raise [ Errors::NoSessionConfig ] If no config could be found.
20
- #
21
- # @return [ Moped::Session ] The new session.
22
- #
23
- # @since 3.0.0
24
- def create(name = nil)
25
- return default unless name
26
- config = Mongoid.sessions[name]
27
- raise Errors::NoSessionConfig.new(name) unless config
28
- create_session(config)
29
- end
30
-
31
- # Get the default session.
32
- #
33
- # @example Get the default session.
34
- # Factory.default
35
- #
36
- # @raise [ Errors::NoSessionConfig ] If no default configuration is
37
- # found.
38
- #
39
- # @return [ Moped::Session ] The default session.
40
- #
41
- # @since 3.0.0
42
- def default
43
- create_session(Mongoid.sessions[:default])
44
- end
45
-
46
- private
47
-
48
- # Create the session for the provided config.
49
- #
50
- # @api private
51
- #
52
- # @example Create the session.
53
- # Factory.create_session(config)
54
- #
55
- # @param [ Hash ] configuration The session config.
56
- #
57
- # @return [ Moped::Session ] The session.
58
- #
59
- # @since 3.0.0
60
- def create_session(configuration)
61
- raise Errors::NoSessionsConfig.new unless configuration
62
- config, options = parse(configuration)
63
- configuration.merge!(config) if configuration.delete(:uri)
64
-
65
- options[:instrumenter] = ActiveSupport::Notifications
66
- session = Moped::Session.new(config[:hosts], options)
67
- session.use(config[:database])
68
- if authenticated?(config)
69
- session.login(config[:username], config[:password])
70
- end
71
- session
72
- end
73
-
74
- # Are we authenticated with this session config?
75
- #
76
- # @api private
77
- #
78
- # @example Is this session authenticated?
79
- # Factory.authenticated?(config)
80
- #
81
- # @param [ Hash ] config The session config.
82
- #
83
- # @return [ true, false ] If we are authenticated.
84
- #
85
- # @since 3.0.0
86
- def authenticated?(config)
87
- config.has_key?(:username) && config.has_key?(:password)
88
- end
89
-
90
- # Parse the configuration. If a uri is provided we need to extract the
91
- # elements of it, otherwise the options are left alone.
92
- #
93
- # @api private
94
- #
95
- # @example Parse the config.
96
- # Factory.parse(config)
97
- #
98
- # @param [ Hash ] config The configuration.
99
- #
100
- # @return [ Array<Hash> ] The configuration, parsed.
101
- #
102
- # @since 3.0.0
103
- def parse(config)
104
- options = config[:options].try(:dup) || {}
105
- parsed = if config.has_key?(:uri)
106
- MongoUri.new(config[:uri]).to_hash
107
- else
108
- inject_ports(config)
109
- end
110
- [ parsed, options.symbolize_keys ]
111
- end
112
-
113
- # Will inject the default port of 27017 if not supplied.
114
- #
115
- # @example Inject default ports.
116
- # factory.inject_ports(config)
117
- #
118
- # @param [ Hash ] config The session configuration.
119
- #
120
- # @return [ Hash ] The altered configuration.
121
- #
122
- # @since 3.1.0
123
- def inject_ports(config)
124
- config["hosts"] = config["hosts"].map do |host|
125
- host =~ /:/ ? host : "#{host}:27017"
126
- end
127
- config
128
- end
129
- end
130
- end
131
- end
@@ -1,93 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Sessions
4
- class MongoUri
5
-
6
- SCHEME = /(mongodb:\/\/)/
7
- USER = /([-.\w:]+)/
8
- PASS = /([^@,]+)/
9
- NODES = /((([-.\w]+)(?::(\w+))?,?)+)/
10
- DATABASE = /(?:\/([-\w]+))?/
11
-
12
- URI = /#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}/
13
-
14
- attr_reader :match
15
-
16
- # Get the database provided in the URI.
17
- #
18
- # @example Get the database.
19
- # uri.database
20
- #
21
- # @return [ String ] The database.
22
- #
23
- # @since 3.0.0
24
- def database
25
- @database ||= match[9]
26
- end
27
-
28
- # Get the hosts provided in the URI.
29
- #
30
- # @example Get the hosts.
31
- # uri.hosts
32
- #
33
- # @return [ Array<String> ] The hosts.
34
- #
35
- # @since 3.0.0
36
- def hosts
37
- @hosts ||= match[5].split(",")
38
- end
39
-
40
- # Create the new uri from the provided string.
41
- #
42
- # @example Create the new uri.
43
- # MongoUri.new(uri)
44
- #
45
- # @param [ String ] string The uri string.
46
- #
47
- # @since 3.0.0
48
- def initialize(string)
49
- @match = string.match(URI)
50
- end
51
-
52
- # Get the password provided in the URI.
53
- #
54
- # @example Get the password.
55
- # uri.password
56
- #
57
- # @return [ String ] The password.
58
- #
59
- # @since 3.0.0
60
- def password
61
- @password ||= match[4]
62
- end
63
-
64
- # Get the uri as a Mongoid friendly configuration hash.
65
- #
66
- # @example Get the uri as a hash.
67
- # uri.to_hash
68
- #
69
- # @return [ Hash ] The uri as options.
70
- #
71
- # @since 3.0.0
72
- def to_hash
73
- config = { database: database, hosts: hosts }
74
- if username && password
75
- config.merge!(username: username, password: password)
76
- end
77
- config
78
- end
79
-
80
- # Get the username provided in the URI.
81
- #
82
- # @example Get the username.
83
- # uri.username
84
- #
85
- # @return [ String ] The username.
86
- #
87
- # @since 3.0.0
88
- def username
89
- @username ||= match[3]
90
- end
91
- end
92
- end
93
- end