blackmagicvoodoo 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,94 @@
1
+ Sinatra ActiveRecord Extension
2
+ ========================
3
+
4
+ Extends [Sinatra](http://www.sinatrarb.com/) with a extension methods and Rake
5
+ tasks for dealing with a SQL database using the [ActiveRecord ORM](http://api.rubyonrails.org/).
6
+
7
+ Install the `sinatra-activerecord` gem along with one of the database adapters:
8
+
9
+ sudo gem install activerecord
10
+ sudo gem install sinatra-activerecord -s http://gems.gemcutter.org
11
+ sudo gem install sqlite3
12
+ sudo gem install mysql
13
+ sudo gem install postgres
14
+
15
+ adding this to your `Rakefile`
16
+
17
+ # require your app file first
18
+ require 'sinatra-ar-exmple-app'
19
+ require 'sinatra/activerecord/rake'
20
+
21
+ $ rake -T
22
+ rake db:create_migration # create an ActiveRecord migration in ./db/migrate
23
+ rake db:migrate # migrate your database
24
+
25
+ create a migration
26
+
27
+ $ rake db:create_migration NAME=create_foos
28
+ $ vim db/migrate/20090922043513_create_foos.rb
29
+
30
+ class CreateFoos < ActiveRecord::Migration
31
+ def self.up
32
+ create_table :foos do |t|
33
+ t.string :name
34
+ end
35
+ end
36
+
37
+ def self.down
38
+ end
39
+ end
40
+
41
+ run the migration
42
+
43
+ $ rake db:migrate
44
+
45
+ I like to split models out into a separate `database.rb` file and then
46
+ require it from the main app file, but you can plop
47
+ the following code in about anywhere and it'll work just fine:
48
+
49
+ require 'sinatra'
50
+ require 'sinatra/activerecord'
51
+
52
+ # Establish the database connection; or, omit this and use the DATABASE_URL
53
+ # environment variable or the default sqlite://<environment>.db as the connection string:
54
+ set :database, 'sqlite://foo.db'
55
+
56
+ # At this point, you can access the ActiveRecord::Base class using the
57
+ # "database" object:
58
+ puts "the foos table doesn't exist" if !database.table_exists?('foos')
59
+
60
+ # models just work ...
61
+ class Foo < ActiveRecord::Base
62
+ end
63
+
64
+ # see:
65
+ Foo.all
66
+
67
+ # access the models within the context of an HTTP request
68
+ get '/foos/:id' do
69
+ @foo = Foo.find(params[:id])
70
+ erb :foos
71
+ end
72
+
73
+ Copyright (c) 2009 Blake Mizerany
74
+
75
+ Permission is hereby granted, free of charge, to any person
76
+ obtaining a copy of this software and associated documentation
77
+ files (the "Software"), to deal in the Software without
78
+ restriction, including without limitation the rights to use,
79
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
80
+ copies of the Software, and to permit persons to whom the
81
+ Software is furnished to do so, subject to the following
82
+ conditions:
83
+
84
+ The above copyright notice and this permission notice shall be
85
+ included in all copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
88
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
89
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
90
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
91
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
92
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
93
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
94
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ require 'uri'
2
+ require 'time'
3
+ require 'sinatra/base'
4
+ require 'active_record'
5
+ require 'logger'
6
+
7
+ module Sinatra
8
+ module ActiveRecordHelper
9
+ def database
10
+ options.database
11
+ end
12
+ end
13
+
14
+ module ActiveRecordExtension
15
+ def database=(url)
16
+ @database = nil
17
+ set :database_url, url
18
+ database
19
+ end
20
+
21
+ def database
22
+ @database ||= (
23
+ url = URI(database_url)
24
+ ActiveRecord::Base.logger = activerecord_logger
25
+ ActiveRecord::Base.establish_connection(database_options)
26
+ ActiveRecord::Base
27
+ )
28
+ end
29
+
30
+ protected
31
+
32
+ def database_options
33
+ url = URI(database_url)
34
+ options = {
35
+ :adapter => url.scheme,
36
+ :host => url.host,
37
+ :port => url.port,
38
+ :database => url.path[1..-1],
39
+ :username => url.user,
40
+ :password => url.password
41
+ }
42
+ case url.scheme
43
+ when "sqlite"
44
+ options[:adapter] = "sqlite3"
45
+ options[:database] = url.host
46
+ when "postgres"
47
+ options[:adapter] = "postgresql"
48
+ end
49
+ options.merge(database_extras)
50
+ end
51
+
52
+ def self.registered(app)
53
+ app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite://#{environment}.db" }
54
+ app.set :database_extras, Hash.new
55
+ app.set :activerecord_logger, Logger.new(STDOUT)
56
+ app.database # force connection
57
+ app.helpers ActiveRecordHelper
58
+ end
59
+ end
60
+
61
+ register ActiveRecordExtension
62
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_record'
2
+ require 'fileutils'
3
+
4
+ namespace :db do
5
+ desc "migrate your database"
6
+ task :migrate do
7
+ ActiveRecord::Migrator.migrate(
8
+ 'db/migrate',
9
+ ENV["VERSION"] ? ENV["VERSION"].to_i : nil
10
+ )
11
+ end
12
+
13
+ desc "create an ActiveRecord migration in ./db/migrate"
14
+ task :create_migration do
15
+ name = ENV['NAME']
16
+ abort("no NAME specified. use `rake db:create_migration NAME=create_users`") if !name
17
+
18
+ migrations_dir = File.join("db", "migrate")
19
+ version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
20
+ filename = "#{version}_#{name}.rb"
21
+ migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
22
+
23
+ FileUtils.mkdir_p(migrations_dir)
24
+
25
+ open(File.join(migrations_dir, filename), 'w') do |f|
26
+ f << (<<-EOS).gsub(" ", "")
27
+ class #{migration_name} < ActiveRecord::Migration
28
+ def self.up
29
+ end
30
+
31
+ def self.down
32
+ end
33
+ end
34
+ EOS
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+
5
+ s.name = 'blackmagicvoodoo'
6
+ s.version = '0.1.2'
7
+ s.date = '2009-09-21'
8
+
9
+ s.description = "Extends Sinatra with activerecord helpers for instant activerecord use"
10
+ s.summary = s.description
11
+
12
+ s.authors = ["Blake Mizerany"]
13
+ s.email = "blake.mizerany@gmail.com"
14
+
15
+ # = MANIFEST =
16
+ s.files = %w[
17
+ README.md
18
+ lib/sinatra/activerecord.rb
19
+ lib/sinatra/activerecord/rake.rb
20
+ sinatra-activerecord.gemspec
21
+ ]
22
+ # = MANIFEST =
23
+
24
+ s.extra_rdoc_files = %w[README.md]
25
+ s.add_dependency 'sinatra', '>= 0.9.4'
26
+
27
+ s.has_rdoc = true
28
+ s.homepage = "http://github.com/rtomayko/sinatra-activerecord"
29
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra::ActiveRecord"]
30
+ s.require_paths = %w[lib]
31
+ s.rubyforge_project = 'bmizerany'
32
+ s.rubygems_version = '1.1.1'
33
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blackmagicvoodoo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Blake Mizerany
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-09-21 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: sinatra
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 51
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 4
34
+ version: 0.9.4
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Extends Sinatra with activerecord helpers for instant activerecord use
38
+ email: blake.mizerany@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.md
45
+ files:
46
+ - README.md
47
+ - lib/sinatra/activerecord.rb
48
+ - lib/sinatra/activerecord/rake.rb
49
+ - sinatra-activerecord.gemspec
50
+ has_rdoc: true
51
+ homepage: http://github.com/rtomayko/sinatra-activerecord
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --line-numbers
57
+ - --inline-source
58
+ - --title
59
+ - Sinatra::ActiveRecord
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: bmizerany
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Extends Sinatra with activerecord helpers for instant activerecord use
87
+ test_files: []
88
+