merb_sequel 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,7 +1,11 @@
1
- merb_sequel
2
- =================
1
+ = merb_sequel
3
2
 
4
3
  A plugin for the Merb framework that provides Sequel access
5
4
 
6
5
  After you install the plugin, you have access to the sequel_migration generator in your merb projects
7
6
  example: /merb/root/script/generate sequel_migration new_migration
7
+
8
+
9
+ = Contributors
10
+
11
+ originally written by Duane Johnson (canadaduane@gmail.com).
data/Rakefile CHANGED
@@ -1,36 +1,43 @@
1
- require 'rubygems'
2
- require 'rake/gempackagetask'
1
+ require "rubygems"
2
+ require "rake/gempackagetask"
3
3
 
4
- PLUGIN = "merb_sequel"
5
- NAME = "merb_sequel"
6
- VERSION = "0.4.1"
7
- AUTHOR = "Duane Johnson"
8
- EMAIL = "canadaduane@gmail.com"
4
+ PLUGIN = "merb_sequel"
5
+ NAME = "merb_sequel"
6
+ VERSION = "0.4.2"
7
+ AUTHOR = "Wayne E. Seguin"
8
+ EMAIL = "wayneeseguin@gmail.com"
9
9
  HOMEPAGE = "http://merb-plugins.rubyforge.org/merb_sequel/"
10
- SUMMARY = "Merb plugin that provides support for the Sequel database object"
10
+ SUMMARY = "Merb plugin that provides support for Sequel and Sequel::Model"
11
11
 
12
- spec = Gem::Specification.new do |s|
13
- s.name = NAME
14
- s.version = VERSION
15
- s.platform = Gem::Platform::RUBY
16
- s.has_rdoc = true
17
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
- s.summary = SUMMARY
19
- s.description = s.summary
20
- s.author = AUTHOR
21
- s.email = EMAIL
22
- s.homepage = HOMEPAGE
23
- s.add_dependency('merb', '>= 0.4.0')
24
- s.require_path = 'lib'
25
- s.autorequire = PLUGIN
26
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,sequel_generators}/**/*")
12
+ specification = Gem::Specification.new do |spec|
13
+ spec.name = NAME
14
+ spec.email = EMAIL
15
+ spec.author = AUTHOR
16
+ spec.version = VERSION
17
+ spec.summary = SUMMARY
18
+ spec.platform = Gem::Platform::RUBY
19
+ spec.has_rdoc = true
20
+ spec.homepage = HOMEPAGE
21
+ spec.description = SUMMARY
22
+ spec.autorequire = PLUGIN
23
+ spec.require_path = "lib"
24
+ spec.extra_rdoc_files = ["README", "LICENSE", 'TODO']
25
+ # Dependencies
26
+ spec.add_dependency("merb", ">= 0.4.0")
27
+ spec.add_dependency("sequel", ">= 1.0.0")
28
+ spec.add_dependency("sequel_model", ">= 0.2")
29
+ spec.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,sequel_generators}/**/*")
27
30
  end
28
31
 
29
- Rake::GemPackageTask.new(spec) do |pkg|
30
- pkg.gem_spec = spec
32
+ Rake::GemPackageTask.new(specification) do |package|
33
+ package.gem_spec = specification
31
34
  end
32
35
 
33
36
  task :install do
34
37
  sh %{rake package}
35
38
  sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
36
- end
39
+ end
40
+
41
+ task :release => :package do
42
+ sh %{rubyforge add_release merb #{PLUGIN} #{VERSION} pkg/#{NAME}-#{VERSION}.gem}
43
+ end
@@ -1,4 +1,4 @@
1
- require 'fileutils'
1
+ require "fileutils"
2
2
 
3
3
  module Merb
4
4
  module Orms
@@ -25,50 +25,35 @@ module Merb
25
25
 
26
26
  # Database connects as soon as the gem is loaded
27
27
  def connect
28
+ require "sequel"
29
+ require "sequel_model"
30
+
28
31
  if File.exists?(config_file)
29
- puts "Connecting to database..."
30
- # Load the correct Sequel adapter and set it up according to the yaml file
31
- case config[:adapter]
32
- when 'mysql'
33
- require "sequel/mysql"
34
- host = config[:host] || 'localhost'
35
- user = config[:user] || config[:username] || 'root'
36
- password = config[:password]
37
- # Use Sequel::Model.db to access this object
38
- ::Sequel.mysql(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER)
39
- when 'postgresql'
40
- require "sequel/postgres"
41
- host = config[:host] || 'localhost'
42
- user = config[:user] || config[:username] || 'root'
43
- password = config[:password]
44
- encoding = config[:encoding] || config[:charset] || nil
45
-
46
- if encoding
47
- ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :encoding => encoding, :logger => MERB_LOGGER)
48
- else
49
- ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER)
50
- end
51
- when 'sqlite'
52
- require "sequel/sqlite"
53
- if config[:database]
54
- ::Sequel.sqlite(config[:database], :logger => MERB_LOGGER)
55
- else
56
- ::Sequel.sqlite(:logger => MERB_LOGGER)
57
- end
58
- else
59
- require "sequel/sqlite"
60
- p full_config
61
- puts "No adapter specified in config/database.yml... trying a memory-only sqlite database"
62
- ::Sequel.sqlite
63
- end
32
+ puts "#{Time.now.httpdate}: Connecting to the '#{config[:adapter]}' database '#{config[:hosts]}' ..."
33
+ connection = ::Sequel.connect(config_options(config))
34
+ MERB_LOGGER.error("Connection Error: #{e}") unless connection
64
35
  else
65
36
  copy_sample_config
66
- puts "No database.yml file found in #{MERB_ROOT}/config."
67
- puts "A sample file was created called database.sample.yml for you to copy and edit."
37
+ puts "#{Time.now.httpdate}: No database.yml file found in #{MERB_ROOT}/config."
38
+ puts "A sample file was created called config/database.sample.yml for you to copy and edit."
68
39
  exit(1)
69
40
  end
70
41
  end
71
42
 
43
+ def config_options(config = {})
44
+ options = {}
45
+ options[:adapter] = (config[:adapter] || "sqlite")
46
+ options[:host] = (config[:host] || "localhost")
47
+ options[:user] = (config[:username] || config[:user] || "root")
48
+ options[:password] = config[:password] if config[:password]
49
+ if (config[:encoding] || config[:charset])
50
+ options[:encoding] = (config[:encoding] || config[:charset])
51
+ end
52
+ options[:database] = config[:database] if config[:database]
53
+ options[:logger] = MERB_LOGGER
54
+ options
55
+ end
56
+
72
57
  # Registering this ORM lets the user choose sequel as a session store
73
58
  # in merb.yml's session_store: option.
74
59
  def register_session_type
@@ -76,7 +61,9 @@ module Merb
76
61
  "merb/session/sequel_session",
77
62
  "Using Sequel database sessions")
78
63
  end
64
+
79
65
  end
80
66
  end
81
67
  end
82
- end
68
+
69
+ end
@@ -1,14 +1,16 @@
1
1
  class AddSessionsTable < Sequel::Migration
2
+
2
3
  def up
3
4
  create_table :sessions do
4
5
  primary_key :id
5
- varchar :session_id, :size => 32, :unique => true
6
+ varchar :session_id, :size => 64, :unique => true
6
7
  timestamp :created_at
7
8
  text :data
8
9
  end
9
10
  end
10
11
 
11
12
  def down
12
- execute 'DROP TABLE sessions'
13
+ execute "DROP TABLE sessions"
13
14
  end
14
- end
15
+
16
+ end
@@ -1,5 +1,8 @@
1
+ require 'base64'
2
+
1
3
  module Merb
2
4
  module SessionMixin
5
+
3
6
  def setup_session
4
7
  MERB_LOGGER.info("Setting up session")
5
8
  before = cookies[_session_id_key]
@@ -13,6 +16,7 @@ module Merb
13
16
  request.session.save if @_fingerprint != Marshal.dump(request.session.data).hash
14
17
  set_cookie(_session_id_key, request.session.values[:session_id], _session_expiry) if (@_new_cookie || request.session.needs_new_cookie)
15
18
  end
19
+
16
20
  end
17
21
 
18
22
  table_name = (Merb::Plugins.config[:sequel][:session_table_name] || "sessions")
@@ -22,6 +26,7 @@ module Merb
22
26
  primary_key :id
23
27
  varchar :session_id
24
28
  varchar :data
29
+ timestamp :created_at
25
30
  end
26
31
 
27
32
  attr_accessor :needs_new_cookie
@@ -29,7 +34,8 @@ module Merb
29
34
  class << self
30
35
  # Generates a new session ID and creates a row for the new session in the database.
31
36
  def generate
32
- create(:session_id => Merb::SessionMixin::rand_uuid, :data => marshal({}))
37
+ create(:session_id => Merb::SessionMixin::rand_uuid,
38
+ :data => marshal({}), :created_at => Time.now)
33
39
  end
34
40
 
35
41
  # Gets the existing session based on the <tt>session_id</tt> available in cookies.
@@ -53,7 +59,10 @@ module Merb
53
59
  255
54
60
  end
55
61
 
56
- def marshal(data) Base64.encode64(Marshal.dump(data)) if data end
62
+ def marshal(data)
63
+ Base64.encode64(Marshal.dump(data)) if data
64
+ end
65
+
57
66
  def unmarshal(data)
58
67
  Marshal.load(Base64.decode64(data)) if data
59
68
  end
@@ -75,8 +84,8 @@ module Merb
75
84
  end
76
85
 
77
86
  # Lazy-delete of session data
78
- def delete
79
- self.data = {}
87
+ def delete(key = nil)
88
+ key ? self.data.delete(key) : self.data.clear
80
89
  end
81
90
 
82
91
  def [](key)
@@ -87,6 +96,14 @@ module Merb
87
96
  data[key] = val
88
97
  end
89
98
 
99
+ def empty?
100
+ data.empty?
101
+ end
102
+
103
+ def each(&b)
104
+ data.each(&b)
105
+ end
106
+
90
107
  # Lazy-unmarshal session state.
91
108
  def data
92
109
  @data ||= self.class.unmarshal(@values[:data]) || {}
@@ -98,6 +115,7 @@ module Merb
98
115
  end
99
116
 
100
117
  private
118
+
101
119
  attr_writer :data
102
120
 
103
121
  before_save do # marshal_data!
@@ -126,4 +144,5 @@ module Merb
126
144
 
127
145
  puts "Created sessions table."
128
146
  end
147
+
129
148
  end
data/lib/merbtasks.rb CHANGED
@@ -1,16 +1,21 @@
1
- require 'fileutils'
1
+ require "fileutils"
2
2
 
3
3
  namespace :sequel do
4
+
4
5
  namespace :db do
6
+
5
7
  desc "Perform migration using migrations in schema/migrations"
6
8
  task :migrate => :merb_env do
7
9
  Sequel::Migrator.apply(Merb::Orms::Sequel.connect, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
8
10
  end
11
+
9
12
  end
10
13
 
11
14
  namespace :sessions do
15
+
12
16
  desc "Creates session migration"
13
17
  task :create => :merb_env do
18
+ # TODO: this should not use '001' always...
14
19
  dest = File.join(MERB_ROOT, "schema", "migrations","001_add_sessions_table.rb")
15
20
  source = File.join(File.dirname(__FILE__), "merb", "session","001_add_sessions_table.rb")
16
21
 
@@ -23,5 +28,7 @@ namespace :sequel do
23
28
 
24
29
  Merb::Orms::Sequel.connect.execute("DELETE FROM #{table_name}")
25
30
  end
31
+
26
32
  end
27
- end
33
+
34
+ end
@@ -1,4 +1,4 @@
1
- require 'merb/generators/merb_generator_helpers'
1
+ require "merb/generators/merb_generator_helpers"
2
2
 
3
3
  class MigrationGenerator < Merb::GeneratorHelpers::MigrationGeneratorBase
4
4
 
@@ -1,17 +1,25 @@
1
+ # For details on Sequel migrations see
2
+ # http://sequel.rubyforge.org/
3
+ # http://code.google.com/p/ruby-sequel/wiki/Migrations
4
+
1
5
  class <%= class_name.snake_case.camel_case %> < Sequel::Migration
6
+
2
7
  def up
3
8
  <%= "create_table :#{table_name} do" if table_name %>
4
- <% for attribute in table_attributes -%>
9
+ <% if table_attributes.empty? -%>
10
+ primary_key :id
11
+ <% else -%>
12
+ <% table_attributes.each do |attribute| -%>
5
13
  <%= attribute.type %> :<%= attribute.name %>
6
14
  <% end -%>
7
- <%= "end" if table_name %>
15
+ <% end -%>
16
+ <%= "end" if table_name %>
8
17
  end
9
18
 
10
19
  def down
11
20
  <% if table_name -%>
12
- execute 'DROP TABLE <%= table_name %>'
21
+ execute "DROP TABLE <%= table_name %>"
13
22
  <% end -%>
14
23
  end
15
- end
16
-
17
24
 
25
+ end
@@ -1,4 +1,4 @@
1
- require 'merb/generators/merb_generator_helpers'
1
+ require "merb/generators/merb_generator_helpers"
2
2
 
3
3
  class ModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase
4
4
 
@@ -1,2 +1,2 @@
1
- class <%= class_name %> < Sequel::Model
2
- end
1
+ class <%= class_name %> < Sequel::Model(:<%= class_name.snake_case.pluralize %>)
2
+ end
@@ -1,4 +1,4 @@
1
- require 'merb/generators/merb_generator_helpers'
1
+ require "merb/generators/merb_generator_helpers"
2
2
 
3
3
  class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorBase
4
4
 
@@ -7,7 +7,7 @@ class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorB
7
7
  name, *actions = args.flatten
8
8
  runtime_options[:actions] = %w[index show new edit]
9
9
  runtime_options[:test_stub_generator] = "merb_controller_test"
10
- super( [name], runtime_options )
10
+ super([name], runtime_options)
11
11
  end
12
12
 
13
13
  def self.superclass
@@ -15,12 +15,13 @@ class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorB
15
15
  end
16
16
 
17
17
  protected
18
+
18
19
  def banner
19
- <<-EOS
20
+ <<-EOS
20
21
  Creates a Merb controller, views and specs using Sequel Models
21
22
 
22
23
  USAGE: #{$0} #{spec.name} resource_name"
23
24
  EOS
24
25
  end
25
26
 
26
- end
27
+ end
@@ -1,6 +1,7 @@
1
1
  <% klass = class_name.singularize -%>
2
2
  <% ivar = class_name.snake_case.singularize -%>
3
3
  class <%= class_name %> < Application
4
+
4
5
  provides :xml, :js, :yaml
5
6
 
6
7
  def index
@@ -9,7 +10,7 @@ class <%= class_name %> < Application
9
10
  end
10
11
 
11
12
  def show
12
- @<%= ivar %> = <%= klass %>[:id => params[:id]]
13
+ @<%= ivar %> = <%= klass %>[params[:id]]
13
14
  render @<%= ivar %>
14
15
  end
15
16
 
@@ -30,12 +31,12 @@ class <%= class_name %> < Application
30
31
 
31
32
  def edit
32
33
  only_provides :html
33
- @<%= ivar %> = <%= klass %>[:id => params[:id]]
34
+ @<%= ivar %> = <%= klass %>[params[:id]]
34
35
  render
35
36
  end
36
37
 
37
38
  def update
38
- @<%= ivar %> = <%= klass %>[:id => params[:id]]
39
+ @<%= ivar %> = <%= klass %>[params[:id]]
39
40
  if @<%= ivar %>.update(params[:<%= ivar %>])
40
41
  redirect url(:<%= ivar %>, @<%= ivar %>)
41
42
  else
@@ -44,11 +45,12 @@ class <%= class_name %> < Application
44
45
  end
45
46
 
46
47
  def destroy
47
- @<%= ivar %> = <%= klass %>[:id => params[:id]]
48
+ @<%= ivar %> = <%= klass %>[params[:id]]
48
49
  if @<%= ivar %>.destroy
49
50
  redirect url(:<%= ivar %>s)
50
51
  else
51
52
  raise BadRequest
52
53
  end
53
54
  end
54
- end
55
+
56
+ end
@@ -1,5 +1,5 @@
1
1
  module Merb
2
- module <%= class_name %>Helper
2
+ module <%= class_name %>Helper
3
3
 
4
4
  end
5
5
  end
@@ -1,7 +1,15 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
2
 
3
3
  describe "merb_sequel" do
4
+
4
5
  it "should do nothing" do
5
6
  true.should == true
6
7
  end
8
+
9
+ end
10
+
11
+ describe "merb_sequel Generators" do
12
+ it "should description" do
13
+
14
+ end
7
15
  end
data/specs/spec_helper.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  $TESTING=true
2
- $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.push File.join(File.dirname(__FILE__), "..", "lib")
metadata CHANGED
@@ -1,91 +1,116 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: merb_sequel
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.4.1
7
- date: 2007-11-12 00:00:00 -06:00
8
- summary: Merb plugin that provides support for the Sequel database object
9
- require_paths:
10
- - lib
11
- email: canadaduane@gmail.com
12
- homepage: http://merb-plugins.rubyforge.org/merb_sequel/
13
- rubyforge_project:
14
- description: Merb plugin that provides support for the Sequel database object
15
- autorequire: merb_sequel
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.4.2
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
- - Duane Johnson
7
+ - Wayne E. Seguin
8
+ autorequire: merb_sequel
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-03 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: sequel
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.0.0
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: sequel_model
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0.2"
41
+ version:
42
+ description: Merb plugin that provides support for Sequel and Sequel::Model
43
+ email: wayneeseguin@gmail.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README
50
+ - LICENSE
51
+ - TODO
31
52
  files:
32
53
  - LICENSE
33
54
  - README
34
55
  - Rakefile
35
56
  - TODO
36
57
  - lib/merb
37
- - lib/merb_sequel.rb
38
- - lib/merbtasks.rb
39
58
  - lib/merb/orms
40
- - lib/merb/session
41
59
  - lib/merb/orms/sequel
42
60
  - lib/merb/orms/sequel/connection.rb
43
61
  - lib/merb/orms/sequel/database.sample.yml
62
+ - lib/merb/session
44
63
  - lib/merb/session/001_add_sessions_table.rb
45
64
  - lib/merb/session/sequel_session.rb
65
+ - lib/merb_sequel.rb
66
+ - lib/merbtasks.rb
46
67
  - specs/merb_sequel_spec.rb
47
68
  - specs/spec_helper.rb
48
69
  - sequel_generators/migration
49
- - sequel_generators/model
50
- - sequel_generators/resource_controller
51
70
  - sequel_generators/migration/migration_generator.rb
52
71
  - sequel_generators/migration/templates
53
- - sequel_generators/migration/USAGE
54
72
  - sequel_generators/migration/templates/new_migration.erb
73
+ - sequel_generators/migration/USAGE
74
+ - sequel_generators/model
55
75
  - sequel_generators/model/model_generator.rb
56
76
  - sequel_generators/model/templates
57
- - sequel_generators/model/USAGE
58
77
  - sequel_generators/model/templates/sequel_model_template.erb
78
+ - sequel_generators/model/USAGE
79
+ - sequel_generators/resource_controller
59
80
  - sequel_generators/resource_controller/resource_controller_generator.rb
60
81
  - sequel_generators/resource_controller/templates
61
- - sequel_generators/resource_controller/USAGE
62
82
  - sequel_generators/resource_controller/templates/controller.rb
63
83
  - sequel_generators/resource_controller/templates/edit.html.erb
64
84
  - sequel_generators/resource_controller/templates/helper.rb
65
85
  - sequel_generators/resource_controller/templates/index.html.erb
66
86
  - sequel_generators/resource_controller/templates/new.html.erb
67
87
  - sequel_generators/resource_controller/templates/show.html.erb
68
- test_files: []
69
-
88
+ - sequel_generators/resource_controller/USAGE
89
+ has_rdoc: true
90
+ homepage: http://merb-plugins.rubyforge.org/merb_sequel/
91
+ post_install_message:
70
92
  rdoc_options: []
71
93
 
72
- extra_rdoc_files:
73
- - README
74
- - LICENSE
75
- - TODO
76
- executables: []
77
-
78
- extensions: []
79
-
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
80
108
  requirements: []
81
109
 
82
- dependencies:
83
- - !ruby/object:Gem::Dependency
84
- name: merb
85
- version_requirement:
86
- version_requirements: !ruby/object:Gem::Version::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: 0.4.0
91
- version:
110
+ rubyforge_project:
111
+ rubygems_version: 1.0.1
112
+ signing_key:
113
+ specification_version: 2
114
+ summary: Merb plugin that provides support for Sequel and Sequel::Model
115
+ test_files: []
116
+