mongodb_model 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mongo/model.rb CHANGED
@@ -43,7 +43,6 @@ module Mongo
43
43
  end
44
44
 
45
45
  Mongo.defaults.merge! \
46
- symbolize: true,
47
46
  convert_underscore_to_dollar: true,
48
47
  batch_size: 50,
49
48
  multi: true,
@@ -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 #{attributes.inspect}!")
38
+ model.save || raise(Mongo::Error, "can't create #{self} #{model.errors.inspect}!")
39
39
  model
40
40
  end
41
41
 
@@ -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
- before_save :update_timestamps
48
+ before_create :update_created_at
49
+ before_save :update_updated_at
46
50
  end
47
51
  end
48
52
  end
@@ -10,7 +10,11 @@ class Mongo::Model::Query < Object
10
10
  end
11
11
 
12
12
  def merge query
13
- raise "can't merge queries with different models!" unless model_class == query.model_class
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
 
@@ -1,4 +1,9 @@
1
1
  module Mongo::Model::QueryMixin
2
+ def exists? options = {}
3
+ self.class.count({_id: _id}, options) > 0
4
+ end
5
+ alias :exist? :exists?
6
+
2
7
  module ClassMethods
3
8
  include Mongo::DynamicFinders
4
9
 
@@ -1,7 +1,7 @@
1
1
  module Mongo::Model::Scope
2
2
  module ClassMethods
3
3
  def current_scope
4
- scope, exclusive = Thread.current[:mongo_model_scope]
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[:mongo_model_scope]
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[:mongo_model_scope] = [scope, exclusive]
31
+ Thread.current[scope_identifier] = [scope, exclusive]
32
32
  return block.call
33
33
  ensure
34
- Thread.current[:mongo_model_scope] = [previous_scope, false]
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: 5, units: 10}),
122
- Player::Mission.build(name: 'Backwater Station', stats: {buildings: 8, units: 25}),
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[:units] = 9
140
- @player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {buildings: 11, units: 40})
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.9
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-03 00:00:00.000000000Z
12
+ date: 2011-09-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n