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.
- data/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +41 -0
- data/lib/config/locales/de.yml +41 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +41 -0
- data/lib/config/locales/fr.yml +42 -0
- data/lib/config/locales/hu.yml +44 -0
- data/lib/config/locales/id.yml +46 -0
- data/lib/config/locales/it.yml +39 -0
- data/lib/config/locales/ja.yml +40 -0
- data/lib/config/locales/kr.yml +65 -0
- data/lib/config/locales/nl.yml +39 -0
- data/lib/config/locales/pl.yml +39 -0
- data/lib/config/locales/pt-BR.yml +40 -0
- data/lib/config/locales/pt.yml +40 -0
- data/lib/config/locales/ro.yml +46 -0
- data/lib/config/locales/ru.yml +41 -0
- data/lib/config/locales/sv.yml +40 -0
- data/lib/config/locales/vi.yml +45 -0
- data/lib/config/locales/zh-CN.yml +33 -0
- data/lib/mongoid.rb +140 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +185 -0
- data/lib/mongoid/attributes/processing.rb +145 -0
- data/lib/mongoid/callbacks.rb +23 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/master.rb +37 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/retry.rb +39 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config.rb +349 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +78 -0
- data/lib/mongoid/contexts.rb +19 -0
- data/lib/mongoid/contexts/enumerable.rb +275 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/copyable.rb +46 -0
- data/lib/mongoid/criteria.rb +357 -0
- data/lib/mongoid/criterion/builder.rb +34 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +108 -0
- data/lib/mongoid/criterion/inclusion.rb +198 -0
- data/lib/mongoid/criterion/inspection.rb +22 -0
- data/lib/mongoid/criterion/optional.rb +193 -0
- data/lib/mongoid/criterion/selector.rb +143 -0
- data/lib/mongoid/criterion/unconvertable.rb +20 -0
- data/lib/mongoid/cursor.rb +86 -0
- data/lib/mongoid/default_scope.rb +36 -0
- data/lib/mongoid/dirty.rb +253 -0
- data/lib/mongoid/document.rb +284 -0
- data/lib/mongoid/errors.rb +13 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mixed_relations.rb +37 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/extensions.rb +123 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +25 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/checks.rb +32 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/range/conversions.rb +25 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +42 -0
- data/lib/mongoid/factory.rb +37 -0
- data/lib/mongoid/field.rb +162 -0
- data/lib/mongoid/fields.rb +183 -0
- data/lib/mongoid/finders.rb +127 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +92 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +54 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +131 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers.rb +32 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +70 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/or.rb +30 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/matchers/strategies.rb +63 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +82 -0
- data/lib/mongoid/named_scope.rb +137 -0
- data/lib/mongoid/nested_attributes.rb +51 -0
- data/lib/mongoid/observer.rb +67 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +240 -0
- data/lib/mongoid/persistence/atomic.rb +88 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -0
- data/lib/mongoid/persistence/atomic/inc.rb +28 -0
- data/lib/mongoid/persistence/atomic/operation.rb +44 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
- data/lib/mongoid/persistence/atomic/push.rb +28 -0
- data/lib/mongoid/persistence/command.rb +71 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +43 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +77 -0
- data/lib/mongoid/railtie.rb +139 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations.rb +107 -0
- data/lib/mongoid/relations/accessors.rb +175 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/constraint.rb +42 -0
- data/lib/mongoid/relations/cyclic.rb +103 -0
- data/lib/mongoid/relations/embedded/atomic.rb +86 -0
- data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
- data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
- data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
- data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
- data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
- data/lib/mongoid/relations/embedded/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +499 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +310 -0
- data/lib/mongoid/relations/many.rb +215 -0
- data/lib/mongoid/relations/metadata.rb +539 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +143 -0
- data/lib/mongoid/relations/referenced/batch.rb +71 -0
- data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +516 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
- data/lib/mongoid/relations/referenced/one.rb +222 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/sharding.rb +51 -0
- data/lib/mongoid/state.rb +67 -0
- data/lib/mongoid/timestamps.rb +14 -0
- data/lib/mongoid/timestamps/created.rb +31 -0
- data/lib/mongoid/timestamps/updated.rb +33 -0
- data/lib/mongoid/validations.rb +124 -0
- data/lib/mongoid/validations/associated.rb +44 -0
- data/lib/mongoid/validations/referenced.rb +58 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +113 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +19 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
- data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/mongoid_generator.rb +70 -0
- data/lib/rails/mongoid.rb +58 -0
- metadata +406 -0
data/lib/mongoid/json.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
# This module is for hooking in to +ActiveModel+s serialization to let
|
|
4
|
+
# configuring the ability to include the root in JSON happen from the Mongoid
|
|
5
|
+
# config.
|
|
6
|
+
module JSON
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
# We need to redefine where the JSON configuration is getting defined,
|
|
10
|
+
# similar to +ActiveRecord+.
|
|
11
|
+
included do
|
|
12
|
+
undef_method :include_root_in_json
|
|
13
|
+
delegate :include_root_in_json, :to => ::Mongoid
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/mongoid/keys.rb
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
|
|
4
|
+
# This module defines the behaviour for overriding the default ids on
|
|
5
|
+
# documents.
|
|
6
|
+
module Keys
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
cattr_accessor :primary_key, :object_ids
|
|
11
|
+
delegate :primary_key, :using_object_ids?, :to => "self.class"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
# Determines if any field that the document id is composed of has changed.
|
|
17
|
+
#
|
|
18
|
+
# @example Has any key field changed?
|
|
19
|
+
# document.key_field_changed?
|
|
20
|
+
#
|
|
21
|
+
# @return [ true, false ] Has a key field changed?
|
|
22
|
+
#
|
|
23
|
+
# @since 2.0.0
|
|
24
|
+
def key_field_changed?
|
|
25
|
+
primary_key.any? { |field| changed.include?(field.to_s) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Sits around a save when composite keys are in play to handle the id magic
|
|
29
|
+
# if a key field has changed.
|
|
30
|
+
#
|
|
31
|
+
# @example Set the composite key.
|
|
32
|
+
# document.set_composite_key
|
|
33
|
+
#
|
|
34
|
+
# @param [ Proc ] block The block this surrounds.
|
|
35
|
+
#
|
|
36
|
+
# @since 2.0.0
|
|
37
|
+
def set_composite_key(&block)
|
|
38
|
+
if persisted? && key_field_changed?
|
|
39
|
+
swap_composite_keys(&block)
|
|
40
|
+
else
|
|
41
|
+
identify and block.call
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Swap out the composite key only after the document has been saved.
|
|
46
|
+
#
|
|
47
|
+
# @example Swap out the keys.
|
|
48
|
+
# document.swap_composite_keys
|
|
49
|
+
#
|
|
50
|
+
# @param [ Proc ] block The save block getting called.
|
|
51
|
+
#
|
|
52
|
+
# @since 2.0.0
|
|
53
|
+
def swap_composite_keys(&block)
|
|
54
|
+
old_id, new_id = id.dup, identify
|
|
55
|
+
@attributes["_id"] = old_id
|
|
56
|
+
block.call
|
|
57
|
+
@attributes["_id"] = new_id
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
module ClassMethods #:nodoc:
|
|
61
|
+
|
|
62
|
+
# Used for telling Mongoid on a per model basis whether to override the
|
|
63
|
+
# default +BSON::ObjectId+ and use a different type. This will be
|
|
64
|
+
# expanded in the future for requiring a PkFactory if the type is not a
|
|
65
|
+
# +BSON::ObjectId+ or +String+.
|
|
66
|
+
#
|
|
67
|
+
# @example Change the documents key type.
|
|
68
|
+
# class Person
|
|
69
|
+
# include Mongoid::Document
|
|
70
|
+
# identity :type => String
|
|
71
|
+
# end
|
|
72
|
+
#
|
|
73
|
+
# @param [ Hash ] options The options.
|
|
74
|
+
#
|
|
75
|
+
# @option options [ Class ] :type The type of the id.
|
|
76
|
+
#
|
|
77
|
+
# @since 2.0.0.beta.1
|
|
78
|
+
def identity(options = {})
|
|
79
|
+
fields["_id"].type = options[:type]
|
|
80
|
+
@object_ids = id_is_object
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Defines the field that will be used for the id of this +Document+. This
|
|
84
|
+
# set the id of this +Document+ before save to a parameterized version of
|
|
85
|
+
# the field that was supplied. This is good for use for readable URLS in
|
|
86
|
+
# web applications.
|
|
87
|
+
#
|
|
88
|
+
# @example Create a composite id.
|
|
89
|
+
# class Person
|
|
90
|
+
# include Mongoid::Document
|
|
91
|
+
# key :first_name, :last_name
|
|
92
|
+
# end
|
|
93
|
+
#
|
|
94
|
+
# @param [ Array<Symbol> ] The fields the key is composed of.
|
|
95
|
+
#
|
|
96
|
+
# @since 1.0.0
|
|
97
|
+
def key(*fields)
|
|
98
|
+
self.primary_key = fields
|
|
99
|
+
identity(:type => String)
|
|
100
|
+
set_callback(:save, :around, :set_composite_key)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Convenience method for determining if we are using +BSON::ObjectIds+ as
|
|
104
|
+
# our id.
|
|
105
|
+
#
|
|
106
|
+
# @example Does this class use object ids?
|
|
107
|
+
# person.using_object_ids?
|
|
108
|
+
#
|
|
109
|
+
# @return [ true, false ] If the class uses BSON::ObjectIds for the id.
|
|
110
|
+
#
|
|
111
|
+
# @since 1.0.0
|
|
112
|
+
def using_object_ids?
|
|
113
|
+
@object_ids ||= id_is_object
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# Is the id field type an object id?
|
|
119
|
+
#
|
|
120
|
+
# @example Is type an object id.
|
|
121
|
+
# Class.id_is_object
|
|
122
|
+
#
|
|
123
|
+
# @return [ true, false ] Is the id an object id.
|
|
124
|
+
#
|
|
125
|
+
# @since 2.0.0
|
|
126
|
+
def id_is_object
|
|
127
|
+
fields["_id"].type == BSON::ObjectId
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Mongoid
|
|
2
|
+
class Logger
|
|
3
|
+
|
|
4
|
+
delegate :info, :debug, :error, :fatal, :unknown, :to => :logger, :allow_nil => true
|
|
5
|
+
|
|
6
|
+
def warn(message)
|
|
7
|
+
logger.warn(message) if logger && logger.respond_to?(:warn)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def logger
|
|
11
|
+
Mongoid.logger
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def inspect
|
|
15
|
+
"#<Mongoid::Logger:0x#{object_id.to_s(16)} @logger=#{logger.inspect}>"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/matchers/strategies"
|
|
3
|
+
|
|
4
|
+
module Mongoid #:nodoc:
|
|
5
|
+
|
|
6
|
+
# This module contains all the behavior for ruby implementations of MongoDB
|
|
7
|
+
# selectors.
|
|
8
|
+
module Matchers
|
|
9
|
+
|
|
10
|
+
# Determines if this document has the attributes to match the supplied
|
|
11
|
+
# MongoDB selector. Used for matching on embedded associations.
|
|
12
|
+
#
|
|
13
|
+
# @example Does the document match?
|
|
14
|
+
# document.matches?(:title => { "$in" => [ "test" ] })
|
|
15
|
+
#
|
|
16
|
+
# @param [ Hash ] selector The MongoDB selector.
|
|
17
|
+
#
|
|
18
|
+
# @return [ true, false ] True if matches, false if not.
|
|
19
|
+
def matches?(selector)
|
|
20
|
+
selector.each_pair do |key, value|
|
|
21
|
+
if value.is_a?(Hash)
|
|
22
|
+
value.each do |item|
|
|
23
|
+
return false unless Strategies.matcher(self, key, Hash[*item]).matches?(Hash[*item])
|
|
24
|
+
end
|
|
25
|
+
else
|
|
26
|
+
return false unless Strategies.matcher(self, key, value).matches?(value)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
return true
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Matchers #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Contains all the default behavior for checking for matching documents
|
|
6
|
+
# given MongoDB expressions.
|
|
7
|
+
class Default
|
|
8
|
+
|
|
9
|
+
attr_accessor :attribute, :document
|
|
10
|
+
|
|
11
|
+
# Creating a new matcher only requires the value.
|
|
12
|
+
#
|
|
13
|
+
# @example Create a new matcher.
|
|
14
|
+
# Default.new("attribute")
|
|
15
|
+
#
|
|
16
|
+
# @param [ Object ] attribute The current attribute to check against.
|
|
17
|
+
#
|
|
18
|
+
# @since 1.0.0
|
|
19
|
+
def initialize(attribute, document = nil)
|
|
20
|
+
@attribute, @document = attribute, document
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Return true if the attribute and value are equal, or if it is an array
|
|
24
|
+
# if the value is included.
|
|
25
|
+
#
|
|
26
|
+
# @example Does this value match?
|
|
27
|
+
# default.matches?("value")
|
|
28
|
+
#
|
|
29
|
+
# @param [ Object ] value The value to check if it matches.
|
|
30
|
+
#
|
|
31
|
+
# @return [ true, false ] True if matches, false if not.
|
|
32
|
+
#
|
|
33
|
+
# @since 1.0.0
|
|
34
|
+
def matches?(value)
|
|
35
|
+
attribute.is_a?(Array) ? attribute.include?(value) : value === attribute
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
protected
|
|
39
|
+
|
|
40
|
+
# Convenience method for getting the first value in a hash.
|
|
41
|
+
#
|
|
42
|
+
# @example Get the first value.
|
|
43
|
+
# matcher.first(:test => "value")
|
|
44
|
+
#
|
|
45
|
+
# @param [ Hash ] hash The has to pull from.
|
|
46
|
+
#
|
|
47
|
+
# @return [ Object ] The first value.
|
|
48
|
+
#
|
|
49
|
+
# @since 1.0.0
|
|
50
|
+
def first(hash)
|
|
51
|
+
hash.values.first
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# If object exists then compare the two, otherwise return false
|
|
55
|
+
#
|
|
56
|
+
# @example Determine if we can compare.
|
|
57
|
+
# matcher.determine("test", "$in")
|
|
58
|
+
#
|
|
59
|
+
# @param [ Object ] value The value to compare with.
|
|
60
|
+
# @param [ Symbol, String ] operator The comparison operation.
|
|
61
|
+
#
|
|
62
|
+
# @return [ true, false ] The comparison or false.
|
|
63
|
+
#
|
|
64
|
+
# @since 1.0.0
|
|
65
|
+
def determine(value, operator)
|
|
66
|
+
attribute ? attribute.send(operator, first(value)) : false
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Matchers #:nodoc:
|
|
4
|
+
class Exists < Default
|
|
5
|
+
# Return true if the attribute exists and checking for existence or
|
|
6
|
+
# return true if the attribute does not exist and checking for
|
|
7
|
+
# non-existence.
|
|
8
|
+
def matches?(value)
|
|
9
|
+
@attribute.nil? != value.values.first
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Matchers #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Defines behavior for handling $or expressions in embedded documents.
|
|
6
|
+
class Or < Default
|
|
7
|
+
|
|
8
|
+
# Does the supplied query match the attribute?
|
|
9
|
+
#
|
|
10
|
+
# @example Does this match?
|
|
11
|
+
# matcher.matches?("$or" => [ { field => value } ])
|
|
12
|
+
#
|
|
13
|
+
# @param [ Array ] conditions The or expression.
|
|
14
|
+
#
|
|
15
|
+
# @return [ true, false ] True if matches, false if not.
|
|
16
|
+
#
|
|
17
|
+
# @since 2.0.0.rc.7
|
|
18
|
+
def matches?(conditions)
|
|
19
|
+
conditions.each do |condition|
|
|
20
|
+
key = condition.keys.first
|
|
21
|
+
value = condition.values.first
|
|
22
|
+
if Strategies.matcher(document, key, value).matches?(value)
|
|
23
|
+
return true
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
return false
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/matchers/default"
|
|
3
|
+
require "mongoid/matchers/all"
|
|
4
|
+
require "mongoid/matchers/exists"
|
|
5
|
+
require "mongoid/matchers/gt"
|
|
6
|
+
require "mongoid/matchers/gte"
|
|
7
|
+
require "mongoid/matchers/in"
|
|
8
|
+
require "mongoid/matchers/lt"
|
|
9
|
+
require "mongoid/matchers/lte"
|
|
10
|
+
require "mongoid/matchers/ne"
|
|
11
|
+
require "mongoid/matchers/nin"
|
|
12
|
+
require "mongoid/matchers/or"
|
|
13
|
+
require "mongoid/matchers/size"
|
|
14
|
+
|
|
15
|
+
module Mongoid #:nodoc:
|
|
16
|
+
module Matchers #:nodoc:
|
|
17
|
+
|
|
18
|
+
# This module is responsible for returning the correct matcher given a
|
|
19
|
+
# MongoDB query expression.
|
|
20
|
+
module Strategies
|
|
21
|
+
extend self
|
|
22
|
+
|
|
23
|
+
MATCHERS = {
|
|
24
|
+
"$all" => Matchers::All,
|
|
25
|
+
"$exists" => Matchers::Exists,
|
|
26
|
+
"$gt" => Matchers::Gt,
|
|
27
|
+
"$gte" => Matchers::Gte,
|
|
28
|
+
"$in" => Matchers::In,
|
|
29
|
+
"$lt" => Matchers::Lt,
|
|
30
|
+
"$lte" => Matchers::Lte,
|
|
31
|
+
"$ne" => Matchers::Ne,
|
|
32
|
+
"$nin" => Matchers::Nin,
|
|
33
|
+
"$or" => Matchers::Or,
|
|
34
|
+
"$size" => Matchers::Size
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
# Get the matcher for the supplied key and value. Will determine the class
|
|
38
|
+
# name from the key.
|
|
39
|
+
#
|
|
40
|
+
# @example Get the matcher.
|
|
41
|
+
# document.matcher(:title, { "$in" => [ "test" ] })
|
|
42
|
+
#
|
|
43
|
+
# @param [ Document ] document The document to check.
|
|
44
|
+
# @param [ Symbol, String ] key The field name.
|
|
45
|
+
# @param [ Object, Hash ] The value or selector.
|
|
46
|
+
#
|
|
47
|
+
# @return [ Matcher ] The matcher.
|
|
48
|
+
#
|
|
49
|
+
# @since 2.0.0.rc.7
|
|
50
|
+
def matcher(document, key, value)
|
|
51
|
+
if value.is_a?(Hash)
|
|
52
|
+
MATCHERS[value.keys.first].new(document.attributes[key.to_s])
|
|
53
|
+
else
|
|
54
|
+
if key == "$or"
|
|
55
|
+
Matchers::Or.new(value, document)
|
|
56
|
+
else
|
|
57
|
+
Default.new(document.attributes[key.to_s])
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|