mongo_mapper 0.6.0 → 0.6.1
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/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/mongo_mapper/document.rb +0 -9
- data/lib/mongo_mapper/embedded_document.rb +12 -3
- data/lib/mongo_mapper/finder_options.rb +5 -9
- data/lib/mongo_mapper/serializers/json_serializer.rb +2 -2
- data/mongo_mapper.gemspec +5 -5
- data/test/functional/test_embedded_document.rb +19 -0
- data/test/models.rb +17 -17
- metadata +3 -3
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ Jeweler::Tasks.new do |gem|
|
|
12
12
|
gem.authors = ["John Nunemaker"]
|
13
13
|
|
14
14
|
gem.add_dependency('activesupport', '>= 2.3')
|
15
|
-
gem.add_dependency('mongo', '0.
|
15
|
+
gem.add_dependency('mongo', '0.17.1')
|
16
16
|
gem.add_dependency('jnunemaker-validatable', '1.8.1')
|
17
17
|
|
18
18
|
gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.1
|
@@ -344,15 +344,6 @@ module MongoMapper
|
|
344
344
|
instances.size == 1 ? instances[0] : instances
|
345
345
|
end
|
346
346
|
|
347
|
-
def initialize_doc(doc)
|
348
|
-
begin
|
349
|
-
klass = doc['_type'].present? ? doc['_type'].constantize : self
|
350
|
-
klass.new(doc)
|
351
|
-
rescue NameError
|
352
|
-
new(doc)
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
347
|
def find_every(options)
|
357
348
|
criteria, options = to_finder_options(options)
|
358
349
|
collection.find(criteria, options).to_a.map do |doc|
|
@@ -87,11 +87,20 @@ module MongoMapper
|
|
87
87
|
if instance_or_hash.is_a?(self)
|
88
88
|
instance_or_hash
|
89
89
|
else
|
90
|
-
|
90
|
+
initialize_doc(instance_or_hash)
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
private
|
95
|
+
def initialize_doc(doc)
|
96
|
+
begin
|
97
|
+
klass = doc['_type'].present? ? doc['_type'].constantize : self
|
98
|
+
klass.new(doc)
|
99
|
+
rescue NameError
|
100
|
+
new(doc)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
95
104
|
def accessors_module
|
96
105
|
module_defined = if method(:const_defined?).arity == 1 # Ruby 1.9 compat check
|
97
106
|
const_defined?('MongoMapperKeys')
|
@@ -195,7 +204,7 @@ module MongoMapper
|
|
195
204
|
end
|
196
205
|
end
|
197
206
|
|
198
|
-
if self.class.embeddable?
|
207
|
+
if self.class.embeddable?
|
199
208
|
if read_attribute(:_id).blank?
|
200
209
|
write_attribute :_id, Mongo::ObjectID.new
|
201
210
|
@new_document = true
|
@@ -362,7 +371,7 @@ module MongoMapper
|
|
362
371
|
def write_attribute(name, value)
|
363
372
|
key = _keys[name]
|
364
373
|
instance_variable_set "@#{name}_before_typecast", value
|
365
|
-
instance_variable_set "@#{name}", key.set(value)
|
374
|
+
instance_variable_set "@#{name}", key.set(value)
|
366
375
|
end
|
367
376
|
|
368
377
|
def embedded_associations
|
@@ -14,8 +14,7 @@ module MongoMapper
|
|
14
14
|
|
15
15
|
def initialize(model, options)
|
16
16
|
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
|
17
|
-
options = options.
|
18
|
-
options.symbolize_keys!
|
17
|
+
options = options.symbolize_keys
|
19
18
|
|
20
19
|
@model = model
|
21
20
|
@options = {}
|
@@ -41,12 +40,10 @@ module MongoMapper
|
|
41
40
|
|
42
41
|
# @return [Hash] Mongo compatible options
|
43
42
|
def options
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
limit = options.delete(:limit) || 0
|
49
|
-
sort = options.delete(:sort) || convert_order_to_sort(options.delete(:order))
|
43
|
+
fields = @options.delete(:fields) || @options.delete(:select)
|
44
|
+
skip = @options.delete(:skip) || @options.delete(:offset) || 0
|
45
|
+
limit = @options.delete(:limit) || 0
|
46
|
+
sort = @options.delete(:sort) || convert_order_to_sort(@options.delete(:order))
|
50
47
|
|
51
48
|
{:fields => to_mongo_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
|
52
49
|
end
|
@@ -74,7 +71,6 @@ module MongoMapper
|
|
74
71
|
|
75
72
|
case value
|
76
73
|
when Array
|
77
|
-
operator_present = field.to_s =~ /^\$/
|
78
74
|
criteria[field] = operator?(field) ? value : {'$in' => value}
|
79
75
|
when Hash
|
80
76
|
criteria[field] = to_mongo_criteria(value, field)
|
@@ -51,7 +51,7 @@ module MongoMapper #:nodoc:
|
|
51
51
|
# "permalink": "1-konata-izumi"}
|
52
52
|
def to_json(options={})
|
53
53
|
apply_to_json_defaults(options)
|
54
|
-
|
54
|
+
|
55
55
|
if include_root_in_json
|
56
56
|
"{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
|
57
57
|
else
|
@@ -75,7 +75,7 @@ module MongoMapper #:nodoc:
|
|
75
75
|
@json_class_name ||= name.demodulize.underscore.inspect
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
private
|
80
80
|
def apply_to_json_defaults(options)
|
81
81
|
unless options[:only]
|
data/mongo_mapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_mapper}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-17}
|
13
13
|
s.default_executable = %q{mmconsole}
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
15
15
|
s.executables = ["mmconsole"]
|
@@ -144,7 +144,7 @@ Gem::Specification.new do |s|
|
|
144
144
|
|
145
145
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
146
146
|
s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
|
147
|
-
s.add_runtime_dependency(%q<mongo>, ["= 0.
|
147
|
+
s.add_runtime_dependency(%q<mongo>, ["= 0.17.1"])
|
148
148
|
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
|
149
149
|
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
150
150
|
s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
|
@@ -152,7 +152,7 @@ Gem::Specification.new do |s|
|
|
152
152
|
s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
|
153
153
|
else
|
154
154
|
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
155
|
-
s.add_dependency(%q<mongo>, ["= 0.
|
155
|
+
s.add_dependency(%q<mongo>, ["= 0.17.1"])
|
156
156
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
|
157
157
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
158
158
|
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
@@ -161,7 +161,7 @@ Gem::Specification.new do |s|
|
|
161
161
|
end
|
162
162
|
else
|
163
163
|
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
164
|
-
s.add_dependency(%q<mongo>, ["= 0.
|
164
|
+
s.add_dependency(%q<mongo>, ["= 0.17.1"])
|
165
165
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
|
166
166
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
167
167
|
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
@@ -34,6 +34,25 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
context "Instantiating single collection inherited embedded documents" do
|
38
|
+
setup do
|
39
|
+
@document = Class.new do
|
40
|
+
include MongoMapper::Document
|
41
|
+
key :message, Message
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "work" do
|
46
|
+
doc1 = @document.create(:message => Enter.new)
|
47
|
+
doc2 = @document.create(:message => Exit.new)
|
48
|
+
doc3 = @document.create(:message => Chat.new)
|
49
|
+
|
50
|
+
doc1.reload.message.class.should be(Enter)
|
51
|
+
doc2.reload.message.class.should be(Exit)
|
52
|
+
doc3.reload.message.class.should be(Chat)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
37
56
|
context "new?" do
|
38
57
|
setup do
|
39
58
|
@document.class_eval do
|
data/test/models.rb
CHANGED
@@ -23,6 +23,12 @@ class WindowSize
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
module AccountsExtensions
|
27
|
+
def inactive
|
28
|
+
all(:last_logged_in => nil)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
26
32
|
class Post
|
27
33
|
include MongoMapper::Document
|
28
34
|
|
@@ -71,10 +77,18 @@ class Enter < Message; end
|
|
71
77
|
class Exit < Message; end
|
72
78
|
class Chat < Message; end
|
73
79
|
|
74
|
-
|
75
|
-
|
76
|
-
|
80
|
+
class Room
|
81
|
+
include MongoMapper::Document
|
82
|
+
|
83
|
+
key :name, String
|
84
|
+
many :messages, :polymorphic => true, :order => 'position' do
|
85
|
+
def older
|
86
|
+
all(:position => {'$gt' => 5})
|
87
|
+
end
|
77
88
|
end
|
89
|
+
many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
|
90
|
+
|
91
|
+
many :accounts, :polymorphic => true, :extend => AccountsExtensions
|
78
92
|
end
|
79
93
|
|
80
94
|
class Account
|
@@ -89,20 +103,6 @@ end
|
|
89
103
|
class User < Account; end
|
90
104
|
class Bot < Account; end
|
91
105
|
|
92
|
-
class Room
|
93
|
-
include MongoMapper::Document
|
94
|
-
|
95
|
-
key :name, String
|
96
|
-
many :messages, :polymorphic => true do
|
97
|
-
def older
|
98
|
-
all(:position => {'$gt' => 5})
|
99
|
-
end
|
100
|
-
end
|
101
|
-
many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
|
102
|
-
|
103
|
-
many :accounts, :polymorphic => true, :extend => AccountsExtensions
|
104
|
-
end
|
105
|
-
|
106
106
|
class Answer
|
107
107
|
include MongoMapper::Document
|
108
108
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-18 00:00:00 -05:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.17.1
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: jnunemaker-validatable
|