mongodb_model 0.0.9 → 0.0.10
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/lib/mongo/model.rb +0 -1
- data/lib/mongo/model/crud.rb +1 -1
- data/lib/mongo/model/misc.rb +5 -1
- data/lib/mongo/model/query.rb +5 -1
- data/lib/mongo/model/query_mixin.rb +5 -0
- data/lib/mongo/model/scope.rb +9 -4
- data/spec/crud_spec.rb +4 -4
- data/spec/query_spec.rb +9 -0
- metadata +2 -2
data/lib/mongo/model.rb
CHANGED
data/lib/mongo/model/crud.rb
CHANGED
@@ -35,7 +35,7 @@ module Mongo::Model::Crud
|
|
35
35
|
|
36
36
|
def create! attributes = {}, options = {}, &block
|
37
37
|
model = build attributes, options, &block
|
38
|
-
model.save || raise(Mongo::Error, "can't create #{
|
38
|
+
model.save || raise(Mongo::Error, "can't create #{self} #{model.errors.inspect}!")
|
39
39
|
model
|
40
40
|
end
|
41
41
|
|
data/lib/mongo/model/misc.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
module Mongo::Model::Misc
|
2
|
+
def update_created_at; self.created_at = Time.now.utc end
|
3
|
+
def update_updated_at; self.updated_at = Time.now.utc end
|
4
|
+
|
2
5
|
def update_timestamps
|
3
6
|
now = Time.now.utc
|
4
7
|
self.created_at ||= now
|
@@ -42,7 +45,8 @@ module Mongo::Model::Misc
|
|
42
45
|
|
43
46
|
def timestamps!
|
44
47
|
attr_accessor :created_at, :updated_at
|
45
|
-
|
48
|
+
before_create :update_created_at
|
49
|
+
before_save :update_updated_at
|
46
50
|
end
|
47
51
|
end
|
48
52
|
end
|
data/lib/mongo/model/query.rb
CHANGED
@@ -10,7 +10,11 @@ class Mongo::Model::Query < Object
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def merge query
|
13
|
-
|
13
|
+
model_class == query.model_class or
|
14
|
+
model_class.is?(query.model_class) or
|
15
|
+
query.model_class.is?(model_class) or
|
16
|
+
raise("can't merge queries with different models!")
|
17
|
+
|
14
18
|
self.class.new model_class, selector.merge(query.selector), options.merge(query.options)
|
15
19
|
end
|
16
20
|
|
data/lib/mongo/model/scope.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Mongo::Model::Scope
|
2
2
|
module ClassMethods
|
3
3
|
def current_scope
|
4
|
-
scope, exclusive = Thread.current[
|
4
|
+
scope, exclusive = Thread.current[scope_identifier]
|
5
5
|
current = if exclusive
|
6
6
|
scope
|
7
7
|
elsif scope
|
@@ -23,15 +23,15 @@ module Mongo::Model::Scope
|
|
23
23
|
end
|
24
24
|
|
25
25
|
scope = query *args
|
26
|
-
previous_scope, previous_exclusive = Thread.current[
|
26
|
+
previous_scope, previous_exclusive = Thread.current[scope_identifier]
|
27
27
|
raise "exclusive scope already applied!" if previous_exclusive
|
28
28
|
|
29
29
|
begin
|
30
30
|
scope = previous_scope.merge scope if !exclusive and previous_scope
|
31
|
-
Thread.current[
|
31
|
+
Thread.current[scope_identifier] = [scope, exclusive]
|
32
32
|
return block.call
|
33
33
|
ensure
|
34
|
-
Thread.current[
|
34
|
+
Thread.current[scope_identifier] = [previous_scope, false]
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -80,5 +80,10 @@ module Mongo::Model::Scope
|
|
80
80
|
super selector, options, &block
|
81
81
|
end
|
82
82
|
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
def scope_identifier
|
86
|
+
@scope_identifier ||= :"mms_#{self.name}"
|
87
|
+
end
|
83
88
|
end
|
84
89
|
end
|
data/spec/crud_spec.rb
CHANGED
@@ -118,8 +118,8 @@ describe "Model CRUD" do
|
|
118
118
|
@mission_class = Player::Mission
|
119
119
|
@player = Player.new
|
120
120
|
@player.missions = [
|
121
|
-
Player::Mission.build(name: 'Wasteland', stats: {buildings
|
122
|
-
Player::Mission.build(name: 'Backwater Station', stats: {buildings
|
121
|
+
Player::Mission.build(name: 'Wasteland', stats: {'buildings' => 5, 'units' => 10}),
|
122
|
+
Player::Mission.build(name: 'Backwater Station', stats: {'buildings' => 8, 'units' => 25}),
|
123
123
|
]
|
124
124
|
end
|
125
125
|
|
@@ -136,8 +136,8 @@ describe "Model CRUD" do
|
|
136
136
|
Player.first.object_id.should_not == @players.object_id
|
137
137
|
|
138
138
|
# update
|
139
|
-
@player.missions.first.stats[
|
140
|
-
@player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {buildings
|
139
|
+
@player.missions.first.stats['units'] = 9
|
140
|
+
@player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {'buildings' => 11, 'units' => 40})
|
141
141
|
@player.save.should be_true
|
142
142
|
Player.count.should == 1
|
143
143
|
Player.first.should == @player
|
data/spec/query_spec.rb
CHANGED
@@ -65,6 +65,15 @@ describe "Model Query" do
|
|
65
65
|
[u.name, u.age].should == ['Zeratul', 500]
|
66
66
|
end
|
67
67
|
|
68
|
+
it 'exist?' do
|
69
|
+
u = Unit.new
|
70
|
+
u.exist?.should be_false
|
71
|
+
u.save!
|
72
|
+
u.exist?.should be_true
|
73
|
+
u.destroy!
|
74
|
+
u.exist?.should be_false
|
75
|
+
end
|
76
|
+
|
68
77
|
it "build should assign protected attributes" do
|
69
78
|
class SpecialUnit < Unit
|
70
79
|
attr_accessor :age, :status
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|