sinatra-activerecord 0.1.0
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.
- data/README.md +94 -0
- data/lib/sinatra/activerecord/rake.rb +37 -0
- data/lib/sinatra/activerecord.rb +61 -0
- data/sinatra-activerecord.gemspec +33 -0
- metadata +70 -0
data/README.md
ADDED
@@ -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,37 @@
|
|
1
|
+
require 'activerecord'
|
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,61 @@
|
|
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
|
+
if url.scheme == "sqlite"
|
35
|
+
{
|
36
|
+
:adapter => "sqlite3",
|
37
|
+
:database => url.host
|
38
|
+
}
|
39
|
+
else
|
40
|
+
{
|
41
|
+
:adapter => url.scheme,
|
42
|
+
:host => url.host,
|
43
|
+
:port => url.port,
|
44
|
+
:database => url.path[1..-1],
|
45
|
+
:username => url.user,
|
46
|
+
:password => url.password
|
47
|
+
}
|
48
|
+
end.merge(database_extras)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.registered(app)
|
52
|
+
app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite://#{environment}.db" }
|
53
|
+
app.set :database_extras, Hash.new
|
54
|
+
app.set :activerecord_logger, Logger.new(STDOUT)
|
55
|
+
app.database # force connection
|
56
|
+
app.helpers ActiveRecordHelper
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
register ActiveRecordExtension
|
61
|
+
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 = 'sinatra-activerecord'
|
6
|
+
s.version = '0.1.0'
|
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,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blake Mizerany
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.4
|
24
|
+
version:
|
25
|
+
description: Extends Sinatra with activerecord helpers for instant activerecord use
|
26
|
+
email: blake.mizerany@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.md
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- lib/sinatra/activerecord.rb
|
36
|
+
- lib/sinatra/activerecord/rake.rb
|
37
|
+
- sinatra-activerecord.gemspec
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/rtomayko/sinatra-activerecord
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --line-numbers
|
45
|
+
- --inline-source
|
46
|
+
- --title
|
47
|
+
- Sinatra::ActiveRecord
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: bmizerany
|
65
|
+
rubygems_version: 1.3.4
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Extends Sinatra with activerecord helpers for instant activerecord use
|
69
|
+
test_files: []
|
70
|
+
|