mongoid 2.8.1 → 3.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +802 -58
- data/LICENSE +1 -1
- data/README.md +10 -11
- data/Rakefile +2 -8
- data/lib/config/locales/en.yml +441 -51
- data/lib/mongoid.rb +24 -39
- data/lib/mongoid/atomic.rb +16 -33
- data/lib/mongoid/atomic/modifiers.rb +2 -2
- data/lib/mongoid/atomic/paths/embedded.rb +4 -5
- data/lib/mongoid/atomic/paths/embedded/many.rb +6 -6
- data/lib/mongoid/atomic/paths/embedded/one.rb +5 -5
- data/lib/mongoid/atomic/paths/root.rb +4 -5
- data/lib/mongoid/attributes.rb +95 -32
- data/lib/mongoid/attributes/processing.rb +14 -10
- data/lib/mongoid/attributes/readonly.rb +56 -0
- data/lib/mongoid/callbacks.rb +90 -27
- data/lib/mongoid/collections/retry.rb +2 -3
- data/lib/mongoid/components.rb +11 -23
- data/lib/mongoid/config.rb +87 -233
- data/lib/mongoid/config/environment.rb +5 -6
- data/lib/mongoid/config/inflections.rb +6 -0
- data/lib/mongoid/config/options.rb +1 -1
- data/lib/mongoid/config/validators.rb +3 -0
- data/lib/mongoid/config/validators/option.rb +25 -0
- data/lib/mongoid/config/validators/session.rb +140 -0
- data/lib/mongoid/contextual.rb +50 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +98 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +181 -0
- data/lib/mongoid/contextual/atomic.rb +179 -0
- data/lib/mongoid/contextual/command.rb +43 -0
- data/lib/mongoid/contextual/find_and_modify.rb +66 -0
- data/lib/mongoid/contextual/map_reduce.rb +273 -0
- data/lib/mongoid/contextual/memory.rb +383 -0
- data/lib/mongoid/contextual/mongo.rb +543 -0
- data/lib/mongoid/copyable.rb +3 -34
- data/lib/mongoid/criteria.rb +436 -250
- data/lib/mongoid/criterion/inspection.rb +14 -8
- data/lib/mongoid/criterion/scoping.rb +114 -44
- data/lib/mongoid/dirty.rb +152 -67
- data/lib/mongoid/document.rb +69 -50
- data/lib/mongoid/errors.rb +22 -1
- data/lib/mongoid/errors/ambiguous_relationship.rb +51 -0
- data/lib/mongoid/errors/callback.rb +5 -6
- data/lib/mongoid/errors/delete_restriction.rb +29 -0
- data/lib/mongoid/errors/document_not_found.rb +98 -17
- data/lib/mongoid/errors/eager_load.rb +3 -6
- data/lib/mongoid/errors/invalid_collection.rb +3 -3
- data/lib/mongoid/errors/invalid_config_option.rb +27 -0
- data/lib/mongoid/errors/invalid_database.rb +3 -3
- data/lib/mongoid/errors/invalid_field.rb +54 -8
- data/lib/mongoid/errors/invalid_field_option.rb +35 -0
- data/lib/mongoid/errors/invalid_find.rb +3 -3
- data/lib/mongoid/errors/invalid_index.rb +28 -0
- data/lib/mongoid/errors/invalid_options.rb +4 -4
- data/lib/mongoid/errors/invalid_scope.rb +24 -0
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +38 -0
- data/lib/mongoid/errors/invalid_storage_options.rb +27 -0
- data/lib/mongoid/errors/invalid_time.rb +3 -6
- data/lib/mongoid/errors/inverse_not_found.rb +29 -0
- data/lib/mongoid/errors/mixed_relations.rb +4 -9
- data/lib/mongoid/errors/mixed_session_configuration.rb +28 -0
- data/lib/mongoid/errors/mongoid_error.rb +54 -3
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +28 -0
- data/lib/mongoid/errors/no_default_session.rb +23 -0
- data/lib/mongoid/errors/no_environment.rb +3 -3
- data/lib/mongoid/errors/no_map_reduce_output.rb +24 -0
- data/lib/mongoid/errors/no_parent.rb +24 -0
- data/lib/mongoid/errors/no_session_config.rb +22 -0
- data/lib/mongoid/errors/no_session_database.rb +27 -0
- data/lib/mongoid/errors/no_session_hosts.rb +27 -0
- data/lib/mongoid/errors/no_sessions_config.rb +20 -0
- data/lib/mongoid/errors/readonly_attribute.rb +25 -0
- data/lib/mongoid/errors/scope_overwrite.rb +4 -4
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -4
- data/lib/mongoid/errors/unknown_attribute.rb +25 -0
- data/lib/mongoid/errors/unsaved_document.rb +4 -8
- data/lib/mongoid/errors/unsupported_javascript.rb +27 -0
- data/lib/mongoid/errors/unsupported_version.rb +4 -4
- data/lib/mongoid/errors/validations.rb +7 -6
- data/lib/mongoid/errors/versioning_not_on_root.rb +23 -0
- data/lib/mongoid/extensions.rb +28 -76
- data/lib/mongoid/extensions/array.rb +127 -0
- data/lib/mongoid/extensions/big_decimal.rb +42 -0
- data/lib/mongoid/extensions/boolean.rb +24 -0
- data/lib/mongoid/extensions/date.rb +70 -0
- data/lib/mongoid/extensions/date_time.rb +68 -0
- data/lib/mongoid/extensions/false_class.rb +26 -0
- data/lib/mongoid/extensions/float.rb +44 -0
- data/lib/mongoid/extensions/hash.rb +91 -0
- data/lib/mongoid/extensions/integer.rb +54 -0
- data/lib/mongoid/extensions/module.rb +28 -0
- data/lib/mongoid/extensions/nil_class.rb +21 -0
- data/lib/mongoid/extensions/object.rb +188 -0
- data/lib/mongoid/extensions/object_id.rb +53 -0
- data/lib/mongoid/extensions/range.rb +55 -0
- data/lib/mongoid/extensions/regexp.rb +27 -0
- data/lib/mongoid/extensions/set.rb +55 -0
- data/lib/mongoid/extensions/string.rb +155 -0
- data/lib/mongoid/extensions/symbol.rb +54 -0
- data/lib/mongoid/extensions/time.rb +78 -0
- data/lib/mongoid/extensions/time_with_zone.rb +55 -0
- data/lib/mongoid/extensions/true_class.rb +26 -0
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields.rb +129 -194
- data/lib/mongoid/fields/foreign_key.rb +134 -0
- data/lib/mongoid/fields/localized.rb +73 -0
- data/lib/mongoid/fields/standard.rb +268 -0
- data/lib/mongoid/fields/validators.rb +2 -0
- data/lib/mongoid/fields/validators/macro.rb +83 -0
- data/lib/mongoid/finders.rb +42 -43
- data/lib/mongoid/hierarchy.rb +25 -14
- data/lib/mongoid/identity_map.rb +31 -19
- data/lib/mongoid/indexes.rb +66 -15
- data/lib/mongoid/indexes/validators/options.rb +80 -0
- data/lib/mongoid/inspection.rb +1 -1
- data/lib/mongoid/javascript.rb +1 -1
- data/lib/mongoid/json.rb +2 -2
- data/lib/mongoid/loggable.rb +69 -0
- data/lib/mongoid/matchers.rb +1 -1
- data/lib/mongoid/matchers/all.rb +7 -8
- data/lib/mongoid/matchers/and.rb +3 -3
- data/lib/mongoid/matchers/default.rb +6 -4
- data/lib/mongoid/matchers/exists.rb +2 -2
- data/lib/mongoid/matchers/gt.rb +2 -2
- data/lib/mongoid/matchers/gte.rb +2 -2
- data/lib/mongoid/matchers/in.rb +3 -7
- data/lib/mongoid/matchers/lt.rb +2 -2
- data/lib/mongoid/matchers/lte.rb +2 -2
- data/lib/mongoid/matchers/ne.rb +2 -2
- data/lib/mongoid/matchers/nin.rb +2 -2
- data/lib/mongoid/matchers/or.rb +2 -2
- data/lib/mongoid/matchers/size.rb +2 -2
- data/lib/mongoid/matchers/strategies.rb +3 -3
- data/lib/mongoid/multi_parameter_attributes.rb +8 -10
- data/lib/mongoid/nested_attributes.rb +17 -9
- data/lib/mongoid/observer.rb +1 -2
- data/lib/mongoid/paranoia.rb +13 -18
- data/lib/mongoid/persistence.rb +43 -39
- data/lib/mongoid/persistence/atomic.rb +2 -2
- data/lib/mongoid/persistence/atomic/add_to_set.rb +5 -9
- data/lib/mongoid/persistence/atomic/bit.rb +5 -7
- data/lib/mongoid/persistence/atomic/inc.rb +5 -7
- data/lib/mongoid/persistence/atomic/operation.rb +45 -6
- data/lib/mongoid/persistence/atomic/pop.rb +5 -7
- data/lib/mongoid/persistence/atomic/pull.rb +5 -7
- data/lib/mongoid/persistence/atomic/pull_all.rb +5 -7
- data/lib/mongoid/persistence/atomic/push.rb +4 -10
- data/lib/mongoid/persistence/atomic/push_all.rb +4 -10
- data/lib/mongoid/persistence/atomic/rename.rb +6 -7
- data/lib/mongoid/persistence/atomic/sets.rb +5 -7
- data/lib/mongoid/persistence/atomic/unset.rb +4 -5
- data/lib/mongoid/persistence/deletion.rb +2 -2
- data/lib/mongoid/persistence/insertion.rb +10 -16
- data/lib/mongoid/persistence/modification.rb +5 -9
- data/lib/mongoid/persistence/operations.rb +6 -19
- data/lib/mongoid/persistence/operations/embedded/insert.rb +7 -6
- data/lib/mongoid/persistence/operations/embedded/remove.rb +5 -5
- data/lib/mongoid/persistence/operations/insert.rb +4 -4
- data/lib/mongoid/persistence/operations/remove.rb +4 -4
- data/lib/mongoid/persistence/operations/update.rb +5 -5
- data/lib/mongoid/railtie.rb +26 -11
- data/lib/mongoid/railties/database.rake +22 -108
- data/lib/mongoid/relations.rb +4 -6
- data/lib/mongoid/relations/accessors.rb +119 -19
- data/lib/mongoid/relations/auto_save.rb +59 -5
- data/lib/mongoid/relations/binding.rb +211 -2
- data/lib/mongoid/relations/bindings/embedded/in.rb +16 -22
- data/lib/mongoid/relations/bindings/embedded/many.rb +9 -50
- data/lib/mongoid/relations/bindings/embedded/one.rb +10 -16
- data/lib/mongoid/relations/bindings/referenced/in.rb +31 -57
- data/lib/mongoid/relations/bindings/referenced/many.rb +8 -20
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -19
- data/lib/mongoid/relations/bindings/referenced/one.rb +10 -24
- data/lib/mongoid/relations/builder.rb +3 -3
- data/lib/mongoid/relations/builders.rb +19 -16
- data/lib/mongoid/relations/builders/embedded/in.rb +5 -5
- data/lib/mongoid/relations/builders/embedded/many.rb +12 -12
- data/lib/mongoid/relations/builders/embedded/one.rb +6 -6
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +8 -8
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +4 -4
- data/lib/mongoid/relations/builders/referenced/in.rb +4 -4
- data/lib/mongoid/relations/builders/referenced/many.rb +5 -5
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +7 -5
- data/lib/mongoid/relations/builders/referenced/one.rb +5 -5
- data/lib/mongoid/relations/cascading.rb +6 -4
- data/lib/mongoid/relations/cascading/delete.rb +3 -5
- data/lib/mongoid/relations/cascading/destroy.rb +3 -3
- data/lib/mongoid/relations/cascading/nullify.rb +3 -3
- data/lib/mongoid/relations/cascading/restrict.rb +37 -0
- data/lib/mongoid/relations/constraint.rb +4 -3
- data/lib/mongoid/relations/conversions.rb +5 -6
- data/lib/mongoid/relations/cyclic.rb +7 -7
- data/lib/mongoid/relations/embedded/batchable.rb +346 -0
- data/lib/mongoid/relations/embedded/in.rb +23 -12
- data/lib/mongoid/relations/embedded/many.rb +99 -161
- data/lib/mongoid/relations/embedded/one.rb +25 -14
- data/lib/mongoid/relations/macros.rb +105 -61
- data/lib/mongoid/relations/many.rb +93 -14
- data/lib/mongoid/relations/metadata.rb +200 -45
- data/lib/mongoid/relations/nested_builder.rb +3 -5
- data/lib/mongoid/relations/one.rb +2 -2
- data/lib/mongoid/relations/options.rb +2 -2
- data/lib/mongoid/relations/polymorphic.rb +9 -9
- data/lib/mongoid/relations/proxy.rb +60 -31
- data/lib/mongoid/relations/referenced/in.rb +40 -15
- data/lib/mongoid/relations/referenced/many.rb +117 -132
- data/lib/mongoid/relations/referenced/many_to_many.rb +101 -46
- data/lib/mongoid/relations/referenced/one.rb +34 -13
- data/lib/mongoid/relations/reflections.rb +3 -3
- data/lib/mongoid/relations/synchronization.rb +19 -23
- data/lib/mongoid/relations/targets/enumerable.rb +86 -57
- data/lib/mongoid/reloading.rb +12 -14
- data/lib/mongoid/scoping.rb +329 -0
- data/lib/mongoid/serialization.rb +8 -27
- data/lib/mongoid/sessions.rb +359 -0
- data/lib/mongoid/sessions/factory.rb +106 -0
- data/lib/mongoid/sessions/mongo_uri.rb +93 -0
- data/lib/mongoid/sessions/validators.rb +2 -0
- data/lib/mongoid/sessions/validators/storage.rb +49 -0
- data/lib/mongoid/sharding.rb +6 -6
- data/lib/mongoid/state.rb +6 -7
- data/lib/mongoid/threaded.rb +167 -59
- data/lib/mongoid/threaded/lifecycle.rb +21 -22
- data/lib/mongoid/threaded/sessions.rb +0 -0
- data/lib/mongoid/timestamps.rb +1 -1
- data/lib/mongoid/timestamps/created.rb +8 -4
- data/lib/mongoid/timestamps/timeless.rb +6 -4
- data/lib/mongoid/timestamps/updated.rb +3 -3
- data/lib/mongoid/unit_of_work.rb +61 -0
- data/lib/mongoid/validations.rb +27 -19
- data/lib/mongoid/validations/associated.rb +2 -2
- data/lib/mongoid/validations/format.rb +2 -2
- data/lib/mongoid/validations/presence.rb +31 -5
- data/lib/mongoid/validations/uniqueness.rb +9 -12
- data/lib/mongoid/version.rb +2 -2
- data/lib/mongoid/versioning.rb +25 -26
- data/lib/rack/mongoid/middleware/identity_map.rb +3 -3
- data/lib/rails/generators/mongoid/config/config_generator.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +59 -19
- data/lib/rails/generators/mongoid/model/model_generator.rb +7 -7
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +2 -2
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +4 -4
- data/lib/rails/generators/mongoid_generator.rb +5 -5
- data/lib/rails/mongoid.rb +69 -25
- metadata +110 -137
- checksums.yaml +0 -7
- data/lib/config/locales/bg.yml +0 -61
- data/lib/config/locales/de.yml +0 -61
- data/lib/config/locales/en-GB.yml +0 -64
- data/lib/config/locales/es.yml +0 -59
- data/lib/config/locales/fr.yml +0 -62
- data/lib/config/locales/hi.yml +0 -53
- data/lib/config/locales/hu.yml +0 -64
- data/lib/config/locales/id.yml +0 -62
- data/lib/config/locales/it.yml +0 -59
- data/lib/config/locales/ja.yml +0 -57
- data/lib/config/locales/kr.yml +0 -54
- data/lib/config/locales/nl.yml +0 -61
- data/lib/config/locales/pl.yml +0 -59
- data/lib/config/locales/pt-BR.yml +0 -60
- data/lib/config/locales/pt.yml +0 -60
- data/lib/config/locales/ro.yml +0 -66
- data/lib/config/locales/ru.yml +0 -61
- data/lib/config/locales/sv.yml +0 -60
- data/lib/config/locales/vi.yml +0 -62
- data/lib/config/locales/zh-CN.yml +0 -53
- data/lib/mongoid/collection.rb +0 -157
- data/lib/mongoid/collections.rb +0 -120
- data/lib/mongoid/collections/master.rb +0 -45
- data/lib/mongoid/collections/operations.rb +0 -44
- data/lib/mongoid/config/database.rb +0 -181
- data/lib/mongoid/config/replset_database.rb +0 -80
- data/lib/mongoid/contexts.rb +0 -25
- data/lib/mongoid/contexts/enumerable.rb +0 -313
- data/lib/mongoid/contexts/enumerable/sort.rb +0 -43
- data/lib/mongoid/contexts/mongo.rb +0 -487
- data/lib/mongoid/criterion/builder.rb +0 -34
- data/lib/mongoid/criterion/complex.rb +0 -84
- data/lib/mongoid/criterion/creational.rb +0 -34
- data/lib/mongoid/criterion/exclusion.rb +0 -110
- data/lib/mongoid/criterion/inclusion.rb +0 -290
- data/lib/mongoid/criterion/optional.rb +0 -259
- data/lib/mongoid/criterion/selector.rb +0 -177
- data/lib/mongoid/cursor.rb +0 -88
- data/lib/mongoid/default_scope.rb +0 -36
- data/lib/mongoid/errors/invalid_type.rb +0 -25
- data/lib/mongoid/extensions/array/deep_copy.rb +0 -25
- data/lib/mongoid/extensions/array/deletion.rb +0 -29
- data/lib/mongoid/extensions/false_class/equality.rb +0 -26
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +0 -47
- data/lib/mongoid/extensions/hash/deep_copy.rb +0 -25
- data/lib/mongoid/extensions/hash/scoping.rb +0 -25
- data/lib/mongoid/extensions/integer/checks.rb +0 -23
- data/lib/mongoid/extensions/nil/collectionization.rb +0 -23
- data/lib/mongoid/extensions/object/checks.rb +0 -29
- data/lib/mongoid/extensions/object/deep_copy.rb +0 -21
- data/lib/mongoid/extensions/object/reflections.rb +0 -48
- data/lib/mongoid/extensions/object/substitutable.rb +0 -15
- data/lib/mongoid/extensions/object/yoda.rb +0 -44
- data/lib/mongoid/extensions/object_id/conversions.rb +0 -60
- data/lib/mongoid/extensions/proc/scoping.rb +0 -25
- data/lib/mongoid/extensions/string/checks.rb +0 -36
- data/lib/mongoid/extensions/string/conversions.rb +0 -22
- data/lib/mongoid/extensions/string/inflections.rb +0 -118
- data/lib/mongoid/extensions/symbol/checks.rb +0 -23
- data/lib/mongoid/extensions/symbol/inflections.rb +0 -67
- data/lib/mongoid/extensions/true_class/equality.rb +0 -26
- data/lib/mongoid/extras.rb +0 -31
- data/lib/mongoid/fields/internal/array.rb +0 -77
- data/lib/mongoid/fields/internal/big_decimal.rb +0 -63
- data/lib/mongoid/fields/internal/bignum.rb +0 -10
- data/lib/mongoid/fields/internal/binary.rb +0 -11
- data/lib/mongoid/fields/internal/boolean.rb +0 -58
- data/lib/mongoid/fields/internal/date.rb +0 -51
- data/lib/mongoid/fields/internal/date_time.rb +0 -28
- data/lib/mongoid/fields/internal/false_class.rb +0 -10
- data/lib/mongoid/fields/internal/fixnum.rb +0 -10
- data/lib/mongoid/fields/internal/float.rb +0 -47
- data/lib/mongoid/fields/internal/foreign_keys/array.rb +0 -88
- data/lib/mongoid/fields/internal/foreign_keys/object.rb +0 -56
- data/lib/mongoid/fields/internal/hash.rb +0 -11
- data/lib/mongoid/fields/internal/integer.rb +0 -59
- data/lib/mongoid/fields/internal/localized.rb +0 -62
- data/lib/mongoid/fields/internal/nil_class.rb +0 -53
- data/lib/mongoid/fields/internal/object.rb +0 -11
- data/lib/mongoid/fields/internal/object_id.rb +0 -46
- data/lib/mongoid/fields/internal/range.rb +0 -61
- data/lib/mongoid/fields/internal/set.rb +0 -57
- data/lib/mongoid/fields/internal/string.rb +0 -42
- data/lib/mongoid/fields/internal/symbol.rb +0 -43
- data/lib/mongoid/fields/internal/time.rb +0 -23
- data/lib/mongoid/fields/internal/time_with_zone.rb +0 -23
- data/lib/mongoid/fields/internal/timekeeping.rb +0 -122
- data/lib/mongoid/fields/internal/true_class.rb +0 -10
- data/lib/mongoid/fields/mappings.rb +0 -42
- data/lib/mongoid/fields/serializable.rb +0 -270
- data/lib/mongoid/identity.rb +0 -92
- data/lib/mongoid/keys.rb +0 -144
- data/lib/mongoid/logger.rb +0 -45
- data/lib/mongoid/multi_database.rb +0 -36
- data/lib/mongoid/named_scope.rb +0 -166
- data/lib/mongoid/relations/embedded/atomic.rb +0 -89
- data/lib/mongoid/relations/embedded/atomic/operation.rb +0 -63
- data/lib/mongoid/relations/embedded/atomic/pull.rb +0 -65
- data/lib/mongoid/relations/embedded/atomic/push_all.rb +0 -59
- data/lib/mongoid/relations/embedded/atomic/set.rb +0 -61
- data/lib/mongoid/relations/embedded/atomic/unset.rb +0 -41
- data/lib/mongoid/relations/referenced/batch.rb +0 -73
- data/lib/mongoid/relations/referenced/batch/insert.rb +0 -57
- data/lib/mongoid/safety.rb +0 -105
- data/lib/mongoid/scope.rb +0 -31
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module DateTime
|
5
|
+
|
6
|
+
# Mongoize the date time into a time.
|
7
|
+
#
|
8
|
+
# @example Mongoize the date time.
|
9
|
+
# date_time.__mongoize_time__
|
10
|
+
#
|
11
|
+
# @return [ Time ] The mongoized time.
|
12
|
+
#
|
13
|
+
# @since 3.0.0
|
14
|
+
def __mongoize_time__
|
15
|
+
return self if utc? && Mongoid.use_utc?
|
16
|
+
::Time.configured.local(year, month, day, hour, min, sec)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
20
|
+
# type.
|
21
|
+
#
|
22
|
+
# @example Mongoize the object.
|
23
|
+
# date_time.mongoize
|
24
|
+
#
|
25
|
+
# @return [ Time ] The object mongoized.
|
26
|
+
#
|
27
|
+
# @since 3.0.0
|
28
|
+
def mongoize
|
29
|
+
::DateTime.mongoize(self)
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
|
34
|
+
# Convert the object from it's mongo friendly ruby type to this type.
|
35
|
+
#
|
36
|
+
# @example Demongoize the object.
|
37
|
+
# DateTime.demongoize(object)
|
38
|
+
#
|
39
|
+
# @param [ Time ] object The time from Mongo.
|
40
|
+
#
|
41
|
+
# @return [ DateTime ] The object as a date.
|
42
|
+
#
|
43
|
+
# @since 3.0.0
|
44
|
+
def demongoize(object)
|
45
|
+
::Time.demongoize(object).try(:to_datetime)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
49
|
+
# type.
|
50
|
+
#
|
51
|
+
# @example Mongoize the object.
|
52
|
+
# DateTime.mongoize("2012-1-1")
|
53
|
+
#
|
54
|
+
# @param [ Object ] object The object to convert.
|
55
|
+
#
|
56
|
+
# @return [ Time ] The object mongoized.
|
57
|
+
#
|
58
|
+
# @since 3.0.0
|
59
|
+
def mongoize(object)
|
60
|
+
::Time.mongoize(object)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
::DateTime.__send__(:include, Mongoid::Extensions::DateTime)
|
68
|
+
::DateTime.__send__(:extend, Mongoid::Extensions::DateTime::ClassMethods)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module FalseClass
|
5
|
+
|
6
|
+
# Is the passed value a boolean?
|
7
|
+
#
|
8
|
+
# @example Is the value a boolean type?
|
9
|
+
# false.is_a?(Boolean)
|
10
|
+
#
|
11
|
+
# @param [ Class ] other The class to check.
|
12
|
+
#
|
13
|
+
# @return [ true, false ] If the other is a boolean.
|
14
|
+
#
|
15
|
+
# @since 1.0.0
|
16
|
+
def is_a?(other)
|
17
|
+
if other == ::Boolean || other.class == ::Boolean
|
18
|
+
return true
|
19
|
+
end
|
20
|
+
super(other)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
::FalseClass.__send__(:include, Mongoid::Extensions::FalseClass)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module Float
|
5
|
+
|
6
|
+
# Convert the float into a time.
|
7
|
+
#
|
8
|
+
# @example Convert the float into a time.
|
9
|
+
# 1335532685.117847.__mongoize_time__
|
10
|
+
#
|
11
|
+
# @return [ Time ] The float as a time.
|
12
|
+
#
|
13
|
+
# @since 3.0.0
|
14
|
+
def __mongoize_time__
|
15
|
+
::Time.at(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
21
|
+
# type.
|
22
|
+
#
|
23
|
+
# @example Mongoize the object.
|
24
|
+
# Float.mongoize("123.11")
|
25
|
+
#
|
26
|
+
# @param [ Object ] object The object to mongoize.
|
27
|
+
#
|
28
|
+
# @return [ String ] The object mongoized.
|
29
|
+
#
|
30
|
+
# @since 3.0.0
|
31
|
+
def mongoize(object)
|
32
|
+
unless object.blank?
|
33
|
+
__numeric__(object) rescue 0.0
|
34
|
+
else
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
::Float.__send__(:include, Mongoid::Extensions::Float)
|
44
|
+
::Float.__send__(:extend, Mongoid::Extensions::Float::ClassMethods)
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module Hash
|
5
|
+
|
6
|
+
# Evolves each value in the hash to an object id if it is convertable.
|
7
|
+
#
|
8
|
+
# @example Convert the hash values.
|
9
|
+
# { field: id }.__evolve_object_id__
|
10
|
+
#
|
11
|
+
# @return [ Hash ] The converted hash.
|
12
|
+
#
|
13
|
+
# @since 3.0.0
|
14
|
+
def __evolve_object_id__
|
15
|
+
update_values(&:__evolve_object_id__)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get the id attribute from this hash, whether it's prefixed with an
|
19
|
+
# underscore or is a symbol.
|
20
|
+
#
|
21
|
+
# @example Extract the id.
|
22
|
+
# { :_id => 1 }.extract_id
|
23
|
+
#
|
24
|
+
# @return [ Object ] The value of the id.
|
25
|
+
#
|
26
|
+
# @since 2.3.2
|
27
|
+
def extract_id
|
28
|
+
self["id"] || self["_id"] || self[:id] || self[:_id]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
32
|
+
# type.
|
33
|
+
#
|
34
|
+
# @example Mongoize the object.
|
35
|
+
# object.mongoize
|
36
|
+
#
|
37
|
+
# @return [ Hash ] The object.
|
38
|
+
#
|
39
|
+
# @since 3.0.0
|
40
|
+
def mongoize
|
41
|
+
::Hash.mongoize(self)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Can the size of this object change?
|
45
|
+
#
|
46
|
+
# @example Is the hash resizable?
|
47
|
+
# {}.resizable?
|
48
|
+
#
|
49
|
+
# @return [ true ] true.
|
50
|
+
#
|
51
|
+
# @since 3.0.0
|
52
|
+
def resizable?
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
module ClassMethods
|
57
|
+
|
58
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
59
|
+
# type.
|
60
|
+
#
|
61
|
+
# @example Mongoize the object.
|
62
|
+
# Hash.mongoize([ 1, 2, 3 ])
|
63
|
+
#
|
64
|
+
# @param [ Object ] object The object to mongoize.
|
65
|
+
#
|
66
|
+
# @return [ Hash ] The object mongoized.
|
67
|
+
#
|
68
|
+
# @since 3.0.0
|
69
|
+
def mongoize(object)
|
70
|
+
return if object.nil?
|
71
|
+
evolve(object).update_values { |value| value.mongoize }
|
72
|
+
end
|
73
|
+
|
74
|
+
# Can the size of this object change?
|
75
|
+
#
|
76
|
+
# @example Is the hash resizable?
|
77
|
+
# {}.resizable?
|
78
|
+
#
|
79
|
+
# @return [ true ] true.
|
80
|
+
#
|
81
|
+
# @since 3.0.0
|
82
|
+
def resizable?
|
83
|
+
true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
::Hash.__send__(:include, Mongoid::Extensions::Hash)
|
91
|
+
::Hash.__send__(:extend, Mongoid::Extensions::Hash::ClassMethods)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module Integer
|
5
|
+
|
6
|
+
# Returns the integer as a time.
|
7
|
+
#
|
8
|
+
# @example Convert the integer to a time.
|
9
|
+
# 1335532685.__mongoize_time__
|
10
|
+
#
|
11
|
+
# @return [ Time ] The converted time.
|
12
|
+
#
|
13
|
+
# @since 3.0.0
|
14
|
+
def __mongoize_time__
|
15
|
+
::Time.at(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Is the object not to be converted to bson on criteria creation?
|
19
|
+
#
|
20
|
+
# @example Is the object unconvertable?
|
21
|
+
# object.unconvertable_to_bson?
|
22
|
+
#
|
23
|
+
# @return [ true ] If the object is unconvertable.
|
24
|
+
#
|
25
|
+
# @since 2.2.1
|
26
|
+
def unconvertable_to_bson?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
|
32
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
33
|
+
# type.
|
34
|
+
#
|
35
|
+
# @example Mongoize the object.
|
36
|
+
# BigDecimal.mongoize("123.11")
|
37
|
+
#
|
38
|
+
# @return [ String ] The object mongoized.
|
39
|
+
#
|
40
|
+
# @since 3.0.0
|
41
|
+
def mongoize(object)
|
42
|
+
unless object.blank?
|
43
|
+
__numeric__(object).to_i rescue 0
|
44
|
+
else
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
::Integer.__send__(:include, Mongoid::Extensions::Integer)
|
54
|
+
::Integer.__send__(:extend, Mongoid::Extensions::Integer::ClassMethods)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module Module
|
5
|
+
|
6
|
+
# Redefine the method. Will undef the method if it exists or simply
|
7
|
+
# just define it.
|
8
|
+
#
|
9
|
+
# @example Redefine the method.
|
10
|
+
# Object.re_define_method("exists?") do
|
11
|
+
# self
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @param [ String, Symbol ] name The name of the method.
|
15
|
+
# @param [ Proc ] block The method body.
|
16
|
+
#
|
17
|
+
# @return [ Method ] The new method.
|
18
|
+
#
|
19
|
+
# @since 3.0.0
|
20
|
+
def re_define_method(name, &block)
|
21
|
+
undef_method(name) if method_defined?(name)
|
22
|
+
define_method(name, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
::Module.__send__(:include, Mongoid::Extensions::Module)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module NilClass
|
5
|
+
|
6
|
+
# Get the name of a nil collection.
|
7
|
+
#
|
8
|
+
# @example Get the nil name.
|
9
|
+
# nil.collectionize
|
10
|
+
#
|
11
|
+
# @return [ String ] A blank string.
|
12
|
+
#
|
13
|
+
# @since 1.0.0
|
14
|
+
def collectionize
|
15
|
+
to_s.collectionize
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
::NilClass.__send__(:include, Mongoid::Extensions::NilClass)
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Extensions
|
4
|
+
module Object
|
5
|
+
|
6
|
+
# Evolve a plain object into an object id.
|
7
|
+
#
|
8
|
+
# @example Evolve the object.
|
9
|
+
# object.__evolve_object_id__
|
10
|
+
#
|
11
|
+
# @return [ Object ] self.
|
12
|
+
#
|
13
|
+
# @since 3.0.0
|
14
|
+
def __evolve_object_id__
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
# Mongoize a plain object into a time.
|
19
|
+
#
|
20
|
+
# @example Mongoize the object.
|
21
|
+
# object.__mongoize_time__
|
22
|
+
#
|
23
|
+
# @return [ Object ] self.
|
24
|
+
#
|
25
|
+
# @since 3.0.0
|
26
|
+
def __mongoize_time__
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
# Do or do not, there is no try. -- Yoda.
|
31
|
+
#
|
32
|
+
# @example Do or do not.
|
33
|
+
# object.do_or_do_not(:use, "The Force")
|
34
|
+
#
|
35
|
+
# @param [ String, Symbol ] name The method name.
|
36
|
+
# @param [ Array ] *args The arguments.
|
37
|
+
#
|
38
|
+
# @return [ Object, nil ] The result of the method call or nil if the
|
39
|
+
# method does not exist.
|
40
|
+
#
|
41
|
+
# @since 2.0.0.rc.1
|
42
|
+
def do_or_do_not(name, *args)
|
43
|
+
send(name, *args) if name && respond_to?(name)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get the value for an instance variable or nil if it doesn't exist.
|
47
|
+
#
|
48
|
+
# @example Get the value for an instance var.
|
49
|
+
# document.ivar("person")
|
50
|
+
#
|
51
|
+
# @param [ String ] name The name of the variable.
|
52
|
+
#
|
53
|
+
# @return [ Object, nil ] The value or nil.
|
54
|
+
#
|
55
|
+
# @since 2.0.0.rc.1
|
56
|
+
def ivar(name)
|
57
|
+
if instance_variable_defined?("@#{name}")
|
58
|
+
return instance_variable_get("@#{name}")
|
59
|
+
else
|
60
|
+
false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
65
|
+
# type.
|
66
|
+
#
|
67
|
+
# @example Mongoize the object.
|
68
|
+
# object.mongoize
|
69
|
+
#
|
70
|
+
# @return [ Object ] The object.
|
71
|
+
#
|
72
|
+
# @since 3.0.0
|
73
|
+
def mongoize
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
# Remove the instance variable for the provided name.
|
78
|
+
#
|
79
|
+
# @example Remove the instance variable
|
80
|
+
# document.remove_ivar("person")
|
81
|
+
#
|
82
|
+
# @param [ String ] name The name of the variable.
|
83
|
+
#
|
84
|
+
# @return [ true, false ] If the variable was defined.
|
85
|
+
#
|
86
|
+
# @since 2.1.0
|
87
|
+
def remove_ivar(name)
|
88
|
+
if instance_variable_defined?("@#{name}")
|
89
|
+
return remove_instance_variable("@#{name}")
|
90
|
+
else
|
91
|
+
false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Is the object's size changable? Only returns true for arrays and hashes
|
96
|
+
# currently.
|
97
|
+
#
|
98
|
+
# @example Is the object resizable?
|
99
|
+
# object.resizable?
|
100
|
+
#
|
101
|
+
# @return [ false ] false.
|
102
|
+
#
|
103
|
+
# @since 3.0.0
|
104
|
+
def resizable?
|
105
|
+
false
|
106
|
+
end
|
107
|
+
|
108
|
+
# Get the substitutable version of an object.
|
109
|
+
#
|
110
|
+
# @example Get the substitutable.
|
111
|
+
# object.substitutable
|
112
|
+
#
|
113
|
+
# @return [ Object ] self.
|
114
|
+
#
|
115
|
+
# @since 2.0.0
|
116
|
+
def substitutable
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
# You must unlearn what you have learned. -- Yoda
|
121
|
+
#
|
122
|
+
# @example You must perform this execution.
|
123
|
+
# object.you_must(:use, "The Force")
|
124
|
+
#
|
125
|
+
# @param [ String, Symbol ] name The method name.
|
126
|
+
# @param [ Array ] *args The arguments.
|
127
|
+
#
|
128
|
+
# @return [ Object, nil ] The result of the method call or nil if the
|
129
|
+
# method does not exist. Nil if the object is frozen.
|
130
|
+
#
|
131
|
+
# @since 2.2.1
|
132
|
+
def you_must(name, *args)
|
133
|
+
frozen? ? nil : do_or_do_not(name, *args)
|
134
|
+
end
|
135
|
+
|
136
|
+
module ClassMethods
|
137
|
+
|
138
|
+
# Convert the provided object to a foreign key, given the metadata key
|
139
|
+
# contstraint.
|
140
|
+
#
|
141
|
+
# @example Convert the object to a fk.
|
142
|
+
# Object.__mongoize_fk__(constraint, object)
|
143
|
+
#
|
144
|
+
# @param [ Constraint ] constraint The constraint.
|
145
|
+
# @param [ Object ] object The object to convert.
|
146
|
+
#
|
147
|
+
# @return [ Object ] The converted object.
|
148
|
+
#
|
149
|
+
# @since 3.0.0
|
150
|
+
def __mongoize_fk__(constraint, object)
|
151
|
+
object.blank? ? nil : constraint.convert(object)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Convert the object from it's mongo friendly ruby type to this type.
|
155
|
+
#
|
156
|
+
# @example Demongoize the object.
|
157
|
+
# Object.demongoize(object)
|
158
|
+
#
|
159
|
+
# @param [ Object ] object The object to demongoize.
|
160
|
+
#
|
161
|
+
# @return [ Object ] The object.
|
162
|
+
#
|
163
|
+
# @since 3.0.0
|
164
|
+
def demongoize(object)
|
165
|
+
object
|
166
|
+
end
|
167
|
+
|
168
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly
|
169
|
+
# type.
|
170
|
+
#
|
171
|
+
# @example Mongoize the object.
|
172
|
+
# Object.mongoize("123.11")
|
173
|
+
#
|
174
|
+
# @param [ Object ] object The object to mongoize.
|
175
|
+
#
|
176
|
+
# @return [ Object ] The object mongoized.
|
177
|
+
#
|
178
|
+
# @since 3.0.0
|
179
|
+
def mongoize(object)
|
180
|
+
object.mongoize
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
::Object.__send__(:include, Mongoid::Extensions::Object)
|
188
|
+
::Object.__send__(:extend, Mongoid::Extensions::Object::ClassMethods)
|