mongomodel 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
@@ -1,27 +1,27 @@
|
|
1
1
|
module MongoModel
|
2
2
|
class MongoOperator
|
3
3
|
attr_reader :field, :operator
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(field, operator)
|
6
6
|
@field, @operator = field, operator
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def to_mongo_selector(value)
|
10
10
|
{ "$#{operator}" => value }
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def inspect
|
14
14
|
"#{field.inspect}.#{operator}"
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def ==(other)
|
18
18
|
other.is_a?(self.class) && field == other.field && operator == other.operator
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def hash
|
22
22
|
field.hash ^ operator.hash
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def eql?(other)
|
26
26
|
self == other
|
27
27
|
end
|
@@ -3,33 +3,33 @@ require 'active_support/core_ext/class/subclasses'
|
|
3
3
|
module MongoModel
|
4
4
|
class MongoOptions
|
5
5
|
ValidKeys = [ :conditions, :select, :offset, :limit, :order ]
|
6
|
-
|
6
|
+
|
7
7
|
attr_reader :selector, :options
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(model, options={})
|
10
10
|
options.assert_valid_keys(ValidKeys)
|
11
|
-
|
11
|
+
|
12
12
|
@model = model
|
13
|
-
|
13
|
+
|
14
14
|
@selector = extract_conditions(options)
|
15
15
|
@options = extract_options(options)
|
16
|
-
|
16
|
+
|
17
17
|
add_type_to_selector
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def to_a
|
21
21
|
[selector, options]
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
private
|
25
25
|
def extract_conditions(options)
|
26
26
|
result = {}
|
27
|
-
|
27
|
+
|
28
28
|
(options[:conditions] || {}).each do |k, v|
|
29
29
|
k = k.to_mongo_operator if k.respond_to?(:to_mongo_operator)
|
30
|
-
|
30
|
+
|
31
31
|
key = k.respond_to?(:field) ? k.field : k
|
32
|
-
|
32
|
+
|
33
33
|
if @model.respond_to?(:properties) && property = @model.properties[key]
|
34
34
|
key = property.as
|
35
35
|
value = v.is_a?(Array) ? v.map { |i| property.to_query(i) } : property.to_query(v);
|
@@ -39,7 +39,7 @@ module MongoModel
|
|
39
39
|
|
40
40
|
if k.respond_to?(:to_mongo_selector)
|
41
41
|
selector = k.to_mongo_selector(value)
|
42
|
-
|
42
|
+
|
43
43
|
if result[key].is_a?(Hash)
|
44
44
|
result[key].merge!(selector)
|
45
45
|
else
|
@@ -49,33 +49,33 @@ module MongoModel
|
|
49
49
|
result[key] = value
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
result
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
def extract_options(options)
|
57
57
|
result = {}
|
58
|
-
|
58
|
+
|
59
59
|
result[:fields] = convert_select(options[:select]) if options[:select]
|
60
60
|
result[:skip] = options[:offset] if options[:offset]
|
61
61
|
result[:limit] = options[:limit] if options[:limit]
|
62
62
|
result[:sort] = MongoOrder.parse(options[:order]).to_sort(@model) if options[:order]
|
63
|
-
|
63
|
+
|
64
64
|
result
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
def convert_select(fields)
|
68
68
|
fields.map do |key|
|
69
69
|
(@model.properties[key.to_sym].try(:as) || key).to_sym
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def add_type_to_selector
|
74
74
|
if use_type_selector?
|
75
75
|
selector['_type'] = { '$in' => @model.type_selector }
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
def use_type_selector?
|
80
80
|
@model.respond_to?(:use_type_selector?) && @model.use_type_selector? && selector['_type'].nil?
|
81
81
|
end
|
@@ -1,36 +1,36 @@
|
|
1
1
|
module MongoModel
|
2
2
|
class MongoOrder
|
3
3
|
attr_reader :clauses
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(*clauses)
|
6
6
|
@clauses = clauses
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def to_a
|
10
10
|
clauses
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def to_s
|
14
14
|
clauses.map { |c| c.to_s }.join(', ')
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def to_sort(model)
|
18
18
|
clauses.map { |c| c.to_sort(model.respond_to?(:properties) ? model.properties[c.field] : nil) }
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def ==(other)
|
22
22
|
other.is_a?(self.class) && clauses == other.clauses
|
23
23
|
end
|
24
24
|
alias eql? ==
|
25
|
-
|
25
|
+
|
26
26
|
def hash
|
27
27
|
clauses.hash
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def reverse
|
31
31
|
self.class.new(*clauses.map { |c| c.reverse })
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def self.parse(order)
|
35
35
|
case order
|
36
36
|
when MongoOrder
|
@@ -47,35 +47,35 @@ module MongoModel
|
|
47
47
|
new(order.to_mongo_order_clause) if order.respond_to?(:to_mongo_order_clause)
|
48
48
|
end
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
class Clause
|
52
52
|
attr_reader :field, :order
|
53
|
-
|
53
|
+
|
54
54
|
def initialize(field, order=:ascending)
|
55
55
|
@field, @order = field.to_sym, order.to_sym
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def to_s
|
59
59
|
"#{field} #{order}"
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
def to_sort(property)
|
63
63
|
[property ? property.as : field.to_s, order]
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def reverse
|
67
67
|
self.class.new(field, order == :ascending ? :descending : :ascending)
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
def ==(other)
|
71
71
|
other.is_a?(self.class) && field == other.field && order == other.order
|
72
72
|
end
|
73
73
|
alias eql? ==
|
74
|
-
|
74
|
+
|
75
75
|
def hash
|
76
76
|
[field, order].hash
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
def self.parse(clause)
|
80
80
|
case clause
|
81
81
|
when Clause
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module MongoModel
|
2
2
|
class Paginator < Array
|
3
3
|
attr_reader :current_page, :per_page, :total_entries
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(scope, page, per_page)
|
6
6
|
@current_page = page.to_i
|
7
7
|
@per_page = per_page.to_i
|
8
|
-
|
8
|
+
|
9
9
|
super(scope.offset(offset).limit(per_page))
|
10
|
-
|
10
|
+
|
11
11
|
# Try to autodetect total entries
|
12
12
|
if total_entries.nil? && size < per_page && (current_page == 1 or size > 0)
|
13
13
|
@total_entries = offset + size
|
@@ -15,23 +15,23 @@ module MongoModel
|
|
15
15
|
@total_entries = scope.count
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def total_pages
|
20
20
|
total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def previous_page
|
24
24
|
current_page > 1 ? (current_page - 1) : nil
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def next_page
|
28
28
|
current_page < total_pages ? (current_page + 1) : nil
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def out_of_bounds?
|
32
32
|
current_page > total_pages
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def offset
|
36
36
|
(current_page - 1) * per_page
|
37
37
|
end
|
@@ -1,29 +1,29 @@
|
|
1
1
|
module MongoModel
|
2
2
|
class Reference
|
3
3
|
attr_reader :id
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(id)
|
6
6
|
@id = id
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def to_s
|
10
10
|
id.to_s
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
alias :to_str :to_s
|
14
|
-
|
14
|
+
|
15
15
|
def hash
|
16
16
|
id.hash
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def as_json(*)
|
20
20
|
to_s
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def blank?
|
24
24
|
id.blank?
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def eql?(other)
|
28
28
|
case other
|
29
29
|
when Reference
|
@@ -33,11 +33,11 @@ module MongoModel
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
alias_method :==, :eql?
|
36
|
-
|
36
|
+
|
37
37
|
def to_mongo
|
38
38
|
id.blank? ? nil : id
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def self.cast(value)
|
42
42
|
case value
|
43
43
|
when BSON::ObjectId
|
@@ -50,7 +50,7 @@ module MongoModel
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def self.from_mongo(value)
|
55
55
|
cast(value)
|
56
56
|
end
|
@@ -4,7 +4,7 @@ module MongoModel
|
|
4
4
|
class Scope
|
5
5
|
MULTI_VALUE_METHODS = [ :select, :order, :where ]
|
6
6
|
SINGLE_VALUE_METHODS = [ :limit, :offset, :from ]
|
7
|
-
|
7
|
+
|
8
8
|
autoload :ArrayMethods, 'mongomodel/support/scope/array_methods'
|
9
9
|
autoload :SpawnMethods, 'mongomodel/support/scope/spawn_methods'
|
10
10
|
autoload :QueryMethods, 'mongomodel/support/scope/query_methods'
|
@@ -13,72 +13,72 @@ module MongoModel
|
|
13
13
|
autoload :DynamicFinders, 'mongomodel/support/scope/dynamic_finders'
|
14
14
|
autoload :Pagination, 'mongomodel/support/scope/pagination'
|
15
15
|
autoload :Batches, 'mongomodel/support/scope/batches'
|
16
|
-
|
16
|
+
|
17
17
|
include Batches, Pagination, DynamicFinders, LoadMethods, FinderMethods, ArrayMethods, QueryMethods, SpawnMethods
|
18
|
-
|
18
|
+
|
19
19
|
delegate :inspect, :as_json, :to => :to_a
|
20
|
-
|
20
|
+
|
21
21
|
attr_reader :klass
|
22
|
-
|
22
|
+
|
23
23
|
def initialize(klass)
|
24
24
|
super
|
25
|
-
|
25
|
+
|
26
26
|
@klass = klass
|
27
|
-
|
27
|
+
|
28
28
|
@loaded = false
|
29
29
|
@documents = []
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def initialize_copy(other)
|
33
33
|
reset
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def build(*args, &block)
|
37
37
|
new(*args, &block)
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def to_a
|
41
41
|
return @documents if loaded?
|
42
|
-
|
42
|
+
|
43
43
|
@documents = _find_and_instantiate
|
44
44
|
@loaded = true
|
45
|
-
|
45
|
+
|
46
46
|
@documents
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
def size
|
50
50
|
loaded? ? @documents.size : count
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
def empty?
|
54
54
|
loaded? ? @documents.empty? : count.zero?
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def count
|
58
58
|
_find.count
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
def destroy_all
|
62
62
|
to_a.each { |doc| doc.destroy }
|
63
63
|
reset
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def destroy(*ids)
|
67
67
|
where(ids_to_conditions(ids)).destroy_all
|
68
68
|
reset
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
def delete_all
|
72
72
|
selector = MongoOptions.new(klass, :conditions => finder_conditions).selector
|
73
73
|
collection.remove(selector, {})
|
74
74
|
reset
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
def delete(*ids)
|
78
78
|
where(ids_to_conditions(ids)).delete_all
|
79
79
|
reset
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def update_all(updates)
|
83
83
|
if updates.any?
|
84
84
|
selector = MongoOptions.new(klass, :conditions => finder_conditions).selector
|
@@ -88,22 +88,22 @@ module MongoModel
|
|
88
88
|
self
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
def update(ids, updates)
|
93
93
|
where(ids_to_conditions(ids)).update_all(updates)
|
94
94
|
reset
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
def loaded?
|
98
98
|
@loaded
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
def reload
|
102
102
|
reset
|
103
103
|
to_a
|
104
104
|
self
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
def reset
|
108
108
|
@loaded = nil
|
109
109
|
@documents = []
|
@@ -111,7 +111,7 @@ module MongoModel
|
|
111
111
|
@options_for_create = nil
|
112
112
|
self
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
def ==(other)
|
116
116
|
case other
|
117
117
|
when Scope
|
@@ -122,11 +122,11 @@ module MongoModel
|
|
122
122
|
to_a == other.to_a
|
123
123
|
end
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
def collection
|
127
127
|
from_value || klass.collection
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
def finder_options
|
131
131
|
@finder_options ||= {}.tap do |result|
|
132
132
|
result[:conditions] = finder_conditions if where_values.any?
|
@@ -136,15 +136,15 @@ module MongoModel
|
|
136
136
|
result[:offset] = offset_value if offset_value.present?
|
137
137
|
end
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
def options_for_create
|
141
141
|
@options_for_create ||= finder_conditions.reject { |k, v| k.is_a?(MongoModel::MongoOperator) || k.respond_to?(:to_mongo_operator) }
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
def respond_to?(method, include_private = false)
|
145
145
|
Array.method_defined?(method) || klass.respond_to?(method, include_private) || super
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
protected
|
149
149
|
def method_missing(method, *args, &block)
|
150
150
|
if Array.method_defined?(method)
|
@@ -157,14 +157,14 @@ module MongoModel
|
|
157
157
|
super
|
158
158
|
end
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
private
|
162
162
|
def _find
|
163
163
|
ensure_indexes!
|
164
164
|
selector, options = MongoOptions.new(klass, finder_options).to_a
|
165
165
|
collection.find(selector, options)
|
166
166
|
end
|
167
|
-
|
167
|
+
|
168
168
|
def _find_and_instantiate
|
169
169
|
_find.to_a.map { |doc|
|
170
170
|
klass.from_mongo(doc).tap { |instance|
|
@@ -172,25 +172,25 @@ module MongoModel
|
|
172
172
|
}
|
173
173
|
}
|
174
174
|
end
|
175
|
-
|
175
|
+
|
176
176
|
def finder_conditions
|
177
177
|
where_values.inject({}) { |conditions, v| conditions.merge(v) }
|
178
178
|
end
|
179
|
-
|
179
|
+
|
180
180
|
def with_scope(&block)
|
181
181
|
klass.send(:with_scope, self, &block)
|
182
182
|
end
|
183
|
-
|
183
|
+
|
184
184
|
def ids_to_conditions(ids)
|
185
185
|
ids = Array.wrap(ids).flatten
|
186
|
-
|
186
|
+
|
187
187
|
if ids.size == 1
|
188
188
|
{ :id => ids.first.to_s }
|
189
189
|
else
|
190
190
|
{ :id.in => ids.map { |id| id.to_s } }
|
191
191
|
end
|
192
192
|
end
|
193
|
-
|
193
|
+
|
194
194
|
def ensure_indexes!
|
195
195
|
if klass.respond_to?(:indexes_initialized?) && !klass.indexes_initialized?
|
196
196
|
klass.ensure_indexes!
|