tinymongo 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,15 +6,15 @@ Simple MongoDB wrapper
6
6
 
7
7
  gem install tinymongo
8
8
 
9
- == Rails Setup
9
+ == Rails Setup (Rails 3)
10
10
 
11
- To create TinyMongo config file (config/tinymongo.yml), do the following:
11
+ To create TinyMongo config file (config/tinymongo.yml) and initializer file (config/initializers/tinymongo.rb), do the following:
12
12
 
13
13
  rails generate tinymongo
14
14
 
15
15
  == Connecting To MongoDB directly (for non-Rails projects)
16
16
 
17
- TinyMongo.configure({:host => 'localhost', :database => 'test'})
17
+ TinyMongo.configure({:host => 'localhost', :database => 'db_name'})
18
18
  TinyMongo.connect
19
19
 
20
20
  == Example
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/init.rb CHANGED
@@ -1,4 +1 @@
1
1
  require 'tinymongo'
2
-
3
- TinyMongo.configure(YAML.load_file(Rails.root + 'config' + 'tinymongo.yml')[Rails.env])
4
- TinyMongo.connect
data/lib/generators/USAGE CHANGED
@@ -6,3 +6,4 @@ Example:
6
6
 
7
7
  This will create:
8
8
  config/tinymongo.yml
9
+ config/initializers/tinymongo.rb
@@ -0,0 +1,2 @@
1
+ TinyMongo.configure(YAML.load_file(Rails.root + 'config' + 'tinymongo.yml')[Rails.env])
2
+ TinyMongo.connect
@@ -3,6 +3,7 @@ class TinymongoGenerator < Rails::Generators::Base
3
3
 
4
4
  def generate_config
5
5
  template "tinymongo.yml.erb", "config/tinymongo.yml"
6
+ copy_file "tinymongo.rb", "config/initializers/tinymongo.rb"
6
7
  end
7
8
 
8
9
  private
@@ -23,6 +23,7 @@ module TinyMongo
23
23
  end
24
24
 
25
25
  def db
26
+ raise 'Not connected to MongoDB. Please connect using TinyMongo.connect().' unless TinyMongo.connected?
26
27
  TinyMongo.db
27
28
  end
28
29
 
@@ -46,10 +47,11 @@ module TinyMongo
46
47
 
47
48
  def find_one(*args)
48
49
  if([BSON::ObjectID, String].include? args[0].class)
49
- self.new(collection.find_one({'_id' => Helper.bson_object_id(args[0])}))
50
+ o = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
50
51
  else
51
- self.new(collection.find_one(*args))
52
+ o = collection.find_one(*args)
52
53
  end
54
+ o ? self.new(o) : nil
53
55
  end
54
56
 
55
57
  def create(hash)
@@ -103,7 +105,16 @@ module TinyMongo
103
105
  end
104
106
 
105
107
  def save
106
- return create_or_update
108
+ if(@_tinymongo_hash['_id'].nil?) # new
109
+ oid = collection.save(@_tinymongo_hash)
110
+ if(oid)
111
+ @_tinymongo_hash.delete(:_id)
112
+ @_tinymongo_hash['_id'] = oid
113
+ end
114
+ else # update
115
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, @_tinymongo_hash, :upsert => true)
116
+ end
117
+ return self
107
118
  end
108
119
 
109
120
  def update_attribute(name, value)
@@ -255,18 +266,5 @@ module TinyMongo
255
266
  collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pullAll' => Helper.hashify_models_in_hash(hash) })
256
267
  end
257
268
  end
258
-
259
- protected
260
- def create_or_update
261
- if(@_tinymongo_hash['_id'].nil?) # new
262
- oid = collection.save(@_tinymongo_hash)
263
- @_tinymongo_hash.delete(:_id)
264
- @_tinymongo_hash['_id'] = oid
265
- else # update
266
- collection.update({ '_id' => @_tinymongo_hash['_id'] }, @_tinymongo_hash)
267
- end
268
- return self
269
- end
270
-
271
269
  end
272
270
  end
data/lib/tinymongo.rb CHANGED
@@ -11,6 +11,7 @@ module TinyMongo
11
11
  @database = config['database'] || 'mongo'
12
12
  @username = config['username']
13
13
  @password = config['password']
14
+ @configured = true
14
15
  end
15
16
 
16
17
  def db
@@ -18,6 +19,8 @@ module TinyMongo
18
19
  end
19
20
 
20
21
  def connect
22
+ raise 'Please do TinyMongo.configure() before attempting to connect.' unless @configured
23
+
21
24
  if defined?(PhusionPassenger) && @connection
22
25
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
23
26
  @connection.connect_to_master if forked
@@ -34,7 +37,11 @@ module TinyMongo
34
37
  end
35
38
  end
36
39
 
37
- true
40
+ @connected = true
41
+ end
42
+
43
+ def connected?
44
+ @connected ? true : false
38
45
  end
39
46
  end
40
47
  end
data/tinymongo.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tinymongo}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Jihoon Kim"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "VERSION",
25
25
  "init.rb",
26
26
  "lib/generators/USAGE",
27
+ "lib/generators/templates/tinymongo.rb",
27
28
  "lib/generators/templates/tinymongo.yml.erb",
28
29
  "lib/generators/tinymongo_generator.rb",
29
30
  "lib/tinymongo.rb",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Peter Jihoon Kim
@@ -60,6 +60,7 @@ files:
60
60
  - VERSION
61
61
  - init.rb
62
62
  - lib/generators/USAGE
63
+ - lib/generators/templates/tinymongo.rb
63
64
  - lib/generators/templates/tinymongo.yml.erb
64
65
  - lib/generators/tinymongo_generator.rb
65
66
  - lib/tinymongo.rb