mongo_mapper_ign 0.7.7 → 0.7.8
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -20
- data/README.rdoc +34 -34
- data/bin/mmconsole +60 -60
- data/lib/mongo_mapper.rb +123 -123
- data/lib/mongo_mapper/document.rb +292 -292
- data/lib/mongo_mapper/embedded_document.rb +71 -71
- data/lib/mongo_mapper/plugins.rb +36 -36
- data/lib/mongo_mapper/plugins/associations.rb +115 -115
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -124
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +31 -31
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +26 -26
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -21
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -39
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +160 -144
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +29 -29
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +130 -130
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -32
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -24
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +42 -42
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +70 -70
- data/lib/mongo_mapper/plugins/associations/proxy.rb +125 -125
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -241
- data/lib/mongo_mapper/plugins/clone.rb +13 -13
- data/lib/mongo_mapper/plugins/descendants.rb +16 -16
- data/lib/mongo_mapper/plugins/dirty.rb +119 -119
- data/lib/mongo_mapper/plugins/equality.rb +23 -23
- data/lib/mongo_mapper/plugins/identity_map.rb +123 -123
- data/lib/mongo_mapper/plugins/inspect.rb +14 -14
- data/lib/mongo_mapper/plugins/keys.rb +322 -322
- data/lib/mongo_mapper/plugins/keys/key.rb +53 -53
- data/lib/mongo_mapper/plugins/logger.rb +17 -17
- data/lib/mongo_mapper/plugins/modifiers.rb +111 -111
- data/lib/mongo_mapper/plugins/pagination.rb +24 -24
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -72
- data/lib/mongo_mapper/plugins/persistence.rb +96 -96
- data/lib/mongo_mapper/plugins/protected.rb +46 -46
- data/lib/mongo_mapper/plugins/rails.rb +57 -57
- data/lib/mongo_mapper/plugins/serialization.rb +92 -92
- data/lib/mongo_mapper/plugins/serialization/array.rb +56 -56
- data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +239 -239
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -21
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -14
- data/lib/mongo_mapper/plugins/validations.rb +46 -46
- data/lib/mongo_mapper/query.rb +28 -28
- data/lib/mongo_mapper/support.rb +197 -197
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -46
- data/lib/mongo_mapper/support/find.rb +77 -77
- data/lib/mongo_mapper/version.rb +3 -3
- metadata +4 -25
@@ -1,22 +1,22 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Timestamps
|
5
|
-
module ClassMethods
|
6
|
-
def timestamps!
|
7
|
-
key :created_at, Time
|
8
|
-
key :updated_at, Time
|
9
|
-
class_eval { before_save :update_timestamps }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module InstanceMethods
|
14
|
-
def update_timestamps
|
15
|
-
now = Time.now.utc
|
16
|
-
self[:created_at] = now if new? && !created_at?
|
17
|
-
self[:updated_at] = now
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Timestamps
|
5
|
+
module ClassMethods
|
6
|
+
def timestamps!
|
7
|
+
key :created_at, Time
|
8
|
+
key :updated_at, Time
|
9
|
+
class_eval { before_save :update_timestamps }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def update_timestamps
|
15
|
+
now = Time.now.utc
|
16
|
+
self[:created_at] = now if new? && !created_at?
|
17
|
+
self[:updated_at] = now
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
22
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Userstamps
|
5
|
-
module ClassMethods
|
6
|
-
def userstamps!
|
7
|
-
key :creator_id, ObjectId
|
8
|
-
key :updater_id, ObjectId
|
9
|
-
belongs_to :creator, :class_name => 'User'
|
10
|
-
belongs_to :updater, :class_name => 'User'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Userstamps
|
5
|
+
module ClassMethods
|
6
|
+
def userstamps!
|
7
|
+
key :creator_id, ObjectId
|
8
|
+
key :updater_id, ObjectId
|
9
|
+
belongs_to :creator, :class_name => 'User'
|
10
|
+
belongs_to :updater, :class_name => 'User'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
15
|
end
|
@@ -1,47 +1,47 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Validations
|
5
|
-
def self.configure(model)
|
6
|
-
model.class_eval { include Validatable }
|
7
|
-
end
|
8
|
-
|
9
|
-
module DocumentMacros
|
10
|
-
def validates_uniqueness_of(*args)
|
11
|
-
add_validations(args, MongoMapper::Plugins::Validations::ValidatesUniquenessOf)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class ValidatesUniquenessOf < Validatable::ValidationBase
|
16
|
-
option :scope, :case_sensitive
|
17
|
-
default :case_sensitive => true
|
18
|
-
|
19
|
-
def valid?(instance)
|
20
|
-
value = instance[attribute]
|
21
|
-
return true if allow_blank && value.blank?
|
22
|
-
return true if allow_nil && value.nil?
|
23
|
-
base_conditions = case_sensitive ? {self.attribute => value} : {}
|
24
|
-
doc = instance.class.first(base_conditions.merge(scope_conditions(instance)).merge(where_conditions(instance)))
|
25
|
-
doc.nil? || instance._id == doc._id
|
26
|
-
end
|
27
|
-
|
28
|
-
def message(instance)
|
29
|
-
super || "has already been taken"
|
30
|
-
end
|
31
|
-
|
32
|
-
def scope_conditions(instance)
|
33
|
-
return {} unless scope
|
34
|
-
Array(scope).inject({}) do |conditions, key|
|
35
|
-
conditions.merge(key => instance[key])
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def where_conditions(instance)
|
40
|
-
conditions = {}
|
41
|
-
conditions[attribute] = /^#{Regexp.escape(instance[attribute].to_s)}$/i unless case_sensitive
|
42
|
-
conditions
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Validations
|
5
|
+
def self.configure(model)
|
6
|
+
model.class_eval { include Validatable }
|
7
|
+
end
|
8
|
+
|
9
|
+
module DocumentMacros
|
10
|
+
def validates_uniqueness_of(*args)
|
11
|
+
add_validations(args, MongoMapper::Plugins::Validations::ValidatesUniquenessOf)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ValidatesUniquenessOf < Validatable::ValidationBase
|
16
|
+
option :scope, :case_sensitive
|
17
|
+
default :case_sensitive => true
|
18
|
+
|
19
|
+
def valid?(instance)
|
20
|
+
value = instance[attribute]
|
21
|
+
return true if allow_blank && value.blank?
|
22
|
+
return true if allow_nil && value.nil?
|
23
|
+
base_conditions = case_sensitive ? {self.attribute => value} : {}
|
24
|
+
doc = instance.class.first(base_conditions.merge(scope_conditions(instance)).merge(where_conditions(instance)))
|
25
|
+
doc.nil? || instance._id == doc._id
|
26
|
+
end
|
27
|
+
|
28
|
+
def message(instance)
|
29
|
+
super || "has already been taken"
|
30
|
+
end
|
31
|
+
|
32
|
+
def scope_conditions(instance)
|
33
|
+
return {} unless scope
|
34
|
+
Array(scope).inject({}) do |conditions, key|
|
35
|
+
conditions.merge(key => instance[key])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def where_conditions(instance)
|
40
|
+
conditions = {}
|
41
|
+
conditions[attribute] = /^#{Regexp.escape(instance[attribute].to_s)}$/i unless case_sensitive
|
42
|
+
conditions
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
47
|
end
|
data/lib/mongo_mapper/query.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
class Query
|
4
|
-
def initialize(model, options={})
|
5
|
-
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
|
6
|
-
@model, @options, @conditions = model, {}, {}
|
7
|
-
query.update(options)
|
8
|
-
add_sci_condition
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
def method_missing(method, *args, &block)
|
13
|
-
query.send(method, *args, &block)
|
14
|
-
end
|
15
|
-
|
16
|
-
def query
|
17
|
-
if @model.enslave?
|
18
|
-
@query ||= Plucky::Query.new(@model.slave_collection).object_ids(@model.object_id_keys)
|
19
|
-
else
|
20
|
-
@query ||= Plucky::Query.new(@model.collection).object_ids(@model.object_id_keys)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def add_sci_condition
|
25
|
-
query[:_type] = @model.to_s if @model.single_collection_inherited?
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
class Query
|
4
|
+
def initialize(model, options={})
|
5
|
+
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
|
6
|
+
@model, @options, @conditions = model, {}, {}
|
7
|
+
query.update(options)
|
8
|
+
add_sci_condition
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
query.send(method, *args, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def query
|
17
|
+
if @model.enslave?
|
18
|
+
@query ||= Plucky::Query.new(@model.slave_collection).object_ids(@model.object_id_keys)
|
19
|
+
else
|
20
|
+
@query ||= Plucky::Query.new(@model.collection).object_ids(@model.object_id_keys)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_sci_condition
|
25
|
+
query[:_type] = @model.to_s if @model.single_collection_inherited?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/mongo_mapper/support.rb
CHANGED
@@ -1,197 +1,197 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
class Array
|
3
|
-
def self.to_mongo(value)
|
4
|
-
value = value.respond_to?(:lines) ? value.lines : value
|
5
|
-
value.to_a
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.from_mongo(value)
|
9
|
-
value || []
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Binary
|
14
|
-
def self.to_mongo(value)
|
15
|
-
if value.is_a?(BSON::Binary)
|
16
|
-
value
|
17
|
-
else
|
18
|
-
value.nil? ? nil : BSON::Binary.new(value)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.from_mongo(value)
|
23
|
-
value
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class Boolean
|
28
|
-
BOOLEAN_MAPPING = {
|
29
|
-
true => true, 'true' => true, 'TRUE' => true, 'True' => true, 't' => true, 'T' => true, '1' => true, 1 => true, 1.0 => true,
|
30
|
-
false => false, 'false' => false, 'FALSE' => false, 'False' => false, 'f' => false, 'F' => false, '0' => false, 0 => false, 0.0 => false, nil => nil
|
31
|
-
}
|
32
|
-
|
33
|
-
def self.to_mongo(value)
|
34
|
-
if value.is_a?(Boolean)
|
35
|
-
value
|
36
|
-
else
|
37
|
-
BOOLEAN_MAPPING[value]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.from_mongo(value)
|
42
|
-
!!value
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class Date
|
47
|
-
def self.to_mongo(value)
|
48
|
-
if value.nil? || value == ''
|
49
|
-
nil
|
50
|
-
else
|
51
|
-
date = value.is_a?(Date) || value.is_a?(Time) ? value : Date.parse(value.to_s)
|
52
|
-
Time.utc(date.year, date.month, date.day)
|
53
|
-
end
|
54
|
-
rescue
|
55
|
-
nil
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.from_mongo(value)
|
59
|
-
value.to_date if value.present?
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
class Float
|
64
|
-
def self.to_mongo(value)
|
65
|
-
value.nil? ? nil : value.to_f
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
class Hash
|
70
|
-
def self.from_mongo(value)
|
71
|
-
HashWithIndifferentAccess.new(value || {})
|
72
|
-
end
|
73
|
-
|
74
|
-
def to_mongo
|
75
|
-
self
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
class Integer
|
80
|
-
def self.to_mongo(value)
|
81
|
-
value_to_i = value.to_i
|
82
|
-
if value_to_i == 0 && value != value_to_i
|
83
|
-
value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
|
84
|
-
else
|
85
|
-
value_to_i
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
class NilClass
|
91
|
-
def to_mongo(value)
|
92
|
-
value
|
93
|
-
end
|
94
|
-
|
95
|
-
def from_mongo(value)
|
96
|
-
value
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
class Object
|
101
|
-
# The hidden singleton lurks behind everyone
|
102
|
-
def metaclass
|
103
|
-
class << self; self end
|
104
|
-
end
|
105
|
-
|
106
|
-
def meta_eval(&blk)
|
107
|
-
metaclass.instance_eval(&blk)
|
108
|
-
end
|
109
|
-
|
110
|
-
# Adds methods to a metaclass
|
111
|
-
def meta_def(name, &blk)
|
112
|
-
meta_eval { define_method(name, &blk) }
|
113
|
-
end
|
114
|
-
|
115
|
-
# Defines an instance method within a class
|
116
|
-
def class_def(name, &blk)
|
117
|
-
class_eval { define_method(name, &blk) }
|
118
|
-
end
|
119
|
-
|
120
|
-
def self.to_mongo(value)
|
121
|
-
value
|
122
|
-
end
|
123
|
-
|
124
|
-
def self.from_mongo(value)
|
125
|
-
value
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
class ObjectId
|
130
|
-
def self.to_mongo(value)
|
131
|
-
Plucky.to_object_id(value)
|
132
|
-
end
|
133
|
-
|
134
|
-
def self.from_mongo(value)
|
135
|
-
value
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
class Set
|
140
|
-
def self.to_mongo(value)
|
141
|
-
value.to_a
|
142
|
-
end
|
143
|
-
|
144
|
-
def self.from_mongo(value)
|
145
|
-
Set.new(value || [])
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
class String
|
150
|
-
def self.to_mongo(value)
|
151
|
-
value.nil? ? nil : value.to_s
|
152
|
-
end
|
153
|
-
|
154
|
-
def self.from_mongo(value)
|
155
|
-
value.nil? ? nil : value.to_s
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
class Time
|
160
|
-
def self.to_mongo(value)
|
161
|
-
if value.nil? || value == ''
|
162
|
-
nil
|
163
|
-
else
|
164
|
-
time_class = Time.try(:zone).present? ? Time.zone : Time
|
165
|
-
time = value.is_a?(Time) ? value : time_class.parse(value.to_s)
|
166
|
-
# strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
|
167
|
-
at(time.to_i).utc if time
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def self.from_mongo(value)
|
172
|
-
if Time.try(:zone).present? && value.present?
|
173
|
-
value.in_time_zone(Time.zone)
|
174
|
-
else
|
175
|
-
value
|
176
|
-
end
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
class BSON::ObjectID
|
181
|
-
alias_method :original_to_json, :to_json
|
182
|
-
|
183
|
-
def as_json(options=nil)
|
184
|
-
to_s
|
185
|
-
end
|
186
|
-
|
187
|
-
def to_json(options = nil)
|
188
|
-
as_json.to_json
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
module MongoMapper
|
193
|
-
module Support
|
194
|
-
autoload :DescendantAppends, 'mongo_mapper/support/descendant_appends'
|
195
|
-
autoload :Find, 'mongo_mapper/support/find'
|
196
|
-
end
|
197
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
class Array
|
3
|
+
def self.to_mongo(value)
|
4
|
+
value = value.respond_to?(:lines) ? value.lines : value
|
5
|
+
value.to_a
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.from_mongo(value)
|
9
|
+
value || []
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Binary
|
14
|
+
def self.to_mongo(value)
|
15
|
+
if value.is_a?(BSON::Binary)
|
16
|
+
value
|
17
|
+
else
|
18
|
+
value.nil? ? nil : BSON::Binary.new(value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.from_mongo(value)
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Boolean
|
28
|
+
BOOLEAN_MAPPING = {
|
29
|
+
true => true, 'true' => true, 'TRUE' => true, 'True' => true, 't' => true, 'T' => true, '1' => true, 1 => true, 1.0 => true,
|
30
|
+
false => false, 'false' => false, 'FALSE' => false, 'False' => false, 'f' => false, 'F' => false, '0' => false, 0 => false, 0.0 => false, nil => nil
|
31
|
+
}
|
32
|
+
|
33
|
+
def self.to_mongo(value)
|
34
|
+
if value.is_a?(Boolean)
|
35
|
+
value
|
36
|
+
else
|
37
|
+
BOOLEAN_MAPPING[value]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.from_mongo(value)
|
42
|
+
!!value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Date
|
47
|
+
def self.to_mongo(value)
|
48
|
+
if value.nil? || value == ''
|
49
|
+
nil
|
50
|
+
else
|
51
|
+
date = value.is_a?(Date) || value.is_a?(Time) ? value : Date.parse(value.to_s)
|
52
|
+
Time.utc(date.year, date.month, date.day)
|
53
|
+
end
|
54
|
+
rescue
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.from_mongo(value)
|
59
|
+
value.to_date if value.present?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Float
|
64
|
+
def self.to_mongo(value)
|
65
|
+
value.nil? ? nil : value.to_f
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Hash
|
70
|
+
def self.from_mongo(value)
|
71
|
+
HashWithIndifferentAccess.new(value || {})
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_mongo
|
75
|
+
self
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Integer
|
80
|
+
def self.to_mongo(value)
|
81
|
+
value_to_i = value.to_i
|
82
|
+
if value_to_i == 0 && value != value_to_i
|
83
|
+
value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
|
84
|
+
else
|
85
|
+
value_to_i
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class NilClass
|
91
|
+
def to_mongo(value)
|
92
|
+
value
|
93
|
+
end
|
94
|
+
|
95
|
+
def from_mongo(value)
|
96
|
+
value
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Object
|
101
|
+
# The hidden singleton lurks behind everyone
|
102
|
+
def metaclass
|
103
|
+
class << self; self end
|
104
|
+
end
|
105
|
+
|
106
|
+
def meta_eval(&blk)
|
107
|
+
metaclass.instance_eval(&blk)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Adds methods to a metaclass
|
111
|
+
def meta_def(name, &blk)
|
112
|
+
meta_eval { define_method(name, &blk) }
|
113
|
+
end
|
114
|
+
|
115
|
+
# Defines an instance method within a class
|
116
|
+
def class_def(name, &blk)
|
117
|
+
class_eval { define_method(name, &blk) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.to_mongo(value)
|
121
|
+
value
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.from_mongo(value)
|
125
|
+
value
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class ObjectId
|
130
|
+
def self.to_mongo(value)
|
131
|
+
Plucky.to_object_id(value)
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.from_mongo(value)
|
135
|
+
value
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Set
|
140
|
+
def self.to_mongo(value)
|
141
|
+
value.to_a
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.from_mongo(value)
|
145
|
+
Set.new(value || [])
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
class String
|
150
|
+
def self.to_mongo(value)
|
151
|
+
value.nil? ? nil : value.to_s
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.from_mongo(value)
|
155
|
+
value.nil? ? nil : value.to_s
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
class Time
|
160
|
+
def self.to_mongo(value)
|
161
|
+
if value.nil? || value == ''
|
162
|
+
nil
|
163
|
+
else
|
164
|
+
time_class = Time.try(:zone).present? ? Time.zone : Time
|
165
|
+
time = value.is_a?(Time) ? value : time_class.parse(value.to_s)
|
166
|
+
# strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
|
167
|
+
at(time.to_i).utc if time
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.from_mongo(value)
|
172
|
+
if Time.try(:zone).present? && value.present?
|
173
|
+
value.in_time_zone(Time.zone)
|
174
|
+
else
|
175
|
+
value
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class BSON::ObjectID
|
181
|
+
alias_method :original_to_json, :to_json
|
182
|
+
|
183
|
+
def as_json(options=nil)
|
184
|
+
to_s
|
185
|
+
end
|
186
|
+
|
187
|
+
def to_json(options = nil)
|
188
|
+
as_json.to_json
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
module MongoMapper
|
193
|
+
module Support
|
194
|
+
autoload :DescendantAppends, 'mongo_mapper/support/descendant_appends'
|
195
|
+
autoload :Find, 'mongo_mapper/support/find'
|
196
|
+
end
|
197
|
+
end
|