mohiam-merb_couchrest 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Generators ADDED
@@ -0,0 +1,4 @@
1
+ scope 'merb-gen' do
2
+ dir = File.join(File.dirname(__FILE__), 'lib', 'generators/')
3
+ Merb.add_generators dir + 'model', dir + 'resource_controller'
4
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 YOUR NAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+ merb_couchrest
2
+ ==============
3
+
4
+ A plugin for the Merb framework that provides support for CouchRest Models.
5
+
6
+ Currently based around CouchRest::ExtendedDocument introduced by Matt Aimonetti.
7
+
8
+
9
+
10
+ = Connection options
11
+
12
+ Merb CouchRest plugin uses config/database.yml for connection configuration.
13
+
14
+ Options are:
15
+
16
+ * host. provide a string or an array of strings to attempt connections with
17
+ * database.
18
+
19
+
20
+ Uses the CouchDB client CouchRest by Chris Anderson.
21
+
22
+ Hacked together by studying the merb_sequel plugin.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+
7
+ GEM_NAME = "merb_couchrest"
8
+ GEM_VERSION = "0.1.3"
9
+ AUTHOR = "Alexander Veenendaal"
10
+ EMAIL = "odogono@gmail.com"
11
+ HOMEPAGE = "http://github.com/mohiam/merb_couchrest"
12
+ SUMMARY = "Merb ORM plugin that provides support for CouchRest Models"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM_NAME
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.author = AUTHOR
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+ s.add_dependency('merb', '>= 1.0.9')
26
+ s.add_dependency("mattetti-couchrest", ">= 0.14")
27
+ s.require_path = 'lib'
28
+ s.files = %w(LICENSE README Rakefile TODO Generators) + Dir.glob("{lib,spec}/**/*")
29
+
30
+ end
31
+
32
+ desc "install the plugin as a gem"
33
+ task :install do
34
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
35
+ end
36
+
37
+ desc "Uninstall the gem"
38
+ task :uninstall do
39
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
40
+ end
41
+
42
+ desc "Create a gemspec file"
43
+ task :gemspec do
44
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
45
+ file.puts spec.to_ruby
46
+ end
47
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ TODO:
2
+ Fix LICENSE with your name
3
+ Fix Rakefile with your name and contact info
4
+ Add your code to lib/merb_couchrest.rb
5
+ Add your Merb rake tasks to lib/merb_couchrest/merbtasks.rb
@@ -0,0 +1,4 @@
1
+ Merb::Generators::ModelGenerator.template :model_couchrest, :orm => :couchrest do |t|
2
+ t.source = File.dirname(__FILE__) / "templates/model/app/models/%file_name%.rb"
3
+ t.destination = "app/models" / base_path / "#{file_name}.rb"
4
+ end
@@ -0,0 +1,12 @@
1
+ Merb::Generators::ResourceControllerGenerator.template :controller_couchrest, :orm => :couchrest do |t|
2
+ t.source = File.dirname(__FILE__) / "templates/resource_controller/app/controllers/%file_name%.rb"
3
+ t.destination = "app/controllers" / base_path / "#{file_name}.rb"
4
+ end
5
+
6
+ [:index, :show, :edit, :new].each do |view|
7
+ Merb::Generators::ResourceControllerGenerator.template "view_#{view}_couchrest".to_sym,
8
+ :orm => :couchrest, :template_engine => :erb do |t|
9
+ t.source = File.dirname(__FILE__) / "templates/resource_controller/app/views/%file_name%/#{view}.html.erb"
10
+ t.destination = "app/views" / base_path / "#{file_name}/#{view}.html.erb"
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ <% with_modules(modules) do -%>
2
+ class <%= class_name %> < CouchRest::ExtendedDocument
3
+ end
4
+ <% end -%>
@@ -0,0 +1,66 @@
1
+ <% with_modules(modules) do -%>
2
+ class <%= class_name %> < Application
3
+ # provides :xml, :yaml, :js
4
+
5
+ # GET /<%= resource_path %>
6
+ def index
7
+ @<%= plural_model %> = <%= model_class_name %>.all
8
+ display @<%= plural_model %>
9
+ end
10
+
11
+ # GET /<%= resource_path %>/:id
12
+ def show
13
+ @<%= singular_model %> = <%= model_class_name %>[params[:id]]
14
+ raise NotFound unless @<%= singular_model %>
15
+ display @<%= singular_model %>
16
+ end
17
+
18
+ # GET /<%= resource_path %>/new
19
+ def new
20
+ only_provides :html
21
+ @<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
22
+ render
23
+ end
24
+
25
+ # POST /<%= resource_path %>
26
+ def create
27
+ @<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
28
+ if @<%= singular_model %>.save
29
+ redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
30
+ else
31
+ render :new
32
+ end
33
+ end
34
+
35
+ # GET /<%= resource_path %>/:id/edit
36
+ def edit
37
+ only_provides :html
38
+ @<%= singular_model %> = <%= model_class_name %>[params[:id]]
39
+ raise NotFound unless @<%= singular_model %>
40
+ render
41
+ end
42
+
43
+ # PUT /<%= resource_path %>/:id
44
+ def update
45
+ @<%= singular_model %> = <%= model_class_name %>[params[:id]]
46
+ raise NotFound unless @<%= singular_model %>
47
+ if @<%= singular_model %>.update(params[:<%= singular_model %>])
48
+ redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
49
+ else
50
+ raise BadRequest
51
+ end
52
+ end
53
+
54
+ # DELETE /<%= resource_path %>/:id
55
+ def destroy
56
+ @<%= singular_model %> = <%= model_class_name %>[params[:id]]
57
+ raise NotFound unless @<%= singular_model %>
58
+ if @<%= singular_model %>.destroy
59
+ redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>s)
60
+ else
61
+ raise BadRequest
62
+ end
63
+ end
64
+
65
+ end
66
+ <% end -%>
@@ -0,0 +1,3 @@
1
+ <h1><%= class_name %> controller, edit action</h1>
2
+
3
+ <p>Edit this file in <tt>app/views/<%= file_name %>/edit.html.erb</tt></p>
@@ -0,0 +1,3 @@
1
+ <h1><%= class_name %> controller, index action</h1>
2
+
3
+ <p>Edit this file in <tt>app/views/<%= file_name %>/index.html.erb</tt></p>
@@ -0,0 +1,3 @@
1
+ <h1><%= class_name %> controller, new action</h1>
2
+
3
+ <p>Edit this file in <tt>app/views/<%= file_name %>/new.html.erb</tt></p>
@@ -0,0 +1,3 @@
1
+ <h1><%= class_name %> controller, show action</h1>
2
+
3
+ <p>Edit this file in <tt>app/views/<%= file_name %>/show.html.erb</tt></p>
@@ -0,0 +1,105 @@
1
+ require "fileutils"
2
+ require "couchrest"
3
+
4
+
5
+ module Merb
6
+ module Orms
7
+ module CouchRest
8
+
9
+
10
+ class << self
11
+
12
+ def config_file() Merb.dir_for(:config) / "database.yml" end
13
+ def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end
14
+ def sample_source() File.dirname(__FILE__) / "database.yml.sample" end
15
+
16
+ def copy_sample_config
17
+ FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
18
+ end
19
+
20
+
21
+ def config
22
+ @config ||= begin
23
+ # Convert string keys to symbols
24
+ full_config = Erubis.load_yaml_file(config_file)
25
+ config = (Merb::Plugins.config[:merb_couchrest] = {})
26
+ (full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value|
27
+ config[key.to_sym] = value
28
+ end
29
+ config
30
+ end
31
+ end
32
+
33
+ def connect
34
+ if File.exists?(config_file)
35
+
36
+ # it is possible that we have been passed an array of hosts - attempt each in turn until one is found
37
+
38
+ config[:host].each do |host|
39
+
40
+ begin
41
+
42
+ Merb.logger.info!("Attempting connection to the '#{config[:database]}' database on '#{host}' ...")
43
+
44
+ # Merb.logger.info!("using string #{host}/#{config[:database]}")
45
+
46
+ server = ::CouchRest.new(host)
47
+
48
+ # calling this will cause an exception if the host is invalid
49
+ info = server.info()
50
+
51
+ Merb.logger.info!("Connected to '#{host}' - version #{info['version']}")
52
+
53
+ begin
54
+ if config[:create_db_if_absent]
55
+ database_connection = server.database!( config[:database] )
56
+ else
57
+ database_connection = server.database( config[:database] )
58
+ end
59
+
60
+ # this will test the connection and raise a ResourceNotFound exception
61
+ # if it is not valid
62
+ database_connection.info()
63
+
64
+ # set the default database
65
+ ::CouchRest::Document.use_database( database_connection )
66
+
67
+ # we are done - return with the result
68
+ return database_connection
69
+
70
+ rescue RestClient::ResourceNotFound
71
+ Merb.logger.info!("#{config[:database]} is not available on #{host}")
72
+ end
73
+
74
+ rescue Errno::ECONNREFUSED
75
+ Merb.logger.info!("#{host} not available")
76
+ rescue => e
77
+ Merb.logger.info!("Connection Error: #{e}")
78
+ Merb.print_colorized_backtrace(e)
79
+ exit(1)
80
+ end
81
+
82
+ end
83
+
84
+ # if we have reached this point, then a connection wasn't found
85
+
86
+ Merb.logger.info!("Unable to connect")
87
+ exit(1)
88
+
89
+
90
+ else
91
+ copy_sample_config
92
+ Merb.logger.set_log(STDERR)
93
+ Merb.logger.error! "No database.yml file found at #{config_file}."
94
+ Merb.logger.error! "A sample file was created called #{sample_dest} for you to copy and edit."
95
+ exit(1)
96
+ end
97
+ end
98
+
99
+
100
+
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,14 @@
1
+ ---
2
+ # This is a sample database file for the CouchRest ORM
3
+ :development: &defaults
4
+ :database: sample_development
5
+ :host: localhost:5984
6
+ :create_db_if_absent: true
7
+
8
+ :test:
9
+ <<: *defaults
10
+ :database: sample_test
11
+
12
+ :production:
13
+ <<: *defaults
14
+ :database: sample_production
@@ -0,0 +1,76 @@
1
+ require 'couchrest'
2
+ require 'merb-core/dispatch/session'
3
+
4
+
5
+ module Merb
6
+
7
+ class CouchRestSessionStore < CouchRest::ExtendedDocument
8
+
9
+ class << self
10
+
11
+ # ==== Parameters
12
+ # session_id<String>:: ID of the session to retrieve.
13
+ #
14
+ # ==== Returns
15
+ # ContainerSession:: The session corresponding to the ID.
16
+ def retrieve_session(session_id)
17
+
18
+ begin
19
+ doc = get( session_id )
20
+ return Marshal.load(doc[:data])
21
+ rescue
22
+ end
23
+
24
+ end
25
+
26
+ # ==== Parameters
27
+ # session_id<String>:: ID of the session to set.
28
+ # data<ContainerSession>:: The session to set.
29
+ #
30
+ # :api: private
31
+ def store_session(session_id, data)
32
+
33
+ marshaled = Marshal.dump(data)
34
+
35
+ begin
36
+ doc = get( session_id )
37
+ doc[:data] = marshaled
38
+ doc[:updated_at] = Time.now
39
+ doc.save
40
+ rescue
41
+ doc = self.new( '_id' => session_id, :data => marshaled, :created_at => Time.now )
42
+ doc.save
43
+ end
44
+
45
+ end
46
+
47
+ # ==== Parameters
48
+ # session_id<String>:: ID of the session to delete.
49
+ #
50
+ # :api: private
51
+ def delete_session(session_id)
52
+ begin
53
+ doc = get(session_id)
54
+ doc.destroy()
55
+ rescue
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+
64
+ class CouchdbSession < SessionStoreContainer
65
+
66
+ # The session store type
67
+ self.session_store_type = :couchrest
68
+
69
+ # The store object is the model class itself
70
+ self.store = CouchRestSessionStore
71
+
72
+ end
73
+
74
+
75
+ end
76
+
@@ -0,0 +1,23 @@
1
+ namespace :couchrest do
2
+
3
+
4
+ task :merb_start do
5
+ Merb.start :adapter => 'runner',
6
+ :environment => ENV['MERB_ENV'] || 'development'
7
+ end
8
+
9
+ namespace :sessions do
10
+
11
+ desc "Clears sessions"
12
+ task :clear => :merb_start do
13
+
14
+ Merb::CouchRestSessionStore.all.each{ |doc| doc.database.delete_doc(doc,true) }
15
+ Merb::CouchRestSessionStore.bulk_save()
16
+
17
+ end
18
+
19
+ end
20
+
21
+
22
+
23
+ end
@@ -0,0 +1,27 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+
4
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
5
+ Merb::Plugins.config[:merb_couchrest] = {}
6
+ require File.join(File.dirname(__FILE__) / "merb" / "orms" / "couchrest" / "connection" )
7
+
8
+ Merb::BootLoader.before_app_loads do
9
+
10
+ Merb::Orms::CouchRest.connect
11
+
12
+ if Merb::Config.session_stores.include?(:couchrest)
13
+ Merb.logger.debug "Using CouchRest sessions"
14
+ require File.join(File.dirname(__FILE__) / "merb" / "session" / "couchrest_session")
15
+ end
16
+
17
+ end
18
+
19
+ Merb::BootLoader.after_app_loads do
20
+
21
+ # TODO - How do we disconnect if, in fact, we need to ?
22
+
23
+ end
24
+
25
+ Merb::Plugins.add_rakefiles "merb_couchrest/merbtasks"
26
+
27
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_couchrest" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mohiam-merb_couchrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Veenendaal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mattetti-couchrest
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.14"
34
+ version:
35
+ description: Merb ORM plugin that provides support for CouchRest Models
36
+ email: odogono@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - LICENSE
44
+ - TODO
45
+ files:
46
+ - LICENSE
47
+ - README
48
+ - Rakefile
49
+ - TODO
50
+ - Generators
51
+ - lib/generators
52
+ - lib/generators/model.rb
53
+ - lib/generators/resource_controller.rb
54
+ - lib/generators/templates
55
+ - lib/generators/templates/model
56
+ - lib/generators/templates/model/app
57
+ - lib/generators/templates/model/app/models
58
+ - lib/generators/templates/model/app/models/%file_name%.rb
59
+ - lib/generators/templates/resource_controller
60
+ - lib/generators/templates/resource_controller/app
61
+ - lib/generators/templates/resource_controller/app/controllers
62
+ - lib/generators/templates/resource_controller/app/controllers/%file_name%.rb
63
+ - lib/generators/templates/resource_controller/app/views
64
+ - lib/generators/templates/resource_controller/app/views/%file_name%
65
+ - lib/generators/templates/resource_controller/app/views/%file_name%/edit.html.erb
66
+ - lib/generators/templates/resource_controller/app/views/%file_name%/index.html.erb
67
+ - lib/generators/templates/resource_controller/app/views/%file_name%/new.html.erb
68
+ - lib/generators/templates/resource_controller/app/views/%file_name%/show.html.erb
69
+ - lib/merb
70
+ - lib/merb/orms
71
+ - lib/merb/orms/couchrest
72
+ - lib/merb/orms/couchrest/connection.rb
73
+ - lib/merb/orms/couchrest/database.yml.sample
74
+ - lib/merb/session
75
+ - lib/merb/session/couchrest_session.rb
76
+ - lib/merb_couchrest
77
+ - lib/merb_couchrest/merbtasks.rb
78
+ - lib/merb_couchrest.rb
79
+ - spec/merb_couchrest_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/mohiam/merb_couchrest
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ version:
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.2.0
104
+ signing_key:
105
+ specification_version: 2
106
+ summary: Merb ORM plugin that provides support for CouchRest Models
107
+ test_files: []
108
+