mongoid-braxton 2.0.2

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 (226) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +50 -0
  3. data/Rakefile +51 -0
  4. data/lib/config/locales/bg.yml +41 -0
  5. data/lib/config/locales/de.yml +41 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +41 -0
  8. data/lib/config/locales/fr.yml +42 -0
  9. data/lib/config/locales/hu.yml +44 -0
  10. data/lib/config/locales/id.yml +46 -0
  11. data/lib/config/locales/it.yml +39 -0
  12. data/lib/config/locales/ja.yml +40 -0
  13. data/lib/config/locales/kr.yml +65 -0
  14. data/lib/config/locales/nl.yml +39 -0
  15. data/lib/config/locales/pl.yml +39 -0
  16. data/lib/config/locales/pt-BR.yml +40 -0
  17. data/lib/config/locales/pt.yml +40 -0
  18. data/lib/config/locales/ro.yml +46 -0
  19. data/lib/config/locales/ru.yml +41 -0
  20. data/lib/config/locales/sv.yml +40 -0
  21. data/lib/config/locales/vi.yml +45 -0
  22. data/lib/config/locales/zh-CN.yml +33 -0
  23. data/lib/mongoid.rb +140 -0
  24. data/lib/mongoid/atomicity.rb +111 -0
  25. data/lib/mongoid/attributes.rb +185 -0
  26. data/lib/mongoid/attributes/processing.rb +145 -0
  27. data/lib/mongoid/callbacks.rb +23 -0
  28. data/lib/mongoid/collection.rb +137 -0
  29. data/lib/mongoid/collections.rb +71 -0
  30. data/lib/mongoid/collections/master.rb +37 -0
  31. data/lib/mongoid/collections/operations.rb +42 -0
  32. data/lib/mongoid/collections/retry.rb +39 -0
  33. data/lib/mongoid/components.rb +45 -0
  34. data/lib/mongoid/config.rb +349 -0
  35. data/lib/mongoid/config/database.rb +167 -0
  36. data/lib/mongoid/config/replset_database.rb +78 -0
  37. data/lib/mongoid/contexts.rb +19 -0
  38. data/lib/mongoid/contexts/enumerable.rb +275 -0
  39. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  40. data/lib/mongoid/contexts/mongo.rb +345 -0
  41. data/lib/mongoid/copyable.rb +46 -0
  42. data/lib/mongoid/criteria.rb +357 -0
  43. data/lib/mongoid/criterion/builder.rb +34 -0
  44. data/lib/mongoid/criterion/complex.rb +34 -0
  45. data/lib/mongoid/criterion/creational.rb +34 -0
  46. data/lib/mongoid/criterion/exclusion.rb +108 -0
  47. data/lib/mongoid/criterion/inclusion.rb +198 -0
  48. data/lib/mongoid/criterion/inspection.rb +22 -0
  49. data/lib/mongoid/criterion/optional.rb +193 -0
  50. data/lib/mongoid/criterion/selector.rb +143 -0
  51. data/lib/mongoid/criterion/unconvertable.rb +20 -0
  52. data/lib/mongoid/cursor.rb +86 -0
  53. data/lib/mongoid/default_scope.rb +36 -0
  54. data/lib/mongoid/dirty.rb +253 -0
  55. data/lib/mongoid/document.rb +284 -0
  56. data/lib/mongoid/errors.rb +13 -0
  57. data/lib/mongoid/errors/document_not_found.rb +29 -0
  58. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  59. data/lib/mongoid/errors/invalid_database.rb +20 -0
  60. data/lib/mongoid/errors/invalid_field.rb +19 -0
  61. data/lib/mongoid/errors/invalid_options.rb +16 -0
  62. data/lib/mongoid/errors/invalid_type.rb +26 -0
  63. data/lib/mongoid/errors/mixed_relations.rb +37 -0
  64. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  65. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  66. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  67. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  68. data/lib/mongoid/errors/validations.rb +24 -0
  69. data/lib/mongoid/extensions.rb +123 -0
  70. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  71. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  72. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  74. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  75. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  76. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  77. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  78. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  79. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  80. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  81. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  84. data/lib/mongoid/extensions/object/checks.rb +32 -0
  85. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  86. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  87. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  88. data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
  89. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  90. data/lib/mongoid/extensions/range/conversions.rb +25 -0
  91. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  92. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  93. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  94. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  95. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  96. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  97. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  98. data/lib/mongoid/extras.rb +42 -0
  99. data/lib/mongoid/factory.rb +37 -0
  100. data/lib/mongoid/field.rb +162 -0
  101. data/lib/mongoid/fields.rb +183 -0
  102. data/lib/mongoid/finders.rb +127 -0
  103. data/lib/mongoid/hierarchy.rb +85 -0
  104. data/lib/mongoid/identity.rb +92 -0
  105. data/lib/mongoid/indexes.rb +38 -0
  106. data/lib/mongoid/inspection.rb +54 -0
  107. data/lib/mongoid/javascript.rb +21 -0
  108. data/lib/mongoid/javascript/functions.yml +37 -0
  109. data/lib/mongoid/json.rb +16 -0
  110. data/lib/mongoid/keys.rb +131 -0
  111. data/lib/mongoid/logger.rb +18 -0
  112. data/lib/mongoid/matchers.rb +32 -0
  113. data/lib/mongoid/matchers/all.rb +11 -0
  114. data/lib/mongoid/matchers/default.rb +70 -0
  115. data/lib/mongoid/matchers/exists.rb +13 -0
  116. data/lib/mongoid/matchers/gt.rb +11 -0
  117. data/lib/mongoid/matchers/gte.rb +11 -0
  118. data/lib/mongoid/matchers/in.rb +11 -0
  119. data/lib/mongoid/matchers/lt.rb +11 -0
  120. data/lib/mongoid/matchers/lte.rb +11 -0
  121. data/lib/mongoid/matchers/ne.rb +11 -0
  122. data/lib/mongoid/matchers/nin.rb +11 -0
  123. data/lib/mongoid/matchers/or.rb +30 -0
  124. data/lib/mongoid/matchers/size.rb +11 -0
  125. data/lib/mongoid/matchers/strategies.rb +63 -0
  126. data/lib/mongoid/multi_database.rb +11 -0
  127. data/lib/mongoid/multi_parameter_attributes.rb +82 -0
  128. data/lib/mongoid/named_scope.rb +137 -0
  129. data/lib/mongoid/nested_attributes.rb +51 -0
  130. data/lib/mongoid/observer.rb +67 -0
  131. data/lib/mongoid/paranoia.rb +103 -0
  132. data/lib/mongoid/paths.rb +61 -0
  133. data/lib/mongoid/persistence.rb +240 -0
  134. data/lib/mongoid/persistence/atomic.rb +88 -0
  135. data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -0
  136. data/lib/mongoid/persistence/atomic/inc.rb +28 -0
  137. data/lib/mongoid/persistence/atomic/operation.rb +44 -0
  138. data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
  139. data/lib/mongoid/persistence/atomic/push.rb +28 -0
  140. data/lib/mongoid/persistence/command.rb +71 -0
  141. data/lib/mongoid/persistence/insert.rb +53 -0
  142. data/lib/mongoid/persistence/insert_embedded.rb +43 -0
  143. data/lib/mongoid/persistence/remove.rb +44 -0
  144. data/lib/mongoid/persistence/remove_all.rb +40 -0
  145. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  146. data/lib/mongoid/persistence/update.rb +77 -0
  147. data/lib/mongoid/railtie.rb +139 -0
  148. data/lib/mongoid/railties/database.rake +171 -0
  149. data/lib/mongoid/railties/document.rb +12 -0
  150. data/lib/mongoid/relations.rb +107 -0
  151. data/lib/mongoid/relations/accessors.rb +175 -0
  152. data/lib/mongoid/relations/auto_save.rb +34 -0
  153. data/lib/mongoid/relations/binding.rb +26 -0
  154. data/lib/mongoid/relations/bindings.rb +9 -0
  155. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  156. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  157. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  158. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  159. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  160. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
  161. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  162. data/lib/mongoid/relations/builder.rb +42 -0
  163. data/lib/mongoid/relations/builders.rb +79 -0
  164. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  165. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  166. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  167. data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
  168. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  169. data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
  170. data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
  171. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  172. data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
  173. data/lib/mongoid/relations/cascading.rb +55 -0
  174. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  175. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  176. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  177. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  178. data/lib/mongoid/relations/constraint.rb +42 -0
  179. data/lib/mongoid/relations/cyclic.rb +103 -0
  180. data/lib/mongoid/relations/embedded/atomic.rb +86 -0
  181. data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
  182. data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
  183. data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
  184. data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
  185. data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
  186. data/lib/mongoid/relations/embedded/in.rb +173 -0
  187. data/lib/mongoid/relations/embedded/many.rb +499 -0
  188. data/lib/mongoid/relations/embedded/one.rb +170 -0
  189. data/lib/mongoid/relations/macros.rb +310 -0
  190. data/lib/mongoid/relations/many.rb +215 -0
  191. data/lib/mongoid/relations/metadata.rb +539 -0
  192. data/lib/mongoid/relations/nested_builder.rb +68 -0
  193. data/lib/mongoid/relations/one.rb +47 -0
  194. data/lib/mongoid/relations/polymorphic.rb +54 -0
  195. data/lib/mongoid/relations/proxy.rb +143 -0
  196. data/lib/mongoid/relations/referenced/batch.rb +71 -0
  197. data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
  198. data/lib/mongoid/relations/referenced/in.rb +216 -0
  199. data/lib/mongoid/relations/referenced/many.rb +516 -0
  200. data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
  201. data/lib/mongoid/relations/referenced/one.rb +222 -0
  202. data/lib/mongoid/relations/reflections.rb +45 -0
  203. data/lib/mongoid/safe.rb +23 -0
  204. data/lib/mongoid/safety.rb +207 -0
  205. data/lib/mongoid/scope.rb +31 -0
  206. data/lib/mongoid/serialization.rb +99 -0
  207. data/lib/mongoid/sharding.rb +51 -0
  208. data/lib/mongoid/state.rb +67 -0
  209. data/lib/mongoid/timestamps.rb +14 -0
  210. data/lib/mongoid/timestamps/created.rb +31 -0
  211. data/lib/mongoid/timestamps/updated.rb +33 -0
  212. data/lib/mongoid/validations.rb +124 -0
  213. data/lib/mongoid/validations/associated.rb +44 -0
  214. data/lib/mongoid/validations/referenced.rb +58 -0
  215. data/lib/mongoid/validations/uniqueness.rb +85 -0
  216. data/lib/mongoid/version.rb +4 -0
  217. data/lib/mongoid/versioning.rb +113 -0
  218. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  219. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
  220. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  221. data/lib/rails/generators/mongoid/model/templates/model.rb +19 -0
  222. data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
  223. data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
  224. data/lib/rails/generators/mongoid_generator.rb +70 -0
  225. data/lib/rails/mongoid.rb +58 -0
  226. metadata +406 -0
@@ -0,0 +1,127 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # This module defines the finder methods that hang off the document at the
5
+ # class level.
6
+ module Finders
7
+
8
+ # Delegate to the criteria methods that are natural for creating a new
9
+ # criteria.
10
+ [ :all_in, :any_in, :any_of, :asc, :ascending, :avg, :desc, :descending,
11
+ :excludes, :limit, :max, :min, :not_in, :only, :order_by,
12
+ :skip, :sum, :without, :where, :update, :update_all, :near ].each do |name|
13
+ define_method(name) do |*args|
14
+ criteria.send(name, *args)
15
+ end
16
+ end
17
+
18
+ # Find +Documents+ given the conditions.
19
+ #
20
+ # Options:
21
+ #
22
+ # args: A +Hash+ with a conditions key and other options
23
+ #
24
+ # <tt>Person.all(:conditions => { :attribute => "value" })</tt>
25
+ def all(*args)
26
+ find(:all, *args)
27
+ end
28
+
29
+ # Returns a count of matching records in the database based on the
30
+ # provided arguments.
31
+ #
32
+ # <tt>Person.count(:conditions => { :attribute => "value" })</tt>
33
+ def count(*args)
34
+ find(:all, *args).count
35
+ end
36
+
37
+ # Returns true if count is zero
38
+ def empty?
39
+ count == 0
40
+ end
41
+
42
+ # Returns true if there are on document in database based on the
43
+ # provided arguments.
44
+ #
45
+ # <tt>Person.exists?(:conditions => { :attribute => "value" })</tt>
46
+ def exists?(*args)
47
+ find(:all, *args).limit(1).count == 1
48
+ end
49
+
50
+ # Find a +Document+ in several different ways.
51
+ #
52
+ # If a +String+ is provided, it will be assumed that it is a
53
+ # representation of a Mongo::ObjectID and will attempt to find a single
54
+ # +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
55
+ # it will attempt to find either a single +Document+ or multiples based
56
+ # on the conditions provided and the first parameter.
57
+ #
58
+ # Example:
59
+ #
60
+ # <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
61
+ # <tt>Person.find(:all, :conditions => { :attribute => "value" })</tt>
62
+ # <tt>Person.find(BSON::ObjectId)</tt>
63
+ #
64
+ # Options:
65
+ #
66
+ # args: An assortment of finder options.
67
+ #
68
+ # Returns:
69
+ #
70
+ # A document or criteria.
71
+ def find(*args)
72
+ criteria.find(*args)
73
+ end
74
+
75
+ # Find the first +Document+ given the conditions, or creates a new document
76
+ # with the conditions that were supplied
77
+ #
78
+ # Options:
79
+ #
80
+ # args: A +Hash+ of attributes
81
+ #
82
+ # <tt>Person.find_or_create_by(:attribute => "value")</tt>
83
+ def find_or_create_by(attrs = {}, &block)
84
+ find_or(:create, attrs, &block)
85
+ end
86
+
87
+ # Find the first +Document+ given the conditions, or instantiates a new document
88
+ # with the conditions that were supplied
89
+ #
90
+ # Options:
91
+ #
92
+ # args: A +Hash+ of attributes
93
+ #
94
+ # <tt>Person.find_or_initialize_by(:attribute => "value")</tt>
95
+ def find_or_initialize_by(attrs = {}, &block)
96
+ find_or(:new, attrs, &block)
97
+ end
98
+
99
+ # Find the first +Document+ given the conditions.
100
+ #
101
+ # Options:
102
+ #
103
+ # args: A +Hash+ with a conditions key and other options
104
+ #
105
+ # <tt>Person.first(:conditions => { :attribute => "value" })</tt>
106
+ def first(*args)
107
+ find(:first, *args)
108
+ end
109
+
110
+ # Find the last +Document+ given the conditions.
111
+ #
112
+ # Options:
113
+ #
114
+ # args: A +Hash+ with a conditions key and other options
115
+ #
116
+ # <tt>Person.last(:conditions => { :attribute => "value" })</tt>
117
+ def last(*args)
118
+ find(:last, *args)
119
+ end
120
+
121
+ protected
122
+ # Find the first object or create/initialize it.
123
+ def find_or(method, attrs = {}, &block)
124
+ first(:conditions => attrs) || send(method, attrs, &block)
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Hierarchy #:nodoc
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ attr_accessor :_parent
7
+ end
8
+
9
+ module ClassMethods #:nodoc:
10
+
11
+ # Determines if the document is a subclass of another document.
12
+ #
13
+ # @example Check if the document is a subclass.
14
+ # Square.hereditary?
15
+ #
16
+ # @return [ true, false ] True if hereditary, false if not.
17
+ def hereditary?
18
+ Mongoid::Document > superclass
19
+ end
20
+ end
21
+
22
+ module InstanceMethods #:nodoc:
23
+
24
+ # Get all child +Documents+ to this +Document+, going n levels deep if
25
+ # necessary. This is used when calling update persistence operations from
26
+ # the root document, where changes in the entire tree need to be
27
+ # determined. Note that persistence from the embedded documents will
28
+ # always be preferred, since they are optimized calls... This operation
29
+ # can get expensive in domains with large hierarchies.
30
+ #
31
+ # @example Get all the document's children.
32
+ # person._children
33
+ #
34
+ # @return [ Array<Document> ] All child documents in the hierarchy.
35
+ def _children
36
+ relations.inject([]) do |children, (name, metadata)|
37
+ children.tap do |kids|
38
+ if metadata.embedded? && name != "versions"
39
+ child = send(name)
40
+ child.to_a.each do |doc|
41
+ kids.push(doc).concat(doc._children)
42
+ end unless child.blank?
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # Determines if the document is a subclass of another document.
49
+ #
50
+ # @example Check if the document is a subclass
51
+ # Square.new.hereditary?
52
+ #
53
+ # @return [ true, false ] True if hereditary, false if not.
54
+ def hereditary?
55
+ self.class.hereditary?
56
+ end
57
+
58
+ # Sets up a child/parent association. This is used for newly created
59
+ # objects so they can be properly added to the graph.
60
+ #
61
+ # @example Set the parent document.
62
+ # document.parentize(parent)
63
+ #
64
+ # @param [ Document ] document The parent document.
65
+ #
66
+ # @return [ Document ] The parent document.
67
+ def parentize(document)
68
+ self._parent = document
69
+ end
70
+
71
+ # Return the root document in the object graph. If the current document
72
+ # is the root object in the graph it will return self.
73
+ #
74
+ # @example Get the root document in the hierarchy.
75
+ # document._root
76
+ #
77
+ # @return [ Document ] The root document in the hierarchy.
78
+ def _root
79
+ object = self
80
+ while (object._parent) do object = object._parent; end
81
+ object || self
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ class Identity #:nodoc:
4
+
5
+ attr_reader :document
6
+
7
+ # Create the identity for the document. The id will be set in either in
8
+ # the form of a Mongo object id or a composite key set up by defining
9
+ # a key on the document. The _type will be set to the document's class
10
+ # name.
11
+ #
12
+ # @example Create the id and set the type.
13
+ # identity.create
14
+ def create
15
+ identify.tap { type }
16
+ end
17
+
18
+ # Create the new identity generator - this will be expanded in the future
19
+ # to support pk generators.
20
+ #
21
+ # @example
22
+ # Identity.new(document)
23
+ #
24
+ # @param [ Document ] document The document to generate an id for.
25
+ #
26
+ # @return [ Identity ] The new identity object.
27
+ def initialize(document)
28
+ @document = document
29
+ end
30
+
31
+ private
32
+
33
+ # Return the proper id for the document. Will be an object id or its string
34
+ # representation depending on the configuration.
35
+ #
36
+ # @example Generate the id.
37
+ # identity.generate_id
38
+ #
39
+ # @return [ Object ] The bson object id or its string equivalent.
40
+ def generate_id
41
+ id = BSON::ObjectId.new
42
+ document.using_object_ids? ? id : id.to_s
43
+ end
44
+
45
+ # Sets the id on the document. Will either set a newly generated id or
46
+ # build the composite key.
47
+ #
48
+ # @example Set the id.
49
+ # identity.identify
50
+ def identify
51
+ if !document.embedded? || Mongoid.embedded_object_id
52
+ document.id = compose.join(" ").identify if document.primary_key
53
+ document.id = generate_id if document.id.blank?
54
+ end
55
+ document.id
56
+ end
57
+
58
+ # Set the _type field on the document if the document is hereditary or in a
59
+ # polymorphic relation.
60
+ #
61
+ # @example Set the type.
62
+ # identity.type
63
+ def type
64
+ document._type = document.class.name if typed?
65
+ end
66
+
67
+ # Generates the array of keys to build the id.
68
+ #
69
+ # @example Build the array for the keys.
70
+ # identity.compose.
71
+ #
72
+ # @return [ Array<Object> ] The array of keys.
73
+ def compose
74
+ document.primary_key.collect do |key|
75
+ document.attributes[key.to_s]
76
+ end.reject { |val| val.nil? }
77
+ end
78
+
79
+ # Determines if the document stores the type information. This is if it is
80
+ # in a hierarchy, has subclasses, or is in a polymorphic relation.
81
+ #
82
+ # @example Check if the document is typed.
83
+ # identity.typed?
84
+ #
85
+ # @return [ true, false ] True if typed, false if not.
86
+ def typed?
87
+ document.hereditary? ||
88
+ document.class.descendants.any? ||
89
+ document.polymorphic?
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Indexes #:nodoc
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ cattr_accessor :index_options
7
+ self.index_options = {}
8
+ end
9
+
10
+ module ClassMethods #:nodoc
11
+
12
+ # Send the actual index creation comments to the MongoDB driver
13
+ def create_indexes
14
+ return unless index_options
15
+ current_collection = self._collection || set_collection
16
+ index_options.each do |name, options|
17
+ current_collection.create_index(name, options)
18
+ end
19
+ end
20
+
21
+ # Add the default indexes to the root document if they do not already
22
+ # exist. Currently this is only _type.
23
+ def add_indexes
24
+ if hereditary? && !index_options[:_type]
25
+ self.index_options[:_type] = {:unique => false, :background => true}
26
+ end
27
+ create_indexes if Mongoid.autocreate_indexes
28
+ end
29
+
30
+ # Adds an index on the field specified. Options can be :unique => true or
31
+ # :unique => false. It will default to the latter.
32
+ def index(name, options = { :unique => false })
33
+ self.index_options[name] = options
34
+ create_indexes if Mongoid.autocreate_indexes
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+
4
+ # Contains the bahviour around inspecting documents via
5
+ # <tt>Object#inspect</tt>.
6
+ module Inspection
7
+
8
+ # Returns the class name plus its attributes. If using dynamic fields will
9
+ # include those as well.
10
+ #
11
+ # @example Inspect the document.
12
+ # person.inspect
13
+ #
14
+ # @return [ String ] A nice pretty string to look at.
15
+ def inspect
16
+ inspection = []
17
+ inspection.concat(inspect_fields).concat(inspect_dynamic_fields)
18
+ "#<#{self.class.name} _id: #{id}, #{inspection * ', '}>"
19
+ end
20
+
21
+ private
22
+
23
+ # Get an array of inspected fields for the document.
24
+ #
25
+ # @example Inspect the defined fields.
26
+ # document.inspect_fields
27
+ #
28
+ # @return [ String ] An array of pretty printed field values.
29
+ def inspect_fields
30
+ fields.map do |name, field|
31
+ unless name == "_id"
32
+ "#{name}: #{@attributes[name].inspect}"
33
+ end
34
+ end.compact
35
+ end
36
+
37
+ # Get an array of inspected dynamic fields for the document.
38
+ #
39
+ # @example Inspect the dynamic fields.
40
+ # document.inspect_dynamic_fields
41
+ #
42
+ # @return [ String ] An array of pretty printed dynamic field values.
43
+ def inspect_dynamic_fields
44
+ if Mongoid.allow_dynamic_fields
45
+ keys = @attributes.keys - fields.keys - relations.keys - ["_id", "_type"]
46
+ return keys.map do |name|
47
+ "#{name}: #{@attributes[name].inspect}"
48
+ end
49
+ else
50
+ []
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ class Javascript
4
+ # Constant for the file that defines all the js functions.
5
+ FUNCTIONS = File.join(File.dirname(__FILE__), "javascript", "functions.yml")
6
+
7
+ # Load the javascript functions and define a class method for each one,
8
+ # that memoizes the value.
9
+ #
10
+ # Example:
11
+ #
12
+ # <tt>Mongoid::Javascript.aggregate</tt>
13
+ YAML.load(File.read(FUNCTIONS)).each_pair do |key, function|
14
+ (class << self; self; end).class_eval <<-EOT
15
+ def #{key}
16
+ @#{key} ||= "#{function}"
17
+ end
18
+ EOT
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ aggregate:
2
+ "function(obj, prev) {
3
+ prev.count++;
4
+ }"
5
+
6
+ group:
7
+ "function(obj, prev) {
8
+ prev.group.push(obj);
9
+ }"
10
+
11
+ max:
12
+ "function(obj, prev) {
13
+ if (prev.max == 'start') {
14
+ prev.max = obj.[field];
15
+ }
16
+ if (prev.max < obj.[field]) {
17
+ prev.max = obj.[field];
18
+ }
19
+ }"
20
+
21
+ min:
22
+ "function(obj, prev) {
23
+ if (prev.min == 'start') {
24
+ prev.min = obj.[field];
25
+ }
26
+ if (prev.min > obj.[field]) {
27
+ prev.min = obj.[field];
28
+ }
29
+ }"
30
+
31
+ sum:
32
+ "function(obj, prev) {
33
+ if (prev.sum == 'start') {
34
+ prev.sum = 0;
35
+ }
36
+ prev.sum += obj.[field];
37
+ }"