mongoid 2.0.0.beta.15 → 2.0.0.beta.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/lib/config/locales/en.yml +40 -0
  2. data/lib/config/locales/es.yml +41 -0
  3. data/lib/config/locales/fr.yml +42 -0
  4. data/lib/config/locales/it.yml +39 -0
  5. data/lib/config/locales/pl.yml +39 -0
  6. data/lib/config/locales/pt.yml +40 -0
  7. data/lib/config/locales/sv.yml +40 -0
  8. data/lib/mongoid.rb +7 -2
  9. data/lib/mongoid/associations.rb +16 -9
  10. data/lib/mongoid/associations/embedded_in.rb +11 -0
  11. data/lib/mongoid/associations/embeds_many.rb +28 -2
  12. data/lib/mongoid/associations/embeds_one.rb +18 -2
  13. data/lib/mongoid/associations/proxy.rb +28 -1
  14. data/lib/mongoid/associations/referenced_in.rb +10 -0
  15. data/lib/mongoid/associations/references_many.rb +10 -7
  16. data/lib/mongoid/associations/references_many_as_array.rb +29 -0
  17. data/lib/mongoid/associations/references_one.rb +9 -1
  18. data/lib/mongoid/attributes.rb +13 -3
  19. data/lib/mongoid/callbacks.rb +1 -0
  20. data/lib/mongoid/collections.rb +1 -1
  21. data/lib/mongoid/components.rb +1 -0
  22. data/lib/mongoid/config.rb +16 -1
  23. data/lib/mongoid/contexts/enumerable.rb +28 -1
  24. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  25. data/lib/mongoid/contexts/mongo.rb +13 -1
  26. data/lib/mongoid/criteria.rb +4 -2
  27. data/lib/mongoid/criterion/inclusion.rb +22 -1
  28. data/lib/mongoid/criterion/optional.rb +14 -39
  29. data/lib/mongoid/criterion/selector.rb +65 -0
  30. data/lib/mongoid/document.rb +5 -11
  31. data/lib/mongoid/errors.rb +10 -130
  32. data/lib/mongoid/errors/document_not_found.rb +29 -0
  33. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  34. data/lib/mongoid/errors/invalid_database.rb +20 -0
  35. data/lib/mongoid/errors/invalid_field.rb +19 -0
  36. data/lib/mongoid/errors/invalid_options.rb +16 -0
  37. data/lib/mongoid/errors/invalid_type.rb +26 -0
  38. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  39. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  40. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  41. data/lib/mongoid/errors/validations.rb +22 -0
  42. data/lib/mongoid/extensions/hash/assimilation.rb +1 -1
  43. data/lib/mongoid/extensions/hash/criteria_helpers.rb +5 -3
  44. data/lib/mongoid/extensions/object/conversions.rb +5 -1
  45. data/lib/mongoid/extensions/objectid/conversions.rb +43 -1
  46. data/lib/mongoid/field.rb +13 -6
  47. data/lib/mongoid/finders.rb +1 -1
  48. data/lib/mongoid/hierarchy.rb +9 -4
  49. data/lib/mongoid/identity.rb +2 -2
  50. data/lib/mongoid/indexes.rb +1 -1
  51. data/lib/mongoid/matchers/default.rb +1 -1
  52. data/lib/mongoid/modifiers.rb +24 -0
  53. data/lib/mongoid/modifiers/command.rb +18 -0
  54. data/lib/mongoid/modifiers/inc.rb +24 -0
  55. data/lib/mongoid/persistence/command.rb +2 -10
  56. data/lib/mongoid/persistence/remove_all.rb +1 -1
  57. data/lib/mongoid/railtie.rb +2 -0
  58. data/lib/mongoid/railties/database.rake +102 -11
  59. data/lib/mongoid/railties/document.rb +12 -0
  60. data/lib/mongoid/safe.rb +13 -0
  61. data/lib/mongoid/safety.rb +12 -0
  62. data/lib/mongoid/validations.rb +0 -4
  63. data/lib/mongoid/version.rb +1 -1
  64. data/lib/mongoid/versioning.rb +11 -1
  65. metadata +55 -28
  66. data/lib/mongoid/validations/locale/en.yml +0 -5
@@ -38,40 +38,6 @@ module Mongoid #:nodoc:
38
38
  @options[:cache] == true
39
39
  end
40
40
 
41
- # If the document is using BSON::ObjectIDs the convert the argument to
42
- # either an object id or an array of them if the supplied argument is an
43
- # Array. Otherwise just return.
44
- #
45
- # Options:
46
- # args: A +String+ or an +Array+ convert to +BSON::ObjectID+
47
- # cast: A +Boolean+ define if we can or not cast to BSON::ObjectID.
48
- # If false, we use the default type of args
49
- #
50
- # Example:
51
- #
52
- # <tt>Mongoid.cast_ids!("4ab2bc4b8ad548971900005c", true)</tt>
53
- # <tt>Mongoid.cast_ids!(["4ab2bc4b8ad548971900005c"])</tt>
54
- #
55
- # Returns:
56
- #
57
- # If using object ids:
58
- # An +Array+ of +BSON::ObjectID+ of each element if params is an +Array+
59
- # A +BSON::ObjectID+ from params if params is +String+
60
- # Otherwise:
61
- # <tt>args</tt>
62
- def cast_ids!(args, cast = true)
63
- return args if !using_object_ids? || args.is_a?(BSON::ObjectID) || !cast
64
- if args.is_a?(String)
65
- BSON::ObjectID(args)
66
- elsif args.is_a?(Array)
67
- args.map{ |a|
68
- a.is_a?(BSON::ObjectID) ? a : BSON::ObjectID(a)
69
- }
70
- else
71
- args
72
- end
73
- end
74
-
75
41
  # Adds fields to be sorted in descending order. Will add them in the order
76
42
  # they were passed into the method.
77
43
  #
@@ -136,9 +102,12 @@ module Mongoid #:nodoc:
136
102
  def id(*ids)
137
103
  ids.flatten!
138
104
  if ids.size > 1
139
- self.in(:_id => cast_ids!(ids, self.klass.primary_key.nil?))
105
+ self.in(
106
+ :_id => ::BSON::ObjectID.cast!(@klass, ids, @klass.primary_key.nil?)
107
+ )
140
108
  else
141
- @selector[:_id] = cast_ids!(ids.first, self.klass.primary_key.nil?)
109
+ @selector[:_id] =
110
+ ::BSON::ObjectID.cast!(@klass, ids.first, @klass.primary_key.nil?)
142
111
  end
143
112
  self
144
113
  end
@@ -183,10 +152,16 @@ module Mongoid #:nodoc:
183
152
  @options[:sort] = [] unless @options[:sort] || args.first.nil?
184
153
  arguments = args.first
185
154
  case arguments
186
- when Hash then arguments.each { |field, direction| @options[:sort] << [ field, direction ] }
187
- when Array then @options[:sort].concat(arguments)
155
+ when Hash
156
+ arguments.each do |field, direction|
157
+ @options[:sort] << [ field, direction ]
158
+ end
159
+ when Array
160
+ @options[:sort].concat(arguments)
188
161
  when Complex
189
- args.flatten.each { |complex| @options[:sort] << [ complex.key, complex.operator.to_sym ] }
162
+ args.flatten.each do |complex|
163
+ @options[:sort] << [ complex.key, complex.operator.to_sym ]
164
+ end
190
165
  end; self
191
166
  end
192
167
 
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Criterion #:nodoc:
4
+
5
+ class Selector < Hash
6
+ attr_reader :klass
7
+
8
+ def initialize(klass)
9
+ @klass = klass
10
+ end
11
+
12
+ def []=(key, value)
13
+ super(key, try_to_typecast(key, value))
14
+ end
15
+
16
+ def merge!(other)
17
+ other.each_pair do |key, value|
18
+ self[key] = value
19
+ end
20
+ self
21
+ end
22
+ alias update merge!
23
+
24
+ private
25
+
26
+ def try_to_typecast(key, value)
27
+ access = key.to_s
28
+ return value unless klass.fields.has_key?(access)
29
+
30
+ field = klass.fields[access]
31
+ typecast_value_for(field, value)
32
+ end
33
+
34
+ def typecast_value_for(field, value)
35
+ return field.set(value) if field.type === value
36
+ case value
37
+ when Hash
38
+ value = value.dup
39
+ value.each_pair do |k, v|
40
+ value[k] = typecast_hash_value(field, k, v)
41
+ end
42
+ when Array
43
+ value.map { |v| typecast_value_for(field, v) }
44
+ when Regexp
45
+ value
46
+ else
47
+ field.set(value)
48
+ end
49
+ end
50
+
51
+ def typecast_hash_value(field, key, value)
52
+ case key
53
+ when "$exists"
54
+ Boolean.set(value)
55
+ when "$size"
56
+ Integer.set(value)
57
+ else
58
+ typecast_value_for(field, value)
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -11,15 +11,6 @@ module Mongoid #:nodoc:
11
11
 
12
12
  module ClassMethods #:nodoc:
13
13
 
14
- # Perform default behavior but mark the hierarchy as being hereditary.
15
- #
16
- # This method must remain in the +Document+ module, even though its
17
- # behavior affects items in the Hierarchy module.
18
- def inherited(subclass)
19
- self.hereditary = true
20
- super
21
- end
22
-
23
14
  # Instantiate a new object, only when loaded from the database or when
24
15
  # the attributes have already been typecast.
25
16
  #
@@ -32,9 +23,9 @@ module Mongoid #:nodoc:
32
23
  document = allocate
33
24
  document.instance_variable_set(:@attributes, attributes)
34
25
  document.setup_modifications
35
- return document
26
+ document
36
27
  else
37
- return new(attrs)
28
+ new(attrs)
38
29
  end
39
30
  end
40
31
 
@@ -114,6 +105,9 @@ module Mongoid #:nodoc:
114
105
  @new_record = true
115
106
  document = yield self if block_given?
116
107
  identify
108
+ run_callbacks(:initialize) do
109
+ document
110
+ end
117
111
  end
118
112
 
119
113
  # Returns the class name plus its attributes.
@@ -1,131 +1,11 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc
3
- module Errors #:nodoc
4
-
5
- # Default parent Mongoid error for all custom errors
6
- class MongoidError < StandardError; end
7
-
8
- # Raised when querying the database for a document by a specific id which
9
- # does not exist. If multiple ids were passed then it will display all of
10
- # those.
11
- #
12
- # Example:
13
- #
14
- # <tt>DocumentNotFound.new(Person, ["1", "2"])</tt>
15
- class DocumentNotFound < MongoidError
16
- attr_reader :klass, :indentifiers
17
- def initialize(klass, ids)
18
- @klass = klass
19
- @identifiers = ids.is_a?(Array) ? ids.join(", ") : ids
20
- super("Document not found for class #{@klass} with id(s) #{@identifiers}")
21
- end
22
- end
23
-
24
- # Raised when invalid options are passed into a constructor or method.
25
- #
26
- # Example:
27
- #
28
- # <tt>InvalidOptions.new</tt>
29
- class InvalidOptions < MongoidError; end
30
-
31
- # Raised when the database connection has not been set up properly, either
32
- # by attempting to set an object on the db that is not a +Mongo::DB+, or
33
- # not setting anything at all.
34
- #
35
- # Example:
36
- #
37
- # <tt>InvalidDatabase.new("Not a DB")</tt>
38
- class InvalidDatabase < MongoidError
39
- attr_reader :database
40
- def initialize(database)
41
- @database = database
42
- super("Database should be a Mongo::DB, not #{@database.class.name}")
43
- end
44
- end
45
-
46
- # Raised when trying to get or set a value for a defined field, where the
47
- # type of the object does not match the defined field type.
48
- #
49
- # Example:
50
- #
51
- # <tt>InvalidType.new(Array, "Not an Array")</tt>
52
- class InvalidType < MongoidError
53
- def initialize(klass, value)
54
- super("Field was defined as a(n) #{klass.name}, but received a #{value.class.name} " +
55
- "with the value #{value.inspect}.")
56
- end
57
- end
58
-
59
- # Raised when the database version is not supported by Mongoid.
60
- #
61
- # Example:
62
- #
63
- # <tt>UnsupportedVersion.new(Mongo::ServerVersion.new("1.3.1"))</tt>
64
- class UnsupportedVersion < MongoidError
65
- def initialize(version)
66
- super("MongoDB #{version} not supported, please upgrade to #{Mongoid::MONGODB_VERSION}")
67
- end
68
- end
69
-
70
- # Raised when a persisence method ending in ! fails validation. The message
71
- # will contain the full error messages from the +Document+ in question.
72
- #
73
- # Example:
74
- #
75
- # <tt>Validations.new(person.errors)</tt>
76
- class Validations < MongoidError
77
- attr_reader :document
78
- def initialize(document)
79
- @document = document
80
- super("Validation Failed: #{@document.errors.full_messages.join(", ")}")
81
- end
82
- end
83
-
84
- # This error is raised when trying to access a Mongo::Collection from an
85
- # embedded document.
86
- #
87
- # Example:
88
- #
89
- # <tt>InvalidCollection.new(Address)</tt>
90
- class InvalidCollection < MongoidError
91
- attr_reader :klass
92
- def initialize(klass)
93
- @klass = klass
94
- super("Access to the collection for #{@klass.name} is not allowed " +
95
- "since it is an embedded document, please access a collection from " +
96
- "the root document")
97
- end
98
- end
99
-
100
- # This error is raised when trying to create a field that conflicts with
101
- # a Mongoid internal attribute or method.
102
- #
103
- # Example:
104
- #
105
- # <tt>InvalidField.new('collection')</tt>
106
- class InvalidField < MongoidError
107
- attr_reader :name
108
- def initialize(name)
109
- @name = name
110
- super("Defining a field named '#{@name}' is not allowed. " +
111
- "Do not define fields that conflict with Mongoid internal attributes " +
112
- "or method names. Use Document#instance_methods to see what " +
113
- "names this includes.")
114
- end
115
- end
116
-
117
- # This error is raised when trying to create set nested records above the
118
- # specified :limit
119
- #
120
- # Example:
121
- #
122
- #<tt>TooManyNestedAttributeRecords.new('association', limit)
123
- class TooManyNestedAttributeRecords < MongoidError
124
- attr_reader :association, :limit
125
- def initialize(association, limit)
126
- @association, @limit = association.to_s.humanize.capitalize, limit
127
- super("Accept Nested Attributes for #{@association} is limited to #{@limit} records")
128
- end
129
- end
130
- end
131
- end
2
+ require "mongoid/errors/mongoid_error"
3
+ require "mongoid/errors/document_not_found"
4
+ require "mongoid/errors/invalid_collection"
5
+ require "mongoid/errors/invalid_database"
6
+ require "mongoid/errors/invalid_field"
7
+ require "mongoid/errors/invalid_options"
8
+ require "mongoid/errors/invalid_type"
9
+ require "mongoid/errors/too_many_nested_attribute_records"
10
+ require "mongoid/errors/unsupported_version"
11
+ require "mongoid/errors/validations"
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when querying the database for a document by a specific id which
6
+ # does not exist. If multiple ids were passed then it will display all of
7
+ # those.
8
+ #
9
+ # Example:
10
+ #
11
+ # <tt>DocumentNotFound.new(Person, ["1", "2"])</tt>
12
+ class DocumentNotFound < MongoidError
13
+
14
+ attr_reader :klass, :indentifiers
15
+
16
+ def initialize(klass, ids)
17
+ @klass = klass
18
+ @identifiers = ids.is_a?(Array) ? ids.join(", ") : ids
19
+
20
+ super(
21
+ translate(
22
+ "document_not_found",
23
+ { :klass => klass.name, :identifiers => @identifiers }
24
+ )
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # This error is raised when trying to access a Mongo::Collection from an
6
+ # embedded document.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidCollection.new(Address)</tt>
11
+ class InvalidCollection < MongoidError
12
+ def initialize(klass)
13
+ super(
14
+ translate("invalid_collection", { :klass => klass.name })
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when the database connection has not been set up properly, either
6
+ # by attempting to set an object on the db that is not a +Mongo::DB+, or
7
+ # not setting anything at all.
8
+ #
9
+ # Example:
10
+ #
11
+ # <tt>InvalidDatabase.new("Not a DB")</tt>
12
+ class InvalidDatabase < MongoidError
13
+ def initialize(database)
14
+ super(
15
+ translate("invalid_database", { :name => database.class.name })
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # This error is raised when trying to create a field that conflicts with
6
+ # a Mongoid internal attribute or method.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidField.new('collection')</tt>
11
+ class InvalidField < MongoidError
12
+ def initialize(name)
13
+ super(
14
+ translate("invalid_field", { :name => name })
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when invalid options are passed into a constructor or method.
6
+ #
7
+ # Example:
8
+ #
9
+ # <tt>InvalidOptions.new</tt>
10
+ class InvalidOptions < MongoidError
11
+ def initialize(key, options)
12
+ super(translate(key, options))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when trying to get or set a value for a defined field, where the
6
+ # type of the object does not match the defined field type.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidType.new(Array, "Not an Array")</tt>
11
+ class InvalidType < MongoidError
12
+ def initialize(klass, value)
13
+ super(
14
+ translate(
15
+ "invalid_type",
16
+ {
17
+ :klass => klass.name,
18
+ :other => value.class.name,
19
+ :value => value.inspect
20
+ }
21
+ )
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end