merb_relaxdb 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Generators ADDED
@@ -0,0 +1,3 @@
1
+ scope 'merb-gen' do
2
+ Merb.add_generators File.join(File.dirname(__FILE__), 'lib', 'generators', 'model')
3
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Paul Carey
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.textile ADDED
@@ -0,0 +1,79 @@
1
+ h2. Overview
2
+
3
+ merb_relaxdb is a merb plugin that provides integration with CouchDB via the RelaxDB gem. Version numbers indicate compatibility with Merb. Revision numbers are excluded e.g. merb_relaxdb 1.0 should be compatible with Merb 1.0.x
4
+
5
+ The current release includes integration with merb-auth, but this should not yet be considered stable. A sample integration file is included below.
6
+
7
+ merb_relaxdb expects your merb app to augment the standard layout as described in the following tree.
8
+ couchdb.yml is mandatory. Reference data, sample data and views are optional.
9
+ merb-gen model my_model will create a sample couchdb.yml.
10
+
11
+ To use merb_relaxdb, list @dependency "merb_relaxdb", "1.0"@ in @config/dependencies.rb@ and @use_orm :relaxdb@ in @config/init.rb@
12
+
13
+ For more details on using RelaxDB, see the "RelaxDB wiki":http://github.com/paulcarey/relaxdb/wikis
14
+ <pre>
15
+ <code>
16
+
17
+ my_merb_app $ tree
18
+ .
19
+ |-- Rakefile
20
+ |-- app
21
+ |-- autotest
22
+ |-- config
23
+ | |-- couchdb.yml
24
+ |-- couchdb
25
+ | |-- data
26
+ | | |-- reference
27
+ | | | `-- reference_data.rb
28
+ | | `-- sample
29
+ | | `-- sample_data.rb
30
+ | `-- views
31
+ | `-- views.js
32
+ |-- lib
33
+ |-- log
34
+ |-- public
35
+ |-- scratch
36
+ `-- spec
37
+
38
+ </code>
39
+ </pre>
40
+
41
+ h3. Sample merb/merb-auth/setup.rb
42
+
43
+ <pre>
44
+ <code>
45
+ begin
46
+ Merb::Authentication.user_class = User
47
+
48
+ # Mixin the salted user mixin
49
+ require 'merb-auth-more/mixins/salted_user'
50
+ require 'merb_relaxdb/rdb_salted_user'
51
+
52
+ Merb::Authentication.user_class.class_eval{ include Merb::Authentication::Mixins::SaltedUser }
53
+
54
+ # Setup the session serialization
55
+ class Merb::Authentication
56
+
57
+ def fetch_user(session_user_id)
58
+ RelaxDB.load(session_user_id) rescue nil
59
+ end
60
+
61
+ def store_user(user)
62
+ user.nil? ? user : user._id
63
+ end
64
+ end
65
+
66
+ rescue => e
67
+ Merb.logger.error <<-TEXT
68
+
69
+ You need to setup some kind of user class with merb-auth.
70
+ Merb::Authentication.user_class = User
71
+
72
+ If you want to fully customize your authentication you should use merb-core directly.
73
+
74
+ See lib/authentication/setup.rb and strategies.rb to customize your setup
75
+
76
+ TEXT
77
+ end
78
+ </code>
79
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'merb-core/tasks/merb_rake_helper'
4
+
5
+ PLUGIN = "merb_relaxdb"
6
+ GEM_NAME = "merb_relaxdb"
7
+ GEM_VERSION = "1.0"
8
+ AUTHOR = "Paul Carey"
9
+ EMAIL = "paul.p.carey@gmail.com"
10
+ HOMEPAGE = "http://github.com/paulcarey/merb_relaxdb/"
11
+ SUMMARY = "Merb plugin that provides integration with CouchDB"
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README.textile", "LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+
25
+ # s.add_dependency('relaxdb', '>= 0.1.0') # causes rake install to fail
26
+ # s.add_dependency('paulcarey-relaxdb', '>= 0.1.0') # allows rake install to succeed
27
+
28
+ s.require_path = 'lib'
29
+ s.autorequire = PLUGIN
30
+ s.files = %w(LICENSE README.textile Rakefile Generators) + Dir.glob("{lib,spec}/**/*")
31
+ end
32
+
33
+ Rake::GemPackageTask.new(spec) do |pkg|
34
+ pkg.gem_spec = spec
35
+ end
36
+
37
+ desc "Install the gem"
38
+ task :install => [:package] do
39
+ sh %{sudo gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}
40
+ end
41
+ # task :install do
42
+ # Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION, :local => true)
43
+ # end
44
+
45
+ desc "Uninstall the gem"
46
+ task :uninstall do
47
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
48
+ end
49
+
50
+ desc "Create a gemspec file"
51
+ task :gemspec do
52
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
53
+ file.puts spec.to_ruby
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ Merb::Generators::ModelGenerator.template :model_relaxdb, :orm => :relaxdb do |template|
2
+ template.source = File.dirname(__FILE__) / "templates/model/app/models/%file_name%.rb"
3
+ template.destination = "app/models" / base_path / "#{file_name}.rb"
4
+ end
@@ -0,0 +1,4 @@
1
+ <% with_modules(modules) do -%>
2
+ class <%= class_name %> < RelaxDB::Document
3
+ end
4
+ <% end -%>
@@ -0,0 +1,43 @@
1
+ module Merb
2
+ module RelaxDB
3
+
4
+ class << self
5
+
6
+ def config_file() Merb.dir_for(:config) / "couchdb.yml" end
7
+ def sample_dest() Merb.dir_for(:config) / "couchdb.yml.sample" end
8
+ def sample_source() File.dirname(__FILE__) / "couchdb.yml.sample" end
9
+
10
+ def connect
11
+ begin
12
+ configure_db
13
+ Merb.logger.info "RelaxDB connected to CouchDB #{::RelaxDB.db.uri}"
14
+ rescue => e
15
+ uri = ::RelaxDB.db ? ::RelaxDB.db.uri : "<initialisation error>"
16
+ Merb.logger.fatal "RelaxDB could not connect to CouchDB at #{uri} \n\tRoot cause:#{e}\n#{e.backtrace.join("\n")}"
17
+ exit(1)
18
+ end
19
+ end
20
+
21
+ def configure_db
22
+ if File.exists?(config_file)
23
+ full_config = Erubis.load_yaml_file(config_file)
24
+ config = full_config[Merb.environment.to_sym]
25
+ config[:logger] = Merb.logger
26
+ ::RelaxDB.configure(config)
27
+ ::RelaxDB.use_db(config[:db])
28
+ else
29
+ copy_sample_config
30
+ Merb.logger.error! "No couchdb.yml file found in #{Merb.root}/config."
31
+ Merb.logger.error! "A sample file was created called couchdb.yml.sample for you to copy and edit."
32
+ exit(1)
33
+ end
34
+ end
35
+
36
+ def copy_sample_config
37
+ FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ :development:
2
+ :host: localhost
3
+ :port: 5984
4
+ :db: mydb_dev
5
+ :design_doc: my_design_doc
6
+
7
+ :test:
8
+ :host: localhost
9
+ :port: 5984
10
+ :db: mydb_test
11
+ :design_doc: my_design_doc
12
+
@@ -0,0 +1,74 @@
1
+ desc "Create CouchDB environment from scratch"
2
+ task :relaxdb => ["relaxdb:db", "relaxdb:views", "relaxdb:data", "relaxdb:sample"]
3
+
4
+ namespace :relaxdb do
5
+
6
+ desc "Create CouchDB database"
7
+ task :db => [:check_env] do
8
+ puts "Creating db #{RelaxDB.db.name}"
9
+
10
+ RelaxDB.delete_db(RelaxDB.db.name) rescue "ok"
11
+ RelaxDB.use_db(RelaxDB.db.name)
12
+ end
13
+
14
+ desc "Create CouchDB views auto generated on model load"
15
+ task :autoviews => [:check_env] do
16
+ puts "Creating auto generated views"
17
+ RelaxDB.use_db RelaxDB.db.name
18
+ RelaxDB.enable_view_creation
19
+ Rake::Task["merb_env"].invoke
20
+ end
21
+
22
+ desc "Create CouchDB views from hand coded JavaScript"
23
+ task :hand_coded_views => [:check_env] do
24
+ puts "Creating hand coded views"
25
+
26
+ Dir['couchdb/views/**/*.js'].each do |filename|
27
+ RelaxDB::ViewUploader.upload(filename)
28
+ end
29
+ end
30
+
31
+ desc "Create hand coded and auto generated views"
32
+ task :views => [:autoviews, :hand_coded_views]
33
+
34
+ desc "Create CouchDB reference data"
35
+ task :data => [:check_env, :merb_env] do
36
+ puts "Creating ref data"
37
+
38
+ Dir['couchdb/data/reference/**/*.rb'].each do |filename|
39
+ require filename
40
+ end
41
+ end
42
+
43
+ desc "Create CouchDB sample data"
44
+ task :sample => [:check_env, :merb_env] do
45
+ create_sample_data
46
+ end
47
+
48
+ desc "Create minimal CouchDB environment from scratch"
49
+ task :mindb => ["relaxdb:db", "relaxdb:views"] do
50
+ Dir['couchdb/data/min/**/*.rb'].each { |f| require f }
51
+ end
52
+
53
+ desc "Create base CouchDB environment from scratch (no sample data)"
54
+ task :basedb => ["relaxdb:db", "relaxdb:views", "relaxdb:data"]
55
+
56
+ desc "Apply all outstanding migrations"
57
+ task :migrate => [:check_env, :merb_env] do
58
+ RelaxDB::Migration.run_all Dir["couchdb/migrations/**/*.rb"]
59
+ end
60
+
61
+ task :check_env do
62
+ unless ENV['MERB_ENV']
63
+ puts "MERB_ENV must be specified. Example: rake relaxdb MERB_ENV=development"
64
+ exit(1)
65
+ end
66
+ end
67
+
68
+ def create_sample_data
69
+ Dir['couchdb/data/sample/**/*.rb'].each do |filename|
70
+ require filename
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,53 @@
1
+ class Merb::Authentication
2
+ module Mixins
3
+ module SaltedUser
4
+ module RDBClassMethods
5
+
6
+ def self.extended(base)
7
+ base.class_eval do
8
+
9
+ property :crypted_password
10
+ property :salt
11
+
12
+ before_save :password_checks
13
+
14
+ def password_checks
15
+ if password_required?
16
+ return false unless !password.blank? && password == password_confirmation
17
+ end
18
+ encrypt_password
19
+ true
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ def authenticate(login, password)
26
+ login_param = Merb::Authentication::Strategies::Basic::Base.login_param
27
+ @u = all.sorted_by(login_param) { |q| q.key(login) }.first
28
+ @u && @u.authenticated?(password) ? @u : nil
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ class Merb::Authentication
37
+ module Mixins
38
+ module SaltedUser
39
+
40
+ def self.included(base)
41
+ base.class_eval do
42
+ attr_accessor :password, :password_confirmation
43
+
44
+ include Merb::Authentication::Mixins::SaltedUser::InstanceMethods
45
+ extend Merb::Authentication::Mixins::SaltedUser::ClassMethods
46
+ extend Merb::Authentication::Mixins::SaltedUser::RDBClassMethods
47
+
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+ dependency "relaxdb"
4
+
5
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
6
+ Merb::Plugins.config[:merb_relaxdb] = {
7
+ :chickens => false
8
+ }
9
+
10
+ Merb::BootLoader.before_app_loads do
11
+ require "yaml"
12
+ require "merb_relaxdb/connection.rb"
13
+ Merb::RelaxDB.connect
14
+ end
15
+
16
+ Merb::BootLoader.after_app_loads do
17
+ end
18
+
19
+ Merb::Plugins.add_rakefiles "merb_relaxdb/merbtasks"
20
+
21
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_relaxdb" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb_relaxdb
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Paul Carey
8
+ autorequire: merb_relaxdb
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-27 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Merb plugin that provides integration with CouchDB
17
+ email: paul.p.carey@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ - LICENSE
25
+ files:
26
+ - LICENSE
27
+ - README.textile
28
+ - Rakefile
29
+ - Generators
30
+ - lib/generators
31
+ - lib/generators/model.rb
32
+ - lib/generators/templates
33
+ - lib/generators/templates/model
34
+ - lib/generators/templates/model/app
35
+ - lib/generators/templates/model/app/models
36
+ - lib/generators/templates/model/app/models/%file_name%.rb
37
+ - lib/merb_relaxdb
38
+ - lib/merb_relaxdb/connection.rb
39
+ - lib/merb_relaxdb/couchdb.yml.sample
40
+ - lib/merb_relaxdb/merbtasks.rb
41
+ - lib/merb_relaxdb/rdb_salted_user.rb
42
+ - lib/merb_relaxdb.rb
43
+ - spec/merb_relaxdb_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/paulcarey/merb_relaxdb/
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Merb plugin that provides integration with CouchDB
71
+ test_files: []
72
+