thorsson-mongo_mapper 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +27 -0
- data/bin/mmconsole +60 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper.rb +79 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins.rb +16 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +97 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
- data/lib/mongo_mapper/plugins/clone.rb +19 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +120 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +309 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
- data/test/functional/associations/test_in_array_proxy.rb +319 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +575 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +182 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +253 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +506 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +104 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +202 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +213 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +644 -0
- data/test/unit/test_extensions.rb +380 -0
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +55 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +544 -0
- metadata +316 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Keys
|
5
|
+
class Key
|
6
|
+
attr_accessor :name, :type, :options, :default_value
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
options = args.extract_options!
|
10
|
+
@name, @type = args.shift.to_s, args.shift
|
11
|
+
self.options = (options || {}).symbolize_keys
|
12
|
+
self.default_value = self.options[:default]
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
@name == other.name && @type == other.type
|
17
|
+
end
|
18
|
+
|
19
|
+
def embeddable?
|
20
|
+
return false unless type.respond_to?(:embeddable?)
|
21
|
+
type.embeddable?
|
22
|
+
end
|
23
|
+
|
24
|
+
def number?
|
25
|
+
[Integer, Float].include?(type)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(value)
|
29
|
+
if value.nil? && !default_value.nil?
|
30
|
+
if default_value.respond_to?(:call)
|
31
|
+
return default_value.call
|
32
|
+
else
|
33
|
+
return default_value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
type.from_mongo(value)
|
38
|
+
end
|
39
|
+
|
40
|
+
def set(value)
|
41
|
+
type.to_mongo(value).tap do |values|
|
42
|
+
if options[:typecast].present?
|
43
|
+
values.map! { |v| typecast_class.to_mongo(v) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def typecast_class
|
50
|
+
@typecast_class ||= options[:typecast].constantize
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Modifiers
|
5
|
+
module ClassMethods
|
6
|
+
def increment(*args)
|
7
|
+
modifier_update('$inc', args)
|
8
|
+
end
|
9
|
+
|
10
|
+
def decrement(*args)
|
11
|
+
criteria, keys = criteria_and_keys_from_args(args)
|
12
|
+
values, to_decrement = keys.values, {}
|
13
|
+
keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
|
14
|
+
collection.update(criteria, {'$inc' => to_decrement}, :multi => true)
|
15
|
+
end
|
16
|
+
|
17
|
+
def set(*args)
|
18
|
+
criteria, updates = criteria_and_keys_from_args(args)
|
19
|
+
updates.each do |key, value|
|
20
|
+
updates[key] = keys[key].set(value) if key?(key)
|
21
|
+
end
|
22
|
+
collection.update(criteria, {'$set' => updates}, :multi => true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def unset(*args)
|
26
|
+
if args[0].is_a?(Hash)
|
27
|
+
criteria, keys = args.shift, args
|
28
|
+
else
|
29
|
+
keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
|
30
|
+
criteria = {:id => ids}
|
31
|
+
end
|
32
|
+
|
33
|
+
criteria = criteria_hash(criteria).to_hash
|
34
|
+
modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
|
35
|
+
collection.update(criteria, {'$unset' => modifiers}, :multi => true)
|
36
|
+
end
|
37
|
+
|
38
|
+
def push(*args)
|
39
|
+
modifier_update('$push', args)
|
40
|
+
end
|
41
|
+
|
42
|
+
def push_all(*args)
|
43
|
+
modifier_update('$pushAll', args)
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_to_set(*args)
|
47
|
+
modifier_update('$addToSet', args)
|
48
|
+
end
|
49
|
+
alias push_uniq add_to_set
|
50
|
+
|
51
|
+
def pull(*args)
|
52
|
+
modifier_update('$pull', args)
|
53
|
+
end
|
54
|
+
|
55
|
+
def pull_all(*args)
|
56
|
+
modifier_update('$pullAll', args)
|
57
|
+
end
|
58
|
+
|
59
|
+
def pop(*args)
|
60
|
+
modifier_update('$pop', args)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def modifier_update(modifier, args)
|
65
|
+
criteria, updates = criteria_and_keys_from_args(args)
|
66
|
+
collection.update(criteria, {modifier => updates}, :multi => true)
|
67
|
+
end
|
68
|
+
|
69
|
+
def criteria_and_keys_from_args(args)
|
70
|
+
keys = args.pop
|
71
|
+
criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
|
72
|
+
[criteria_hash(criteria).to_hash, keys]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
module InstanceMethods
|
77
|
+
def unset(*keys)
|
78
|
+
self.class.unset(id, *keys)
|
79
|
+
end
|
80
|
+
|
81
|
+
def increment(hash)
|
82
|
+
self.class.increment(id, hash)
|
83
|
+
end
|
84
|
+
|
85
|
+
def decrement(hash)
|
86
|
+
self.class.decrement(id, hash)
|
87
|
+
end
|
88
|
+
|
89
|
+
def set(hash)
|
90
|
+
self.class.set(id, hash)
|
91
|
+
end
|
92
|
+
|
93
|
+
def push(hash)
|
94
|
+
self.class.push(id, hash)
|
95
|
+
end
|
96
|
+
|
97
|
+
def pull(hash)
|
98
|
+
self.class.pull(id, hash)
|
99
|
+
end
|
100
|
+
|
101
|
+
def add_to_set(hash)
|
102
|
+
self.class.push_uniq(id, hash)
|
103
|
+
end
|
104
|
+
alias push_uniq add_to_set
|
105
|
+
|
106
|
+
def pop(hash)
|
107
|
+
self.class.pop(id, hash)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Persistence
|
5
|
+
module ClassMethods
|
6
|
+
def connection(mongo_connection=nil)
|
7
|
+
assert_supported
|
8
|
+
if mongo_connection.nil?
|
9
|
+
@connection ||= MongoMapper.connection
|
10
|
+
else
|
11
|
+
@connection = mongo_connection
|
12
|
+
end
|
13
|
+
@connection
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_database_name(name)
|
17
|
+
assert_supported
|
18
|
+
@database_name = name
|
19
|
+
end
|
20
|
+
|
21
|
+
def database_name
|
22
|
+
assert_supported
|
23
|
+
@database_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def database
|
27
|
+
assert_supported
|
28
|
+
if database_name.nil?
|
29
|
+
MongoMapper.database
|
30
|
+
else
|
31
|
+
connection.db(database_name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_collection_name(name)
|
36
|
+
assert_supported
|
37
|
+
@collection_name = name
|
38
|
+
end
|
39
|
+
|
40
|
+
def collection_name
|
41
|
+
assert_supported
|
42
|
+
@collection_name ||= self.to_s.tableize.gsub(/\//, '.')
|
43
|
+
end
|
44
|
+
|
45
|
+
def collection
|
46
|
+
assert_supported
|
47
|
+
database.collection(collection_name)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def assert_supported
|
52
|
+
if embeddable?
|
53
|
+
raise NotSupported.new('This is not supported for embeddable documents at this time.')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module InstanceMethods
|
59
|
+
def collection
|
60
|
+
_root_document.class.collection
|
61
|
+
end
|
62
|
+
|
63
|
+
def database
|
64
|
+
_root_document.class.database
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
module MongoMapper
|
5
|
+
module Plugins
|
6
|
+
module Protected
|
7
|
+
module ClassMethods
|
8
|
+
def attr_protected(*attrs)
|
9
|
+
raise AccessibleOrProtected.new(name) if try(:accessible_attributes?)
|
10
|
+
self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
|
11
|
+
end
|
12
|
+
|
13
|
+
def protected_attributes
|
14
|
+
self.read_inheritable_attribute(:attr_protected)
|
15
|
+
end
|
16
|
+
|
17
|
+
def protected_attributes?
|
18
|
+
!protected_attributes.nil?
|
19
|
+
end
|
20
|
+
|
21
|
+
def key(*args)
|
22
|
+
key = super
|
23
|
+
attr_protected key.name.to_sym if key.options[:protected]
|
24
|
+
key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
def assign(attrs={})
|
30
|
+
super(filter_protected_attrs(attrs))
|
31
|
+
end
|
32
|
+
|
33
|
+
def update_attributes(attrs={})
|
34
|
+
super(filter_protected_attrs(attrs))
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_attributes!(attrs={})
|
38
|
+
super(filter_protected_attrs(attrs))
|
39
|
+
end
|
40
|
+
|
41
|
+
def protected_attributes
|
42
|
+
self.class.protected_attributes
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
def filter_protected_attrs(attrs)
|
47
|
+
return attrs if protected_attributes.blank? || attrs.blank?
|
48
|
+
attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'mongo_mapper/plugins/querying/decorator'
|
3
|
+
require 'mongo_mapper/plugins/querying/plucky_methods'
|
4
|
+
|
5
|
+
module MongoMapper
|
6
|
+
module Plugins
|
7
|
+
module Querying
|
8
|
+
module ClassMethods
|
9
|
+
include PluckyMethods
|
10
|
+
|
11
|
+
def find_each(opts={})
|
12
|
+
super(opts).each { |doc| yield load(doc) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_by_id(id)
|
16
|
+
find_one(:_id => id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def first_or_create(args)
|
20
|
+
first(args) || create(args.reject { |key, value| !key?(key) })
|
21
|
+
end
|
22
|
+
|
23
|
+
def first_or_new(args)
|
24
|
+
first(args) || new(args.reject { |key, value| !key?(key) })
|
25
|
+
end
|
26
|
+
|
27
|
+
def create(*docs)
|
28
|
+
initialize_each(*docs) { |doc| doc.save }
|
29
|
+
end
|
30
|
+
|
31
|
+
def create!(*docs)
|
32
|
+
initialize_each(*docs) { |doc| doc.save! }
|
33
|
+
end
|
34
|
+
|
35
|
+
def update(*args)
|
36
|
+
if args.length == 1
|
37
|
+
update_multiple(args[0])
|
38
|
+
else
|
39
|
+
id, attributes = args
|
40
|
+
update_single(id, attributes)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete(*ids)
|
45
|
+
query(:_id => ids.flatten).remove
|
46
|
+
end
|
47
|
+
|
48
|
+
def delete_all(options={})
|
49
|
+
query(options).remove
|
50
|
+
end
|
51
|
+
|
52
|
+
def destroy(*ids)
|
53
|
+
find_some!(ids.flatten).each(&:destroy)
|
54
|
+
end
|
55
|
+
|
56
|
+
def destroy_all(options={})
|
57
|
+
find_each(options) { |document| document.destroy }
|
58
|
+
end
|
59
|
+
|
60
|
+
# @api private for now
|
61
|
+
def query(options={})
|
62
|
+
Plucky::Query.new(collection).tap do |query|
|
63
|
+
query.extend(Decorator)
|
64
|
+
query.object_ids(object_id_keys)
|
65
|
+
query.update(options)
|
66
|
+
query.model(self)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# @api private for now
|
71
|
+
def criteria_hash(criteria={})
|
72
|
+
Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
def find_some(ids, options={})
|
77
|
+
query = query(options).update(:_id => ids.flatten.compact.uniq)
|
78
|
+
find_many(query.to_hash).compact
|
79
|
+
end
|
80
|
+
|
81
|
+
def find_some!(ids, options={})
|
82
|
+
ids = ids.flatten.compact.uniq
|
83
|
+
docs = find_some(ids, options)
|
84
|
+
|
85
|
+
if ids.size != docs.size
|
86
|
+
raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{docs.size}, but was expecting #{ids.size}"
|
87
|
+
end
|
88
|
+
|
89
|
+
docs
|
90
|
+
end
|
91
|
+
|
92
|
+
# All query methods that load documents pass through find_one or find_many
|
93
|
+
def find_one(options={})
|
94
|
+
query(options).first
|
95
|
+
end
|
96
|
+
|
97
|
+
# All query methods that load documents pass through find_one or find_many
|
98
|
+
def find_many(options)
|
99
|
+
query(options).all
|
100
|
+
end
|
101
|
+
|
102
|
+
def initialize_each(*docs)
|
103
|
+
instances = []
|
104
|
+
docs = [{}] if docs.blank?
|
105
|
+
docs.flatten.each do |attrs|
|
106
|
+
doc = new(attrs)
|
107
|
+
yield(doc)
|
108
|
+
instances << doc
|
109
|
+
end
|
110
|
+
instances.size == 1 ? instances[0] : instances
|
111
|
+
end
|
112
|
+
|
113
|
+
def update_single(id, attrs)
|
114
|
+
if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
|
115
|
+
raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
|
116
|
+
end
|
117
|
+
|
118
|
+
find(id).tap do |doc|
|
119
|
+
doc.update_attributes(attrs)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def update_multiple(docs)
|
124
|
+
unless docs.is_a?(Hash)
|
125
|
+
raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
|
126
|
+
end
|
127
|
+
|
128
|
+
instances = []
|
129
|
+
docs.each_pair { |id, attrs| instances << update(id, attrs) }
|
130
|
+
instances
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
module InstanceMethods
|
135
|
+
def save(options={})
|
136
|
+
options.assert_valid_keys(:validate, :safe)
|
137
|
+
options.reverse_merge!(:validate => true)
|
138
|
+
!options[:validate] || valid? ? create_or_update(options) : false
|
139
|
+
end
|
140
|
+
|
141
|
+
def save!(options={})
|
142
|
+
options.assert_valid_keys(:safe)
|
143
|
+
save(options) || raise(DocumentNotValid.new(self))
|
144
|
+
end
|
145
|
+
|
146
|
+
def destroy
|
147
|
+
delete
|
148
|
+
end
|
149
|
+
|
150
|
+
def delete
|
151
|
+
@_destroyed = true
|
152
|
+
self.class.delete(id) unless new?
|
153
|
+
end
|
154
|
+
|
155
|
+
private
|
156
|
+
def create_or_update(options={})
|
157
|
+
result = new? ? create(options) : update(options)
|
158
|
+
result != false
|
159
|
+
end
|
160
|
+
|
161
|
+
def create(options={})
|
162
|
+
save_to_collection(options)
|
163
|
+
end
|
164
|
+
|
165
|
+
def update(options={})
|
166
|
+
save_to_collection(options)
|
167
|
+
end
|
168
|
+
|
169
|
+
def save_to_collection(options={})
|
170
|
+
@_new = false
|
171
|
+
collection.save(to_mongo, :safe => options[:safe])
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|