merb_sequel 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ GEM_EMAIL = "wayneeseguin@gmail.com, lancecarlson@gmail.com"
18
18
 
19
19
  GEM_NAME = "merb_sequel"
20
20
  PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
21
- GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.5") + PKG_BUILD
21
+ GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.6") + PKG_BUILD
22
22
 
23
23
  RELEASE_NAME = "REL #{GEM_VERSION}"
24
24
 
@@ -36,7 +36,7 @@ spec = Gem::Specification.new do |s|
36
36
  s.author = GEM_AUTHOR
37
37
  s.email = GEM_EMAIL
38
38
  s.homepage = PROJECT_URL
39
- s.add_dependency("merb-core", ">= 0.9.5")
39
+ s.add_dependency("merb-core", ">= 0.9.6")
40
40
  s.add_dependency("sequel", ">= 1.4.0")
41
41
  s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,sequel_generators}/**/*")
42
42
  end
@@ -50,13 +50,13 @@ end
50
50
  ##############################################################################
51
51
  desc "Install the gem"
52
52
  task :install => [:package] do
53
- sh %{#{sudo} gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}
53
+ sh install_command(GEM_NAME, GEM_VERSION)
54
54
  end
55
55
 
56
56
  namespace :jruby do
57
57
  desc "Run :package and install the resulting .gem with jruby"
58
58
  task :install => :package do
59
- sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
59
+ sh jinstall_command(GEM_NAME, GEM_VERSION)
60
60
  end
61
61
  end
62
62
 
@@ -1,4 +1,5 @@
1
1
  require "fileutils"
2
+ require "sequel"
2
3
 
3
4
  module Merb
4
5
  module Orms
@@ -15,9 +16,7 @@ module Merb
15
16
  end
16
17
 
17
18
  def config
18
-
19
- @config ||=
20
- begin
19
+ @config ||= begin
21
20
  # Convert string keys to symbols
22
21
  full_config = Erubis.load_yaml_file(config_file)
23
22
  config = (Merb::Plugins.config[:merb_sequel] = {})
@@ -26,14 +25,10 @@ module Merb
26
25
  end
27
26
  config
28
27
  end
29
-
30
28
  end
31
29
 
32
30
  # Database connects as soon as the gem is loaded
33
31
  def connect
34
-
35
- require "sequel"
36
-
37
32
  if File.exists?(config_file)
38
33
  Merb.logger.info!("Connecting to the '#{config[:database]}' database on '#{config[:host]}' using '#{config[:adapter]}' ...")
39
34
  connection = ::Sequel.connect(config_options(config))
@@ -46,11 +41,9 @@ module Merb
46
41
  Merb.logger.error! "A sample file was created called config/database.yml.sample for you to copy and edit."
47
42
  exit(1)
48
43
  end
49
-
50
44
  end
51
45
 
52
46
  def config_options(config = {})
53
-
54
47
  options = {}
55
48
  options[:adapter] = (config[:adapter] || "sqlite")
56
49
  options[:host] = (config[:host] || "localhost")
@@ -63,16 +56,6 @@ module Merb
63
56
  options[:logger] = Merb.logger
64
57
  options
65
58
  end
66
-
67
- # Registering this ORM lets the user choose sequel as a session store
68
- # in merb.yml's session_store: option.
69
- def register_session_type
70
- Merb.register_session_type(
71
- "sequel",
72
- "merb/session/sequel_session",
73
- "Using Sequel database sessions"
74
- )
75
- end
76
59
 
77
60
  end
78
61
 
@@ -1,156 +1,106 @@
1
- require "base64"
1
+ require 'sequel'
2
+ require 'merb-core/dispatch/session'
3
+ require 'base64'
2
4
 
3
5
  module Merb
4
6
 
5
- module SessionMixin
6
- def setup_session
7
- before = cookies[_session_id_key]
8
- request.session, cookies[_session_id_key] = Merb::SequelSession.persist(cookies[_session_id_key])
9
- @_fingerprint = Marshal.dump(request.session.data).hash
10
- @_new_cookie = cookies[_session_id_key] != before
11
- end
12
-
13
- def finalize_session
14
- request.session.save if @_fingerprint != Marshal.dump(request.session.data).hash
15
- set_cookie(_session_id_key, request.session.values[:session_id], Time.now + _session_expiry) if (@_new_cookie || request.session.needs_new_cookie)
16
- end
17
-
18
- def session_store_type
19
- "sequel"
20
- end
21
-
22
- end
23
-
24
7
  table_name = (Merb::Plugins.config[:merb_sequel][:session_table_name] || "sessions")
25
8
 
26
- class SequelSession < Sequel::Model(table_name.to_sym)
9
+ # Sessions stored in Sequel model.
10
+ #
11
+ # To use Sequel based sessions add the following to config/init.rb:
12
+ #
13
+ # Merb::Config[:session_store] = 'sequel'
14
+
15
+ class SequelSessionStore < Sequel::Model(table_name.to_sym)
27
16
 
28
17
  set_schema do
29
18
  primary_key :id
30
19
  varchar :session_id
31
- varchar :data
20
+ text :data
32
21
  timestamp :created_at
33
22
  end
34
23
 
35
- attr_accessor :needs_new_cookie
36
-
37
24
  class << self
38
- # Generates a new session ID and creates a row for the new session in the database.
39
- def generate
40
- create(:session_id => Merb::SessionMixin::rand_uuid,
41
- :data => marshal({}), :created_at => Time.now)
25
+
26
+ # ==== Parameters
27
+ # session_id<String>:: ID of the session to retrieve.
28
+ #
29
+ # ==== Returns
30
+ # ContainerSession:: The session corresponding to the ID.
31
+ def retrieve_session(session_id)
32
+ if item = find(:session_id => session_id)
33
+ item.data
34
+ end
42
35
  end
43
36
 
44
- # Gets the existing session based on the <tt>session_id</tt> available in cookies.
45
- # If none is found, generates a new session.
46
- def persist(session_id)
47
- if session_id
48
- session = find(:session_id => session_id)
37
+ # ==== Parameters
38
+ # session_id<String>:: ID of the session to set.
39
+ # data<ContainerSession>:: The session to set.
40
+ def store_session(session_id, data)
41
+ if item = find(:session_id => session_id)
42
+ item.update(:data => data)
43
+ else
44
+ create(:session_id => session_id, :data => data, :created_at => Time.now)
49
45
  end
50
- unless session
51
- session = generate
52
- end
53
- [session, session.values[:session_id]]
54
46
  end
55
47
 
56
- # Don't try to reload ARStore::Session in dev mode.
57
- def reloadable?
58
- false
48
+ # ==== Parameters
49
+ # session_id<String>:: ID of the session to delete.
50
+ def delete_session(session_id)
51
+ if item = find(:session_id => session_id)
52
+ item.delete
53
+ end
59
54
  end
60
-
55
+
56
+ # ==== Returns
57
+ # Integer:: The maximum length of the 'data' column.
61
58
  def data_column_size_limit
62
- 255
63
- end
64
-
65
- def marshal(data)
66
- Base64.encode64(Marshal.dump(data)) if data
67
- end
68
-
69
- def unmarshal(data)
70
- Marshal.load(Base64.decode64(data)) if data
59
+ 512 # TODO - figure out how much space we actually have
71
60
  end
72
61
 
73
62
  alias :create_table! :create_table
74
63
  alias :drop_table! :drop_table
75
64
  end
76
65
 
77
- # Regenerate the Session ID
78
- def regenerate
79
- update_attributes(:session_id => Merb::SessionMixin::rand_uuid)
80
- self.needs_new_cookie = true
81
- end
82
-
83
- # Recreates the cookie with the default expiration time
84
- # Useful during log in for pushing back the expiration date
85
- def refresh_expiration
86
- self.needs_new_cookie = true
87
- end
88
-
89
- # Lazy-delete of session data
90
- def delete(key = nil)
91
- key ? self.data.delete(key) : self.data.clear
92
- end
93
-
94
- def [](key)
95
- data[key]
96
- end
97
-
98
- def []=(key, val)
99
- data[key] = val
100
- end
101
-
102
- def empty?
103
- data.empty?
104
- end
105
-
106
- def each(&b)
107
- data.each(&b)
66
+ # Lazy-unserialize session state.
67
+ def data
68
+ @data ||= (@values[:data] ? Marshal.load(@values[:data]) : {})
108
69
  end
109
70
 
110
- def each_with_index(&b)
111
- data.each_with_index(&b)
112
- end
113
-
114
- # Lazy-unmarshal session state.
115
- def data
116
- @data ||= self.class.unmarshal(@values[:data]) || {}
71
+ # Virtual attribute writer - override.
72
+ def data=(hsh)
73
+ @data = hsh if hsh.is_a?(Hash)
117
74
  end
118
75
 
119
76
  # Has the session been loaded yet?
120
77
  def loaded?
121
- !! @data
78
+ !!@data
122
79
  end
123
80
 
124
- private
125
-
126
- attr_writer :data
127
-
128
- before_save do # marshal_data!
129
- # return false if !loaded?
130
- @values[:data] = self.class.marshal(self.data)
81
+ before_save do
82
+ @values[:data] = Marshal.dump(self.data)
83
+ if @values[:data].size > self.class.data_column_size_limit
84
+ raise Merb::SessionMixin::SessionOverflow
85
+ end
131
86
  end
132
-
133
- # Ensures that the data about to be stored in the database is not
134
- # larger than the data storage column. Raises
135
- # ActionController::SessionOverflowError.
136
- # before_save do # raise_on_session_data_overflow!
137
- # return false if !loaded?
138
- # limit = self.class.data_column_size_limit
139
- # if loaded? and limit and read_attribute(@@data_column_name).size > limit
140
- # raise MerbController::SessionOverflowError
141
- # end
142
- # end
143
87
 
144
88
  end
145
89
 
146
90
  unless Sequel::Model.db.table_exists?(table_name.to_sym)
147
91
  puts "Warning: The database did not contain a '#{table_name}' table for sessions."
148
-
149
- SequelSession.class_eval do
150
- create_table unless table_exists?
151
- end
152
-
92
+ SequelSessionStore.class_eval { create_table unless table_exists? }
153
93
  puts "Created sessions table."
154
94
  end
95
+
96
+ class SequelSession < SessionStoreContainer
97
+
98
+ # The session store type
99
+ self.session_store_type = :sequel
100
+
101
+ # The store object is the model class itself
102
+ self.store = SequelSessionStore
103
+
104
+ end
155
105
 
156
106
  end
@@ -9,7 +9,10 @@ if defined?(Merb::Plugins)
9
9
 
10
10
  def self.run
11
11
  Merb::Orms::Sequel.connect
12
- Merb::Orms::Sequel.register_session_type
12
+ if Merb::Config.session_stores.include?(:sequel)
13
+ Merb.logger.debug "Using Sequel sessions"
14
+ require File.join(File.dirname(__FILE__) / "merb" / "session" / "sequel_session")
15
+ end
13
16
  end
14
17
 
15
18
  end
@@ -0,0 +1,46 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'merb-core'
3
+ require 'merb-core/test'
4
+ require 'merb-core/test/helpers'
5
+
6
+ Merb::BootLoader.before_app_loads do
7
+ require 'sequel'
8
+ DB = Sequel.sqlite
9
+ require "merb/session/sequel_session"
10
+ end
11
+
12
+ Merb.start :environment => 'test', :adapter => 'runner', :session_store => 'sequel'
13
+
14
+ Spec::Runner.configure do |config|
15
+ config.include Merb::Test::RequestHelper
16
+ end
17
+
18
+ # Load up the shared specs from merb-core
19
+ if (gem_spec = Gem.source_index.search('merb-core').last) &&
20
+ gem_spec.files.include?('spec/public/session/controllers/sessions.rb')
21
+ require gem_spec.full_gem_path / 'spec/public/session/controllers/sessions.rb'
22
+ require gem_spec.full_gem_path / 'spec/public/session/session_spec.rb'
23
+ end
24
+
25
+ describe Merb::SequelSession do
26
+
27
+ before do
28
+ @session_class = Merb::SequelSession
29
+ @session = @session_class.generate
30
+ end
31
+
32
+ it_should_behave_like "All session-store backends"
33
+
34
+ it "should have a session_store_type class attribute" do
35
+ @session.class.session_store_type.should == :sequel
36
+ end
37
+
38
+ end
39
+
40
+ describe Merb::SequelSession, "mixed into Merb::Controller" do
41
+
42
+ before(:all) { @session_class = Merb::SequelSession }
43
+
44
+ it_should_behave_like "All session-stores mixed into Merb::Controller"
45
+
46
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb_sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wayne E. Seguin, Lance Carlson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-21 00:00:00 +03:00
12
+ date: 2008-09-08 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.5
23
+ version: 0.9.6
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sequel
@@ -85,6 +85,7 @@ files:
85
85
  - lib/merb_sequel
86
86
  - lib/merb_sequel/merbtasks.rb
87
87
  - lib/merb_sequel.rb
88
+ - specs/merb_sequel_session_spec.rb
88
89
  - specs/merb_sequel_spec.rb
89
90
  - specs/spec_helper.rb
90
91
  has_rdoc: true