mongoid 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -1
- data/Rakefile +3 -2
- data/lib/config/locales/bg.yml +6 -0
- data/lib/config/locales/de.yml +6 -0
- data/lib/config/locales/en-GB.yml +48 -0
- data/lib/config/locales/en.yml +6 -3
- data/lib/config/locales/es.yml +6 -0
- data/lib/config/locales/fr.yml +6 -0
- data/lib/config/locales/hi.yml +39 -0
- data/lib/config/locales/hu.yml +13 -7
- data/lib/config/locales/id.yml +3 -0
- data/lib/config/locales/it.yml +7 -1
- data/lib/config/locales/ja.yml +4 -1
- data/lib/config/locales/kr.yml +9 -34
- data/lib/config/locales/nl.yml +6 -0
- data/lib/config/locales/pl.yml +6 -0
- data/lib/config/locales/pt-BR.yml +6 -0
- data/lib/config/locales/pt.yml +6 -0
- data/lib/config/locales/ro.yml +6 -0
- data/lib/config/locales/ru.yml +6 -0
- data/lib/config/locales/sv.yml +6 -0
- data/lib/config/locales/vi.yml +3 -0
- data/lib/config/locales/zh-CN.yml +6 -0
- data/lib/mongoid.rb +51 -45
- data/lib/mongoid/atomic.rb +145 -0
- data/lib/mongoid/atomic/modifiers.rb +109 -0
- data/lib/mongoid/atomic/paths.rb +3 -0
- data/lib/mongoid/atomic/paths/embedded.rb +43 -0
- data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
- data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
- data/lib/mongoid/atomic/paths/root.rb +40 -0
- data/lib/mongoid/attributes.rb +12 -23
- data/lib/mongoid/attributes/processing.rb +5 -5
- data/lib/mongoid/callbacks.rb +2 -0
- data/lib/mongoid/collection.rb +12 -59
- data/lib/mongoid/collections.rb +23 -20
- data/lib/mongoid/collections/master.rb +6 -4
- data/lib/mongoid/collections/operations.rb +1 -0
- data/lib/mongoid/collections/retry.rb +7 -0
- data/lib/mongoid/components.rb +2 -2
- data/lib/mongoid/config.rb +42 -55
- data/lib/mongoid/config/database.rb +6 -2
- data/lib/mongoid/config/replset_database.rb +7 -3
- data/lib/mongoid/contexts.rb +9 -3
- data/lib/mongoid/contexts/enumerable.rb +7 -3
- data/lib/mongoid/contexts/mongo.rb +139 -101
- data/lib/mongoid/criteria.rb +86 -69
- data/lib/mongoid/criterion/complex.rb +32 -5
- data/lib/mongoid/criterion/inclusion.rb +4 -2
- data/lib/mongoid/criterion/optional.rb +111 -86
- data/lib/mongoid/criterion/selector.rb +8 -4
- data/lib/mongoid/cursor.rb +27 -27
- data/lib/mongoid/dirty.rb +54 -214
- data/lib/mongoid/document.rb +37 -39
- data/lib/mongoid/errors/document_not_found.rb +3 -4
- data/lib/mongoid/errors/invalid_collection.rb +2 -3
- data/lib/mongoid/errors/invalid_database.rb +2 -3
- data/lib/mongoid/errors/invalid_field.rb +2 -3
- data/lib/mongoid/errors/invalid_options.rb +19 -7
- data/lib/mongoid/errors/invalid_type.rb +2 -3
- data/lib/mongoid/errors/mongoid_error.rb +5 -6
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -3
- data/lib/mongoid/errors/unsupported_version.rb +2 -3
- data/lib/mongoid/errors/validations.rb +2 -3
- data/lib/mongoid/extensions.rb +8 -62
- data/lib/mongoid/extensions/array/deletion.rb +29 -0
- data/lib/mongoid/extensions/false_class/equality.rb +14 -1
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +21 -10
- data/lib/mongoid/extensions/hash/scoping.rb +14 -1
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -1
- data/lib/mongoid/extensions/object/reflections.rb +33 -2
- data/lib/mongoid/extensions/object_id/conversions.rb +2 -36
- data/lib/mongoid/extensions/proc/scoping.rb +14 -1
- data/lib/mongoid/extensions/string/conversions.rb +4 -16
- data/lib/mongoid/extensions/string/inflections.rb +35 -14
- data/lib/mongoid/extensions/symbol/inflections.rb +38 -12
- data/lib/mongoid/extensions/true_class/equality.rb +14 -1
- data/lib/mongoid/extras.rb +11 -30
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields.rb +121 -29
- data/lib/mongoid/fields/mappings.rb +36 -0
- data/lib/mongoid/fields/serializable.rb +131 -0
- data/lib/mongoid/fields/serializable/array.rb +64 -0
- data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
- data/lib/mongoid/fields/serializable/bignum.rb +10 -0
- data/lib/mongoid/fields/serializable/binary.rb +11 -0
- data/lib/mongoid/fields/serializable/boolean.rb +44 -0
- data/lib/mongoid/fields/serializable/date.rb +51 -0
- data/lib/mongoid/fields/serializable/date_time.rb +28 -0
- data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
- data/lib/mongoid/fields/serializable/float.rb +33 -0
- data/lib/mongoid/fields/serializable/foreign_keys/array.rb +56 -0
- data/lib/mongoid/fields/serializable/foreign_keys/object.rb +43 -0
- data/lib/mongoid/fields/serializable/hash.rb +25 -0
- data/lib/mongoid/fields/serializable/integer.rb +33 -0
- data/lib/mongoid/fields/serializable/object.rb +11 -0
- data/lib/mongoid/fields/serializable/object_id.rb +32 -0
- data/lib/mongoid/fields/serializable/range.rb +42 -0
- data/lib/mongoid/fields/serializable/set.rb +42 -0
- data/lib/mongoid/fields/serializable/string.rb +28 -0
- data/lib/mongoid/fields/serializable/symbol.rb +28 -0
- data/lib/mongoid/fields/serializable/time.rb +12 -0
- data/lib/mongoid/fields/serializable/time_with_zone.rb +12 -0
- data/lib/mongoid/fields/serializable/timekeeping.rb +102 -0
- data/lib/mongoid/finders.rb +61 -37
- data/lib/mongoid/hierarchy.rb +43 -8
- data/lib/mongoid/identity_map.rb +106 -0
- data/lib/mongoid/indexes.rb +17 -1
- data/lib/mongoid/javascript.rb +2 -3
- data/lib/mongoid/keys.rb +10 -21
- data/lib/mongoid/logger.rb +22 -1
- data/lib/mongoid/matchers/all.rb +10 -0
- data/lib/mongoid/matchers/default.rb +1 -1
- data/lib/mongoid/matchers/exists.rb +10 -0
- data/lib/mongoid/matchers/gt.rb +10 -0
- data/lib/mongoid/matchers/gte.rb +10 -0
- data/lib/mongoid/matchers/in.rb +10 -0
- data/lib/mongoid/matchers/lt.rb +10 -0
- data/lib/mongoid/matchers/lte.rb +10 -0
- data/lib/mongoid/matchers/ne.rb +10 -0
- data/lib/mongoid/matchers/nin.rb +10 -0
- data/lib/mongoid/matchers/or.rb +7 -4
- data/lib/mongoid/matchers/size.rb +10 -0
- data/lib/mongoid/multi_database.rb +26 -6
- data/lib/mongoid/multi_parameter_attributes.rb +40 -17
- data/lib/mongoid/named_scope.rb +1 -2
- data/lib/mongoid/nested_attributes.rb +4 -1
- data/lib/mongoid/observer.rb +108 -5
- data/lib/mongoid/paranoia.rb +26 -26
- data/lib/mongoid/persistence.rb +15 -21
- data/lib/mongoid/persistence/atomic.rb +135 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +11 -8
- data/lib/mongoid/persistence/atomic/bit.rb +37 -0
- data/lib/mongoid/persistence/atomic/inc.rb +9 -6
- data/lib/mongoid/persistence/atomic/operation.rb +48 -7
- data/lib/mongoid/persistence/atomic/pop.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +10 -9
- data/lib/mongoid/persistence/atomic/push.rb +8 -5
- data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
- data/lib/mongoid/persistence/atomic/rename.rb +31 -0
- data/lib/mongoid/persistence/atomic/set.rb +30 -0
- data/lib/mongoid/persistence/atomic/unset.rb +28 -0
- data/lib/mongoid/persistence/deletion.rb +32 -0
- data/lib/mongoid/persistence/insertion.rb +41 -0
- data/lib/mongoid/persistence/modification.rb +37 -0
- data/lib/mongoid/persistence/operations.rb +214 -0
- data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
- data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
- data/lib/mongoid/persistence/operations/insert.rb +34 -0
- data/lib/mongoid/persistence/operations/remove.rb +33 -0
- data/lib/mongoid/persistence/operations/update.rb +53 -0
- data/lib/mongoid/railtie.rb +21 -33
- data/lib/mongoid/railties/database.rake +12 -12
- data/lib/mongoid/relations.rb +9 -5
- data/lib/mongoid/relations/accessors.rb +15 -36
- data/lib/mongoid/relations/auto_save.rb +2 -2
- data/lib/mongoid/relations/binding.rb +28 -1
- data/lib/mongoid/relations/bindings/embedded/in.rb +17 -30
- data/lib/mongoid/relations/bindings/embedded/many.rb +16 -21
- data/lib/mongoid/relations/bindings/embedded/one.rb +11 -16
- data/lib/mongoid/relations/bindings/referenced/in.rb +31 -32
- data/lib/mongoid/relations/bindings/referenced/many.rb +19 -61
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -63
- data/lib/mongoid/relations/bindings/referenced/one.rb +18 -26
- data/lib/mongoid/relations/builder.rb +4 -2
- data/lib/mongoid/relations/builders.rb +21 -2
- data/lib/mongoid/relations/builders/embedded/in.rb +5 -1
- data/lib/mongoid/relations/builders/embedded/many.rb +12 -4
- data/lib/mongoid/relations/builders/embedded/one.rb +5 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +2 -2
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/in.rb +2 -5
- data/lib/mongoid/relations/builders/referenced/many.rb +2 -3
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +14 -5
- data/lib/mongoid/relations/builders/referenced/one.rb +2 -3
- data/lib/mongoid/relations/embedded/atomic.rb +2 -2
- data/lib/mongoid/relations/embedded/in.rb +72 -41
- data/lib/mongoid/relations/embedded/many.rb +116 -120
- data/lib/mongoid/relations/embedded/one.rb +59 -41
- data/lib/mongoid/relations/embedded/sort.rb +31 -0
- data/lib/mongoid/relations/macros.rb +28 -24
- data/lib/mongoid/relations/many.rb +10 -103
- data/lib/mongoid/relations/metadata.rb +335 -38
- data/lib/mongoid/relations/one.rb +7 -32
- data/lib/mongoid/relations/options.rb +47 -0
- data/lib/mongoid/relations/proxy.rb +29 -28
- data/lib/mongoid/relations/referenced/batch.rb +2 -3
- data/lib/mongoid/relations/referenced/in.rb +66 -53
- data/lib/mongoid/relations/referenced/many.rb +216 -143
- data/lib/mongoid/relations/referenced/many_to_many.rb +132 -163
- data/lib/mongoid/relations/referenced/one.rb +76 -58
- data/lib/mongoid/relations/synchronization.rb +113 -0
- data/lib/mongoid/relations/targets.rb +2 -0
- data/lib/mongoid/relations/targets/enumerable.rb +329 -0
- data/lib/mongoid/safety.rb +24 -156
- data/lib/mongoid/serialization.rb +21 -0
- data/lib/mongoid/state.rb +34 -0
- data/lib/mongoid/threaded.rb +175 -0
- data/lib/mongoid/timestamps/updated.rb +1 -1
- data/lib/mongoid/validations.rb +3 -7
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +61 -7
- data/lib/rack/mongoid.rb +2 -0
- data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +1 -1
- data/lib/rails/generators/mongoid/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +1 -1
- data/lib/rails/generators/mongoid/observer/templates/{observer.rb → observer.rb.tt} +0 -0
- data/lib/rails/mongoid.rb +17 -17
- metadata +136 -102
- data/lib/mongoid/atomicity.rb +0 -111
- data/lib/mongoid/collections/cyclic_iterator.rb +0 -34
- data/lib/mongoid/collections/slaves.rb +0 -61
- data/lib/mongoid/extensions/array/conversions.rb +0 -23
- data/lib/mongoid/extensions/array/parentization.rb +0 -13
- data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
- data/lib/mongoid/extensions/binary/conversions.rb +0 -17
- data/lib/mongoid/extensions/boolean/conversions.rb +0 -27
- data/lib/mongoid/extensions/date/conversions.rb +0 -25
- data/lib/mongoid/extensions/datetime/conversions.rb +0 -12
- data/lib/mongoid/extensions/float/conversions.rb +0 -20
- data/lib/mongoid/extensions/hash/conversions.rb +0 -19
- data/lib/mongoid/extensions/integer/conversions.rb +0 -20
- data/lib/mongoid/extensions/object/conversions.rb +0 -25
- data/lib/mongoid/extensions/range/conversions.rb +0 -25
- data/lib/mongoid/extensions/set/conversions.rb +0 -20
- data/lib/mongoid/extensions/symbol/conversions.rb +0 -21
- data/lib/mongoid/extensions/time_conversions.rb +0 -38
- data/lib/mongoid/field.rb +0 -162
- data/lib/mongoid/paths.rb +0 -61
- data/lib/mongoid/persistence/command.rb +0 -71
- data/lib/mongoid/persistence/insert.rb +0 -53
- data/lib/mongoid/persistence/insert_embedded.rb +0 -43
- data/lib/mongoid/persistence/remove.rb +0 -44
- data/lib/mongoid/persistence/remove_all.rb +0 -40
- data/lib/mongoid/persistence/remove_embedded.rb +0 -48
- data/lib/mongoid/persistence/update.rb +0 -77
- data/lib/mongoid/safe.rb +0 -23
- data/lib/mongoid/validations/referenced.rb +0 -58
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
module ForeignKeys #:nodoc:
|
6
|
+
|
7
|
+
# Defines the behaviour for array fields.
|
8
|
+
class Array
|
9
|
+
include Serializable
|
10
|
+
|
11
|
+
# Get the default value for the field. If the default is a proc call
|
12
|
+
# it, otherwise clone the array.
|
13
|
+
#
|
14
|
+
# @example Get the default.
|
15
|
+
# field.default
|
16
|
+
#
|
17
|
+
# @return [ Object ] The default value cloned.
|
18
|
+
#
|
19
|
+
# @since 2.1.0
|
20
|
+
def default
|
21
|
+
default_value.dup
|
22
|
+
end
|
23
|
+
|
24
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
25
|
+
# compatible object to store.
|
26
|
+
#
|
27
|
+
# @example Serialize the field.
|
28
|
+
# field.serialize(object)
|
29
|
+
#
|
30
|
+
# @param [ Object ] object The object to cast.
|
31
|
+
#
|
32
|
+
# @return [ Array ] The converted object.
|
33
|
+
#
|
34
|
+
# @since 2.1.0
|
35
|
+
def serialize(object)
|
36
|
+
object.blank? ? [] : constraint.convert(object)
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
# Get the constraint from the metadata once.
|
42
|
+
#
|
43
|
+
# @example Get the constraint.
|
44
|
+
# field.constraint
|
45
|
+
#
|
46
|
+
# @return [ Constraint ] The relation's contraint.
|
47
|
+
#
|
48
|
+
# @since 2.1.0
|
49
|
+
def constraint
|
50
|
+
@constraint ||= options[:metadata].constraint
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
module ForeignKeys #:nodoc:
|
6
|
+
|
7
|
+
# Defines the behaviour for integer foreign key fields.
|
8
|
+
class Object
|
9
|
+
include Serializable
|
10
|
+
|
11
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
12
|
+
# compatible object to store.
|
13
|
+
#
|
14
|
+
# @example Serialize the field.
|
15
|
+
# field.serialize(object)
|
16
|
+
#
|
17
|
+
# @param [ Object ] object The object to cast.
|
18
|
+
#
|
19
|
+
# @return [ Array ] The converted object.
|
20
|
+
#
|
21
|
+
# @since 2.1.0
|
22
|
+
def serialize(object)
|
23
|
+
object.blank? ? nil : constraint.convert(object)
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# Get the constraint from the metadata once.
|
29
|
+
#
|
30
|
+
# @example Get the constraint.
|
31
|
+
# field.constraint
|
32
|
+
#
|
33
|
+
# @return [ Constraint ] The relation's contraint.
|
34
|
+
#
|
35
|
+
# @since 2.1.0
|
36
|
+
def constraint
|
37
|
+
@constraint ||= options[:metadata].constraint
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
# Defines the behaviour for hash fields.
|
6
|
+
class Hash
|
7
|
+
include Serializable
|
8
|
+
|
9
|
+
# Get the default value for the field. If the default is a proc call
|
10
|
+
# it, otherwise clone the array.
|
11
|
+
#
|
12
|
+
# @example Get the default.
|
13
|
+
# field.default
|
14
|
+
#
|
15
|
+
# @return [ Object ] The default value.
|
16
|
+
#
|
17
|
+
# @since 2.1.0
|
18
|
+
def default
|
19
|
+
return nil unless default_value
|
20
|
+
default_value.respond_to?(:call) ? default_value.call : default_value.dup
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for integer fields.
|
7
|
+
class Integer
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
11
|
+
# compatible object to store.
|
12
|
+
#
|
13
|
+
# @example Serialize the field.
|
14
|
+
# field.serialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ Integer ] The converted integer.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def serialize(object)
|
22
|
+
return nil if object.blank?
|
23
|
+
begin
|
24
|
+
object.to_s =~ /\./ ? Float(object) : Integer(object)
|
25
|
+
rescue
|
26
|
+
object
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias :set :serialize
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for BSON::ObjectId fields.
|
7
|
+
class ObjectId
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
11
|
+
# compatible object to store.
|
12
|
+
#
|
13
|
+
# @example Serialize the field.
|
14
|
+
# field.serialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ BSON::ObjectId ] The converted object id.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def serialize(object)
|
22
|
+
if object.is_a?(::String)
|
23
|
+
BSON::ObjectId.from_string(object) unless object.blank?
|
24
|
+
else
|
25
|
+
object
|
26
|
+
end
|
27
|
+
end
|
28
|
+
alias :set :serialize
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for range fields.
|
7
|
+
class Range
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Deserialize this field from the type stored in MongoDB to the type
|
11
|
+
# defined on the model.
|
12
|
+
#
|
13
|
+
# @example Deserialize the field.
|
14
|
+
# field.deserialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ Range ] The converted range.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def deserialize(object)
|
22
|
+
object.nil? ? nil : ::Range.new(object["min"], object["max"])
|
23
|
+
end
|
24
|
+
|
25
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
26
|
+
# compatible object to store.
|
27
|
+
#
|
28
|
+
# @example Serialize the field.
|
29
|
+
# field.serialize(object)
|
30
|
+
#
|
31
|
+
# @param [ Object ] object The object to cast.
|
32
|
+
#
|
33
|
+
# @return [ Hash ] The converted hash.
|
34
|
+
#
|
35
|
+
# @since 2.1.0
|
36
|
+
def serialize(object)
|
37
|
+
object.nil? ? nil : { "min" => object.min, "max" => object.max }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for set fields.
|
7
|
+
class Set
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Deserialize this field from the type stored in MongoDB to the type
|
11
|
+
# defined on the model.
|
12
|
+
#
|
13
|
+
# @example Deserialize the field.
|
14
|
+
# field.deserialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ Set ] The converted set.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def deserialize(object)
|
22
|
+
::Set.new(object)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
26
|
+
# compatible object to store.
|
27
|
+
#
|
28
|
+
# @example Serialize the field.
|
29
|
+
# field.serialize(object)
|
30
|
+
#
|
31
|
+
# @param [ Object ] object The object to cast.
|
32
|
+
#
|
33
|
+
# @return [ Array ] The converted array.
|
34
|
+
#
|
35
|
+
# @since 2.1.0
|
36
|
+
def serialize(object)
|
37
|
+
object.to_a
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for string fields.
|
7
|
+
class String
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
11
|
+
# compatible object to store.
|
12
|
+
#
|
13
|
+
# @example Serialize the field.
|
14
|
+
# field.serialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ String ] The converted string.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def serialize(object)
|
22
|
+
object.try(:to_s)
|
23
|
+
end
|
24
|
+
alias :set :serialize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# Defines the behaviour for symbol fields.
|
7
|
+
class Symbol
|
8
|
+
include Serializable
|
9
|
+
|
10
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
11
|
+
# compatible object to store.
|
12
|
+
#
|
13
|
+
# @example Serialize the field.
|
14
|
+
# field.serialize(object)
|
15
|
+
#
|
16
|
+
# @param [ Object ] object The object to cast.
|
17
|
+
#
|
18
|
+
# @return [ Symbol ] The converted symbol.
|
19
|
+
#
|
20
|
+
# @since 2.1.0
|
21
|
+
def serialize(object)
|
22
|
+
object.blank? ? nil : object.to_sym
|
23
|
+
end
|
24
|
+
alias :set :serialize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Fields #:nodoc:
|
4
|
+
module Serializable #:nodoc:
|
5
|
+
|
6
|
+
# This module contains shared behaviour for date conversions.
|
7
|
+
module Timekeeping
|
8
|
+
|
9
|
+
# When reading the field do we need to cast the value? This holds true when
|
10
|
+
# times are stored or for big decimals which are stored as strings.
|
11
|
+
#
|
12
|
+
# @example Typecast on a read?
|
13
|
+
# field.cast_on_read?
|
14
|
+
#
|
15
|
+
# @return [ true ] Date fields cast on read.
|
16
|
+
#
|
17
|
+
# @since 2.1.0
|
18
|
+
def cast_on_read?; true; end
|
19
|
+
|
20
|
+
# Deserialize this field from the type stored in MongoDB to the type
|
21
|
+
# defined on the model.
|
22
|
+
#
|
23
|
+
# @example Deserialize the field.
|
24
|
+
# field.deserialize(object)
|
25
|
+
#
|
26
|
+
# @param [ Object ] object The object to cast.
|
27
|
+
#
|
28
|
+
# @return [ Time ] The converted time.
|
29
|
+
#
|
30
|
+
# @since 2.1.0
|
31
|
+
def deserialize(object)
|
32
|
+
return nil if object.blank?
|
33
|
+
object = object.getlocal unless Mongoid::Config.use_utc?
|
34
|
+
if Mongoid::Config.use_activesupport_time_zone?
|
35
|
+
time_zone = Mongoid::Config.use_utc? ? 'UTC' : ::Time.zone
|
36
|
+
object = object.in_time_zone(time_zone)
|
37
|
+
end
|
38
|
+
object
|
39
|
+
end
|
40
|
+
|
41
|
+
# Serialize the object from the type defined in the model to a MongoDB
|
42
|
+
# compatible object to store.
|
43
|
+
#
|
44
|
+
# @example Serialize the field.
|
45
|
+
# field.serialize(object)
|
46
|
+
#
|
47
|
+
# @param [ Object ] object The object to cast.
|
48
|
+
#
|
49
|
+
# @return [ Time ] The converted UTC time.
|
50
|
+
#
|
51
|
+
# @since 2.1.0
|
52
|
+
def serialize(object)
|
53
|
+
return nil if object.blank?
|
54
|
+
time = convert_to_time(object)
|
55
|
+
strip_milliseconds(time).utc
|
56
|
+
end
|
57
|
+
|
58
|
+
# Convert the provided object to a UTC time to store in the database.
|
59
|
+
#
|
60
|
+
# @example Set the time.
|
61
|
+
# Time.convert_to_time(Date.today)
|
62
|
+
#
|
63
|
+
# @param [ String, Date, DateTime, Array ] value The object to cast.
|
64
|
+
#
|
65
|
+
# @return [ Time ] The object as a UTC time.
|
66
|
+
#
|
67
|
+
# @since 1.0.0
|
68
|
+
def convert_to_time(value)
|
69
|
+
time = Mongoid::Config.use_activesupport_time_zone? ? ::Time.zone : ::Time
|
70
|
+
case value
|
71
|
+
when ::String
|
72
|
+
time.parse(value)
|
73
|
+
when ::DateTime
|
74
|
+
time.local(value.year, value.month, value.day, value.hour, value.min, value.sec)
|
75
|
+
when ::Date
|
76
|
+
time.local(value.year, value.month, value.day)
|
77
|
+
when ::Array
|
78
|
+
time.local(*value)
|
79
|
+
else
|
80
|
+
value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Strip the milliseconds off the time.
|
85
|
+
#
|
86
|
+
# @todo Durran: Why is this here? Still need time refactoring.
|
87
|
+
#
|
88
|
+
# @example Strip.
|
89
|
+
# Time.strip_millseconds(Time.now)
|
90
|
+
#
|
91
|
+
# @param [ Time ] time The time to strip.
|
92
|
+
#
|
93
|
+
# @return [ Time ] The time without millis.
|
94
|
+
#
|
95
|
+
# @since 2.1.0
|
96
|
+
def strip_milliseconds(time)
|
97
|
+
::Time.at(time.to_i)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|