Permalink
Browse files

installing rspec

  • Loading branch information...
1 parent 3535185 commit 1aa69b160b920f28ed118880bd50cf80fa35121c @robertomiranda committed Sep 27, 2012
Showing with 79 additions and 20 deletions.
  1. +1 −0 .rspec
  2. +14 −0 Rakefile
  3. +1 −2 lib/mini_mongo.rb
  4. +14 −13 lib/mini_mongo/base.rb
  5. +15 −4 lib/mini_mongo/connection.rb
  6. +1 −1 lib/mini_mongo/version.rb
  7. +3 −0 mini_mongo.gemspec
  8. +10 −0 spec/document_spec.rb
  9. +4 −0 spec/models/post.rb
  10. +16 −0 spec/spec_helper.rb
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
@@ -1,8 +1,22 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
Bundler::GemHelper.install_tasks
task :console do
puts "Loading development console..."
system("irb -r mini_mongo")
end
+
+task :help do
+ puts "Available rake tasks: "
+ puts "rake console - Run a IRB console with all enviroment loaded"
+ puts "rake spec - Run specs"
+end
+
+task :spec do
+ desc "Run all specs with rcov"
+ RSpec::Core::RakeTask.new(:spec) do |t|
+ t.pattern = "spec/**/*_spec.rb"
+ end
+end
View
@@ -1,5 +1,4 @@
-require "active_support"
-require 'active_support/core_ext'
+require 'active_support/all'
module MiniMongo
autoload :Connection, 'mini_mongo/connection'
autoload :MiniMongoError, 'mini_mongo/errors'
View
@@ -1,18 +1,19 @@
require "json"
-class MiniMongo::Base
- include MiniMongo::Mapper
+module MiniMongo
+ class Base
+ include MiniMongo::Mapper
- def initialize(attrs = {})
- attrs["id"] = attrs["_id"].to_s
- attrs.delete("_id")
- attrs.delete(:_id)
- attrs.each do |key, value|
- instance_variable_set("@#{key}", value)
- self.instance_eval("def #{key};@#{key};end")
- self.instance_eval("def #{key}=(val);@#{key}=val;end")
+ def initialize(attrs = {})
+ attrs["id"] = attrs["_id"].to_s
+ attrs.delete("_id")
+ attrs.delete(:_id)
+ attrs.each do |key, value|
+ instance_variable_set("@#{key}", value)
+ self.instance_eval("def #{key};@#{key};end")
+ self.instance_eval("def #{key}=(val);@#{key}=val;end")
+ end
+ self.instance_eval("def to_json;#{attrs}.to_json;end")
+ self.instance_eval("def to_h;#{attrs};end")
end
- self.instance_eval("def to_json;#{attrs}.to_json;end")
- self.instance_eval("def to_h;#{attrs};end")
end
-
end
@@ -2,6 +2,7 @@
require "mongo"
module MiniMongo
module Connection
+
@@slave_ok = false
def slave_ok=(attr)
@@ -10,14 +11,24 @@ def slave_ok=(attr)
def database_url=(url)
@@database_url = url
+ @@db_info = URI.parse(@@database_url)
+ @@db_name = @@db_info.path.gsub(/^\//, '')
+ @@db_user = @@db_info.user
+ @@db_password = @@db_info.password
+ end
+
+ def db_name
+ @@db_name
end
def db_connection
- db = URI.parse(@@database_url)
- db_name = db.path.gsub(/^\//, '')
- @@db_connection ||= Mongo::Connection.new(db.host, db.port, :slave_ok => @@slave_ok).db(db_name)
- @@db_connection.authenticate(db.user, db.password) if db.user && !db.user.empty?
+ @@db_connection ||= Mongo::Connection.new(@@db_info.host, @@db_info.port, :slave_ok => @@slave_ok).db(@@db_name)
+ @@db_connection.authenticate(@@db_info.user, @@db_info.password) if @@db_info.user && !@@db_info.user.empty?
@@db_connection
end
+
+ def drop
+ self.db_connection.drop_database(@@db_name)
+ end
end
end
@@ -1,3 +1,3 @@
module MiniMongo
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
@@ -18,4 +18,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "mongo", "~> 1.6.4"
gem.add_dependency "bson_ext", "~> 1.6.4"
gem.add_dependency "activesupport", "~> 3.2.7"
+ gem.add_development_dependency "simplecov"
+ gem.add_development_dependency 'rspec'
+ gem.add_development_dependency "rake"
end
View
@@ -0,0 +1,10 @@
+require "spec_helper"
+describe "Document" do
+ def post_attributes
+ {}
+ end
+
+ it "should create a new Document" do
+ post = Post.insert({:author => "Chuck Norris"})
+ end
+end
View
@@ -0,0 +1,4 @@
+class Post < MiniMongo::Base
+ maps :posts
+end
+
View
@@ -0,0 +1,16 @@
+$: << File.dirname(__FILE__)
+ENV['RACK_ENV'] = "test"
+require 'simplecov'
+SimpleCov.start
+
+require 'bundler/setup'
+Dir["./lib/*.rb"].each {|file| require file }
+MiniMongo.database_url = "mongodb://:@localhost:27017/posts"
+Dir["./spec/models/*.rb"].each {|file| require file }
+
+RSpec.configure do |config|
+
+ config.before(:each) do
+ Post.remove_all
+ end
+end

0 comments on commit 1aa69b1

Please sign in to comment.