From 2d9326f8e5d65d1a8e4ceb0de02cbbfa2bd2f082 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Sat, 14 Nov 2009 22:40:15 +0800 Subject: [PATCH 1/3] Add couchrest support --- generators/base_app/config/dependencies.rb.tt | 4 ++ .../components/orms/couchrest_orm_gen.rb | 64 +++++++++++++++++++ generators/skeleton_generator.rb | 2 +- sinatra_more.gemspec | 1 + test/generators/test_skeleton_generator.rb | 8 +++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 generators/components/orms/couchrest_orm_gen.rb diff --git a/generators/base_app/config/dependencies.rb.tt b/generators/base_app/config/dependencies.rb.tt index 4a5d055..fd146a3 100644 --- a/generators/base_app/config/dependencies.rb.tt +++ b/generators/base_app/config/dependencies.rb.tt @@ -1,5 +1,9 @@ # Dependencies contains all required gems, helpers and core configuration +def app(&block) + <%= @class_name %>.class_eval(&block) +end + class <%= @class_name %> < Sinatra::Application bundler_require_dependencies diff --git a/generators/components/orms/couchrest_orm_gen.rb b/generators/components/orms/couchrest_orm_gen.rb new file mode 100644 index 0000000..b959703 --- /dev/null +++ b/generators/components/orms/couchrest_orm_gen.rb @@ -0,0 +1,64 @@ +module SinatraMore + module CouchrestOrmGen + + COUCHREST = <<-COUCHREST +module CouchRestInitializer + def self.registered(app) + app.configure(:development) { set :couchdb, CouchRest.database!('your_dev_db_here') } + app.configure(:production) { set :couchdb, CouchRest.database!('your_production_db_here') } + app.configure(:test) { set :couchdb, CouchRest.database!('your_test_db_here') } + end +end +COUCHREST + + USER = <<-USER +class User < CouchRest::ExtendedDocument + include CouchRest::Validation + + use_database app { couchdb } + + property :name + property :username + property :email + property :crypted_password + unique_id :id + + attr_accessor :password, :password_confirmation + validates_is_confirmed :password + + save_callback :before, :encrypt_password + + def self.find(id) + get(id) + end + + def self.id_for(username) + "user/\#{username}" + end + + def self.authenticate(username, password) + user = User.get(id_for(username)) + user && user.has_password?(password) ? user : nil + end + + def id + self.class.id_for(username) + end + + def encrypt_password + self.crypted_password = BCrypt::Password.create(password) + end + + def has_password?(password) + BCrypt::Password.new(crypted_password) == password + end +end +USER + + def setup_orm + require_dependencies 'couchrest' + create_file("config/initializers/couch_rest.rb", COUCHREST) + create_file("app/models/user.rb", USER) + end + end +end \ No newline at end of file diff --git a/generators/skeleton_generator.rb b/generators/skeleton_generator.rb index 61d2ddf..e4d16d5 100644 --- a/generators/skeleton_generator.rb +++ b/generators/skeleton_generator.rb @@ -20,7 +20,7 @@ def self.banner; "sinatra_gen [app_name] [path] [options]"; end class_option :run_bundler, :aliases => '-b', :default => false, :type => :boolean # Definitions for the available customizable components - component_option :orm, "database engine", :aliases => '-d', :choices => [:datamapper, :mongomapper, :activerecord, :sequel] + component_option :orm, "database engine", :aliases => '-d', :choices => [:datamapper, :mongomapper, :activerecord, :sequel, :couchrest] component_option :test, "testing framework", :aliases => '-t', :choices => [:bacon, :shoulda, :rspec, :testspec, :riot] component_option :mock, "mocking library", :aliases => '-m', :choices => [:mocha, :rr] component_option :script, "javascript library", :aliases => '-s', :choices => [:jquery, :prototype, :rightjs] diff --git a/sinatra_more.gemspec b/sinatra_more.gemspec index 213dd93..5e9e6bd 100644 --- a/sinatra_more.gemspec +++ b/sinatra_more.gemspec @@ -55,6 +55,7 @@ Gem::Specification.new do |s| "generators/components/mocks/mocha_mock_gen.rb", "generators/components/mocks/rr_mock_gen.rb", "generators/components/orms/activerecord_orm_gen.rb", + "generators/components/orms/couchrest_orm_gen.rb", "generators/components/orms/datamapper_orm_gen.rb", "generators/components/orms/mongomapper_orm_gen.rb", "generators/components/orms/sequel_orm_gen.rb", diff --git a/test/generators/test_skeleton_generator.rb b/test/generators/test_skeleton_generator.rb index 48a82d1..d781a30 100644 --- a/test/generators/test_skeleton_generator.rb +++ b/test/generators/test_skeleton_generator.rb @@ -103,6 +103,14 @@ def setup assert_match_in_file(/MongoDbInitializer/, '/tmp/sample_app/config/initializers/mongo_db.rb') assert_match_in_file(/class User.*?include MongoMapper::Document/m, '/tmp/sample_app/app/models/user.rb') end + + should "properly generate for couchrest" do + buffer = silence_logger { SinatraMore::SkeletonGenerator.start(['sample_app', '/tmp', '--orm=couchrest', '--script=none']) } + assert_match /Applying.*?couchrest.*?orm/, buffer + assert_match_in_file(/gem 'couchrest'/, '/tmp/sample_app/Gemfile') + assert_match_in_file(/CouchRestInitializer/, '/tmp/sample_app/config/initializers/couch_rest.rb') + assert_match_in_file(/class User < CouchRest::ExtendedDocument/m, '/tmp/sample_app/app/models/user.rb') + end end context "the generator for renderer component" do From 25d3ce8ca10538f8f4cdb9c8f6c4efab85f899bd Mon Sep 17 00:00:00 2001 From: nesquena Date: Sun, 15 Nov 2009 12:51:13 -0700 Subject: [PATCH 2/3] Added notes about couchrest support to README --- README.rdoc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 7076c86..1a6e810 100644 --- a/README.rdoc +++ b/README.rdoc @@ -691,7 +691,7 @@ The available components and their default options are listed below: * renderer: erb (default), haml * mock: mocha (default), rr * script: jquery (default), prototype, rightjs -* orm: datamapper (default), mongomapper, activerecord, sequel +* orm: datamapper (default), mongomapper, activerecord, sequel, couchrest The generator uses the bundler gem to resolve any application dependencies when the application is newly created. The necessary bundler command can be executed automatically through the generator with @@ -730,6 +730,7 @@ See the wiki article for additional information: Date: Sun, 15 Nov 2009 13:09:44 -0700 Subject: [PATCH 3/3] Added message about commands concept --- ROADMAP | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ROADMAP b/ROADMAP index 26f6331..0ed7732 100644 --- a/ROADMAP +++ b/ROADMAP @@ -26,7 +26,7 @@ Additional wish-list features (integrate existing plugin) (?) * Internationalization - http://r18n.rubyforge.org/#sinatra * Asynchronous Commands - http://github.com/deadprogrammer/spork * Capistrano Deployment - http://github.com/nakajima/capinatra (?) - * Job Queue - http://github.com/adamcooke/resque (or http://github.com/bmizerany/sinatra-dj) + * Job Queue - http://github.com/adamcooke/resque (or http://github.com/bmizerany/sinatra-dj) 'sinatra-cache' Caching concept: @@ -79,4 +79,9 @@ Additional wish-list features (integrate existing plugin) (?) # and to use link_to "Show Admin", url_for(:admin, :show, :id => 5) - link_to "Accounts", url_for(:accounts) \ No newline at end of file + link_to "Accounts", url_for(:accounts) + +'commands' Concept: (console (?) http://github.com/sickill/racksh) + + $ sinatra_more console # loads console for sinatra + $ sinatra_more test # runs all test using specified testing framework \ No newline at end of file