Browse files

Added generation/install functionality and specs

  • Loading branch information...
1 parent 78ef221 commit c94368611fe0b0b286946eff99c5250caaace2b2 @hpoydar hpoydar committed May 4, 2009
View
40 README.md
@@ -2,22 +2,52 @@
A Rails plugin for connecting to and working with a [CouchDB](http://couchdb.apache.org) document-oriented database via the [CouchRest](http://github.com/jchris/couchrest) RESTful CouchDB client.
-# Usage
+Specifically, this plugin provides the following utilities:
-TODO
+* Initializer for use with a couchdb.yml configuration file
+* CouchDB specific rake tasks
+
+Important: This plugin assumes your application only uses one CouchDB database.
+
+## Requirements
+
+* [CouchRest gem](http://github.com/jchris/couchrest)
+* JSON gem
+* [RSpec](http://github.com/dchelimsky/rspec) BDD framework (optional - for running plugin specs)
+* [RSpec-Rails](http://github.com/dchelimsky/rspec-rails) library (optional - for running plugin specs)
+
+## Installation and usage
+
+Install with the native Rails plugin installation script:
+
+ script/plugin install
+
+Or simply add to vendor/plugins and generate the files you need:
+
+ script/generate couchrest_rails relax
+
+## Further development and testing
+
+To run the test suite, you'll need rspec installed with rspec-rails library enabled for the host application. You can run the tests in the following way:
+
+ <rails_root>$ rake spec:plugins
+ <plugin_root>$ rake spec
+ <plugin_root>$ autospec
## TODO
-* generate files via install.rb and generate method
-* couch.yml setup
* rake tasks - view syncs
* rake tasks - test setup/teardown
* rake tasks - fixtures
* CouchDocument model for extending
** timestamp hooks
** basic views?
-* document usage
+* document usages
+* restrict model to default attributes and their types?
+* test views?
* gemify
+* Add more parseable options to couchdb.yml
+* Expand beyond a single database per applicatipn
## License
View
28 Rakefile
@@ -1,23 +1,11 @@
require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
+require 'spec/rake/spectask'
-desc 'Default: run unit tests.'
-task :default => :test
+desc 'Default: run specs.'
+task :default => :spec
-desc 'Test the couchrest_rails plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-desc 'Generate documentation for the couchrest_rails plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'Couchrest-rails'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
View
1 generators/couchrest_rails/couchrest_rails_generator.rb
@@ -1,6 +1,7 @@
class CouchrestRailsGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
+ m.template "couchdb.yml", "config/couchdb.yml"
m.template "couchdb_initializer.rb", "config/initializers/couchdb.rb"
end
end
View
12 generators/couchrest_rails/templates/couchdb.yml
@@ -0,0 +1,12 @@
+environment_database: &environment_database
+ database: <%%= RAILS_ROOT.sub(/\A.*\/([^\/]+)\/?\z/, '\1') %>_<%%= RAILS_ENV %>
+
+development:
+ host: localhost
+ port: 5984
+ <<: *environment_database
+
+test:
+ host: localhost
+ port: 5984
+ <<: *environment_database
View
47 generators/couchrest_rails/templates/couchdb_initializer.rb
@@ -1,14 +1,33 @@
-couchdb_config = YAML.load_file(File.join(Rails.root, %w[config couchdb.yml]))
-
-ENV['RAILS_ENV'] = "development" if ENV['RAILS_ENV'] == nil
-
-host = couchdb_config[ENV['RAILS_ENV']]["host"]
-port = couchdb_config[ENV['RAILS_ENV']]["port"]
-database = couchdb_config[ENV['RAILS_ENV']]["database"]
-
-host = "localhost" if host == nil
-port = "5984" if port == nil
-
-Rails.logger.error("No database specified in config/couchdb.yml") if database == nil
-
-COUCHDB_SERVER = {:host => host, :port => port, :database => database}
+begin
+
+ couchdb_config = YAML::load(ERB.new(IO.read(RAILS_ROOT + "/config/couchdb.yml")).result)[ENV['RAILS_ENV']]
+
+ host = couchdb_config["host"] || 'localhost'
+ port = couchdb_config["port"] || '5984'
+ database = couchdb_config["database"]
+ username = couchdb_config["username"]
+ password = couchdb_config["password"]
+ ssl = couchdb_config["ssl"] || false
+
+ host = "localhost" if host == nil
+ port = "5984" if port == nil
+ ssl = false if ssl == nil
+
+ protocol = ssl ? 'https' : 'http'
+ authorized_host = (username.blank? && password.blank?) ? host : "#{CGI.escape(username)}:#{CGI.escape(password)}@#{host}"
+
+ raise unless database
+
+rescue
+
+ raise "There was a problem with your config/couchdb.yml file. Check and make sure it's present and the syntax is correct."
+
+else
+
+ COUCHDB_SERVER = {
+ :host => "#{protocol}://#{authorized_host}:#{port}",
+ :database => database,
+ :instance => "#{protocol}://#{authorized_host}:#{port}/#{database}"
+ }
+
+end
View
3 init.rb
@@ -1 +1,2 @@
-config.gem 'couchrest'
+config.gem 'couchrest'
+config.gem 'json'
View
4 install.rb
@@ -1 +1,3 @@
-# Install hook code here
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+Rails::Generator::Scripts::Generate.new.run(['couchrest_rails', 'relax'], :destination => RAILS_ROOT)
View
5 lib/autotest/discover.rb
@@ -0,0 +1,5 @@
+$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
+
+Autotest.add_discovery do
+ "rspec"
+end
View
28 spec/lib/couchrest_rails_spec.rb
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+
+describe 'CouchrestRails' do
+
+ describe 'installation' do
+
+ before :all do
+ @fake_rails_root = File.join(File.dirname(__FILE__), 'rails_root')
+ FileUtils.mkdir_p(@fake_rails_root)
+ FileUtils.mkdir_p("#{@fake_rails_root}/config/initializers")
+ end
+
+ after :all do
+ FileUtils.rm_rf(@fake_rails_root)
+ end
+
+ it "should generate the necessary files in the host application" do
+ Rails::Generator::Scripts::Generate.new.run(
+ ['couchrest_rails', 'relax'], :destination => @fake_rails_root)
+ Dir.glob(File.join(@fake_rails_root, "**", "*.*")).map {|f| File.basename(f)}.should ==
+ ['couchdb.yml', 'couchdb.rb']
+ end
+
+ end
+
+end
View
6 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+begin
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
+rescue LoadError
+ puts "You need to install rspec in your base application"
+ exit
+end
View
52 spec/tasks/couchrest_rails_tasks.rake_spec.rb
@@ -0,0 +1,52 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'net/http'
+require 'json'
+
+describe 'rake tasks' do
+
+ before :each do
+ CouchRest.new("#{COUCHDB_SERVER[:host]}").database("#{COUCHDB_SERVER[:database]}").delete! rescue nil
+ end
+
+ after :all do
+ CouchRest.new("#{COUCHDB_SERVER[:host]}").database("#{COUCHDB_SERVER[:database]}").delete! rescue nil
+ end
+
+ describe 'couchdb:create' do
+
+ it 'should create a couchdb database for the current environment' do
+ `cd #{RAILS_ROOT}; RAILS_ENV=#{ENV['RAILS_ENV']} rake couchdb:create`
+ res = CouchRest.get(COUCHDB_SERVER[:instance])
+ res['db_name'].should == COUCHDB_SERVER[:database]
+ end
+
+ it 'should do nothing and display a message if the database already exists' do
+ CouchRest.database!("#{COUCHDB_SERVER[:instance]}")
+ res = `cd #{RAILS_ROOT}; RAILS_ENV=#{ENV['RAILS_ENV']} rake couchdb:create`
+ res.should =~ /already exists/i
+ end
+
+ end
+
+ describe 'couchdb:drop' do
+
+ it 'should delete the couchdb database' do
+ CouchRest.database!("#{COUCHDB_SERVER[:instance]}")
+ `cd #{RAILS_ROOT}; RAILS_ENV=#{ENV['RAILS_ENV']} rake couchdb:drop`
+ res = CouchRest.get(COUCHDB_SERVER[:instance]) rescue nil
+ res.should be_nil
+ end
+
+ end
+
+ describe 'couchdb:reset' do
+
+ end
+
+ describe 'couchdb:test:reset' do
+
+ end
+
+end
+
+
View
14 tasks/couchrest_rails_tasks.rake
@@ -1,15 +1,21 @@
+require 'json'
+
namespace :couchdb do
desc "Create the CouchDB database defined in config/couchdb.yml for the current environment"
task :create => :environment do
- CouchRest.database!("http://#{COUCHDB_SERVER[:host]}:#{COUCHDB_SERVER[:port]}/#{COUCHDB_SERVER[:database]}")
+ res = CouchRest.get(COUCHDB_SERVER[:instance]) rescue nil
+ if res && res['db_name'] && res['db_name'] == COUCHDB_SERVER[:database]
+ puts "The CouchDB database '#{COUCHDB_SERVER[:database]}' already exists"
+ else
+ CouchRest.database!(COUCHDB_SERVER[:instance])
+ end
end
-
desc "Drops the couchdb database for the current RAILS_ENV"
task :drop => :environment do
- cr = CouchRest.new("http://#{COUCHDB_SERVER[:host]}:#{COUCHDB_SERVER[:port]}")
- db = cr.database("#{COUCHDB_SERVER[:database]}")
+ cr = CouchRest.new(COUCHDB_SERVER[:host])
+ db = cr.database(COUCHDB_SERVER[:database])
db.delete! rescue nil
end

0 comments on commit c943686

Please sign in to comment.