merb_sequel 0.0.4

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 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,4 @@
1
+ merb_sequel
2
+ =================
3
+
4
+ A plugin for the Merb framework that provides ....
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ PLUGIN = "merb_sequel"
5
+ NAME = "merb_sequel"
6
+ VERSION = "0.0.4"
7
+ AUTHOR = "Duane Johnson"
8
+ EMAIL = "canadaduane@gmail.com"
9
+ HOMEPAGE = "http://merb-plugins.rubyforge.org/merb_sequel/"
10
+ SUMMARY = "Merb plugin that provides support for the Sequel database object"
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.requirements << 'merb'
24
+ s.require_path = 'lib'
25
+ s.autorequire = PLUGIN
26
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ 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_sequel.rb
5
+ Add your Merb rake tasks to lib/merb_sequel/merbtasks.rb
@@ -0,0 +1,82 @@
1
+ require 'fileutils'
2
+
3
+ module Merb
4
+ module Orms
5
+ module Sequel
6
+ class << self
7
+ def config_file() MERB_ROOT / "config" / "database.yml" end
8
+ def sample_dest() MERB_ROOT / "config" / "database.sample.yml" end
9
+ def sample_source() File.dirname(__FILE__) / "database.sample.yml" end
10
+
11
+ def copy_sample_config
12
+ FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
13
+ end
14
+
15
+ def config
16
+ @config ||=
17
+ begin
18
+ # Convert string keys to symbols
19
+ full_config = Erubis.load_yaml_file(config_file)
20
+ config = (Merb::Plugins.config[:merb_sequel] = {})
21
+ (full_config[MERB_ENV.to_sym] || full_config[MERB_ENV]).each { |k, v| config[k.to_sym] = v }
22
+ config
23
+ end
24
+ end
25
+
26
+ # Database connects as soon as the gem is loaded
27
+ def connect
28
+ 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
64
+ else
65
+ 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."
68
+ exit(1)
69
+ end
70
+ end
71
+
72
+ # Registering this ORM lets the user choose sequel as a session store
73
+ # in merb.yml's session_store: option.
74
+ def register_session_type
75
+ Merb::Server.register_session_type("sequel",
76
+ "merb/session/sequel_session",
77
+ "Using Sequel database sessions")
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,17 @@
1
+ ---
2
+ # This is a sample database file for the Sequel ORM
3
+ :development: &defaults
4
+ :adapter: mysql
5
+ :database: sample_development
6
+ :username: teh_user
7
+ :password: secrets
8
+ :host: localhost
9
+ :socket: /tmp/mysql.sock
10
+
11
+ :test:
12
+ <<: *defaults
13
+ :database: sample_test
14
+
15
+ :production:
16
+ <<: *defaults
17
+ :database: sample_production
@@ -0,0 +1,14 @@
1
+ class AddSessionsTable < Sequel::Migration
2
+ def up
3
+ create_table :sessions do
4
+ primary_key :id
5
+ varchar :session_id, :size => 32, :unique => true
6
+ timestamp :created_at
7
+ text :data
8
+ end
9
+ end
10
+
11
+ def down
12
+ execute 'DROP TABLE sessions'
13
+ end
14
+ end
@@ -0,0 +1,129 @@
1
+ module Merb
2
+ module SessionMixin
3
+ def setup_session
4
+ MERB_LOGGER.info("Setting up session")
5
+ before = cookies[_session_id_key]
6
+ @_session, cookies[_session_id_key] = Merb::SequelSession.persist(cookies[_session_id_key])
7
+ @_fingerprint = Marshal.dump(@_session.data).hash
8
+ @_new_cookie = cookies[_session_id_key] != before
9
+ end
10
+
11
+ def finalize_session
12
+ MERB_LOGGER.info("Finalize session")
13
+ @_session.save if @_fingerprint != Marshal.dump(@_session.data).hash
14
+ set_cookie(_session_id_key, @_session.values[:session_id], _session_expiry) if (@_new_cookie || @_session.needs_new_cookie)
15
+ end
16
+ end
17
+
18
+ table_name = (Merb::Plugins.config[:sequel][:session_table_name] || "sessions")
19
+
20
+ class SequelSession < Sequel::Model(table_name.to_sym)
21
+ set_schema do
22
+ primary_key :id
23
+ varchar :session_id
24
+ varchar :data
25
+ end
26
+
27
+ attr_accessor :needs_new_cookie
28
+
29
+ class << self
30
+ # Generates a new session ID and creates a row for the new session in the database.
31
+ def generate
32
+ create(:session_id => Merb::SessionMixin::rand_uuid, :data => marshal({}))
33
+ end
34
+
35
+ # Gets the existing session based on the <tt>session_id</tt> available in cookies.
36
+ # If none is found, generates a new session.
37
+ def persist(session_id)
38
+ if session_id
39
+ session = find_by_session_id(session_id)
40
+ end
41
+ unless session
42
+ session = generate
43
+ end
44
+ [session, session.values[:session_id]]
45
+ end
46
+
47
+ # Don't try to reload ARStore::Session in dev mode.
48
+ def reloadable? #:nodoc:
49
+ false
50
+ end
51
+
52
+ def data_column_size_limit
53
+ 255
54
+ end
55
+
56
+ def marshal(data) Base64.encode64(Marshal.dump(data)) if data end
57
+ def unmarshal(data)
58
+ Marshal.load(Base64.decode64(data)) if data
59
+ end
60
+
61
+ alias :create_table! :create_table
62
+ alias :drop_table! :drop_table
63
+ end
64
+
65
+ # Regenerate the Session ID
66
+ def regenerate
67
+ update_attributes(:session_id => Merb::SessionMixin::rand_uuid)
68
+ self.needs_new_cookie = true
69
+ end
70
+
71
+ # Recreates the cookie with the default expiration time
72
+ # Useful during log in for pushing back the expiration date
73
+ def refresh_expiration
74
+ self.needs_new_cookie = true
75
+ end
76
+
77
+ # Lazy-delete of session data
78
+ def delete
79
+ self.data = {}
80
+ end
81
+
82
+ def [](key)
83
+ data[key]
84
+ end
85
+
86
+ def []=(key, val)
87
+ data[key] = val
88
+ end
89
+
90
+ # Lazy-unmarshal session state.
91
+ def data
92
+ @data ||= self.class.unmarshal(@values[:data]) || {}
93
+ end
94
+
95
+ # Has the session been loaded yet?
96
+ def loaded?
97
+ !! @data
98
+ end
99
+
100
+ private
101
+ attr_writer :data
102
+
103
+ before_save do # marshal_data!
104
+ # return false if !loaded?
105
+ @values[:data] = self.class.marshal(self.data)
106
+ end
107
+
108
+ # Ensures that the data about to be stored in the database is not
109
+ # larger than the data storage column. Raises
110
+ # ActionController::SessionOverflowError.
111
+ # before_save do # raise_on_session_data_overflow!
112
+ # return false if !loaded?
113
+ # limit = self.class.data_column_size_limit
114
+ # if loaded? and limit and read_attribute(@@data_column_name).size > limit
115
+ # raise MerbController::SessionOverflowError
116
+ # end
117
+ # end
118
+ end
119
+
120
+ unless Sequel::Model.db.table_exists?(table_name.to_sym)
121
+ puts "Warning: The database did not contain a '#{table_name}' table for sessions."
122
+
123
+ SequelSession.class_eval do
124
+ create_table unless table_exists?
125
+ end
126
+
127
+ puts "Created sessions table."
128
+ end
129
+ end
@@ -0,0 +1,12 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+ if Merb::Server.app_loaded?
4
+ puts "Warning: The merb_sequel gem must be loaded before the application"
5
+ else
6
+ require "merb/orms/sequel/connection"
7
+ Merb::Orms::Sequel.connect
8
+ Merb::Orms::Sequel.register_session_type
9
+ end
10
+
11
+ Merb::Plugins.add_rakefiles "merbtasks"
12
+ end
data/lib/merbtasks.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'fileutils'
2
+
3
+ namespace :sequel do
4
+ namespace :db do
5
+ desc "Perform migration using migrations in schema/migrations"
6
+ task :migrate => :merb_env do
7
+ Sequel::Migrator.apply(Merb::Orms::Sequel.connect, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
8
+ end
9
+ end
10
+
11
+ namespace :sessions do
12
+ desc "Creates session migration"
13
+ task :create => :merb_env do
14
+ dest = File.join(MERB_ROOT, "schema", "migrations","001_add_sessions_table.rb")
15
+ source = File.join(File.dirname(__FILE__), "merb", "session","001_add_sessions_table.rb")
16
+
17
+ FileUtils.cp source, dest unless File.exists?(dest)
18
+ end
19
+
20
+ desc "Clears sessions"
21
+ task :clear => :merb_env do
22
+ table_name = (Merb::Plugins.config[:sequel][:session_table_name] || "sessions")
23
+
24
+ Merb::Orms::Sequel.connect.execute("DELETE FROM #{table_name}")
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_sequel" 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,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.1
3
+ specification_version: 1
4
+ name: merb_sequel
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.4
7
+ date: 2007-09-24 00:00:00 -07: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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Duane Johnson
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - TODO
36
+ - lib/merb
37
+ - lib/merb_sequel.rb
38
+ - lib/merbtasks.rb
39
+ - lib/merb/orms
40
+ - lib/merb/session
41
+ - lib/merb/orms/sequel
42
+ - lib/merb/orms/sequel/connection.rb
43
+ - lib/merb/orms/sequel/database.sample.yml
44
+ - lib/merb/session/001_add_sessions_table.rb
45
+ - lib/merb/session/sequel_session.rb
46
+ - specs/merb_sequel_spec.rb
47
+ - specs/spec_helper.rb
48
+ test_files: []
49
+
50
+ rdoc_options: []
51
+
52
+ extra_rdoc_files:
53
+ - README
54
+ - LICENSE
55
+ - TODO
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ requirements:
61
+ - merb
62
+ dependencies: []
63
+