Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use datastore from rack_datamapper

  • Loading branch information...
commit e9d35fef9e9e7951a819e0f14d2d4f876414f3de 1 parent cf71c32
@mkristian authored
View
4 Rakefile
@@ -9,10 +9,10 @@ require 'pathname'
require './lib/datamapper4rails/version.rb'
-Hoe.new('datamapper4rail-ixtlan', Datamapper4rails::VERSION) do |p|
+Hoe.new('datamapper4rail', Datamapper4rails::VERSION) do |p|
p.rubyforge_name = 'datamapper4rail'
p.developer('mkristian', 'm.kristian@web.de')
- p.extra_deps = ['slf4r']
+ p.extra_deps = ['slf4r', 'rack_datamapper', 'rails_datamapper']
p.remote_rdoc_dir = '' # Release to root
end
View
14 lib/datamapper4rails.rb
@@ -1 +1,13 @@
-require 'datamapper4rails/database_config'
+#require 'datamapper4rails/database_config'
+require 'rails_datamapper'
+
+# keep this here until rails_datamapper has it included
+module DataMapper
+ module Validate
+ class ValidationErrors
+ def count
+ size
+ end
+ end
+ end
+end
View
54 lib/datamapper4rails/database_config.rb
@@ -1,54 +0,0 @@
-require 'dm-core'
-
-def config_file()
- RAILS_ROOT + "/config/database.yml"
-end
-
-def create_connection()
- conf = config.dup
- if repositories = conf.delete(:repositories)
- repositories.each do |repo, conf|
- ::DataMapper.setup(repo, conf) unless conf.empty?
- end
- else
- ::DataMapper.setup(:default, conf) unless conf.empty?
- end
-end
-
-def get_config_for_environment
- if hash = full_config[RAILS_ENV]
- symbolize_keys(hash)
- elsif hash = full_config[RAILS_ENV.to_sym]
- hash
- else
- raise ArgumentError, "missing environment '#{RAILS_ENV}' in config file #{config_file}"
- end
-end
-
-def full_config
- @full_config ||= YAML::load(ERB.new(IO.read(config_file)).result)
-end
-
-def config
- @config ||= get_config_for_environment
-end
-
-def symbolize_keys(h)
- config = {}
-
- h.each do |k, v|
- if k == 'port'
- config[k.to_sym] = v.to_i
- elsif k == 'adapter' && v == 'postgresql'
- config[k.to_sym] = 'postgres'
- elsif v.is_a?(Hash)
- config[k.to_sym] = symbolize_keys(v)
- else
- config[k.to_sym] = v
- end
- end
-
- config
-end
-
-create_connection()
View
60 lib/datamapper4rails/datamapper_store.rb
@@ -1,4 +1,6 @@
require 'dm-core'
+require 'rack_datamapper/session/datamapper'
+require 'active_support'
module ActionController
module Session
@@ -6,63 +8,21 @@ class DatamapperStore < AbstractStore
def initialize(app, options = {})
super
- if options.delete(:cache)
- @@cache = {}
- else
- @@cache = nil unless self.class.class_variable_defined? :@@cache
+ id_generator = Proc.new do
+ ::ActiveSupport::SecureRandom.hex(16)
end
- @@session_class = options.delete(:session_class) || ::DatamapperStore::Session unless (self.class.class_variable_defined? :@@session_class and @@session_class)
+ @store = ::DataMapper::Session::Abstract::Store.new(app, options, id_generator)
+ @options = options
end
-
+
private
def get_session(env, sid)
- sid ||= generate_sid
- session =
- if @@cache
- @@cache[sid] || @@session_class.get(sid)
- else
- @@session_class.get(sid)
- end
- [sid, session.nil? ? {} : session.data]
+ @store.get_session(env, sid)
end
-
+
def set_session(env, sid, session_data)
- session =
- if @@cache
- @@cache[sid] || @@session_class.get(sid)
- else
- @@session_class.get(sid)
- end || @@session_class.new(:session_id => sid)
- session.data = session_data || {}
- session.updated_at = Time.now if session.dirty?
- @@cache[sid] = session if @@cache
- session.save
+ @store.set_session(env, sid, session_data, @options)
end
end
end
end
-
-module DatamapperStore
- class Session
-
- include ::DataMapper::Resource
-
- def self.name
- "session"
- end
-
- property :session_id, String, :key => true
-
- property :data, Text, :nullable => false, :default => ::Base64.encode64(Marshal.dump({}))
-
- property :updated_at, DateTime, :nullable => true, :index => true
-
- def data=(data)
- attribute_set(:data, ::Base64.encode64(Marshal.dump(data)))
- end
-
- def data
- Marshal.load(::Base64.decode64(attribute_get(:data)))
- end
- end
-end
View
124 spec/datamapper_store_spec.rb
@@ -2,126 +2,22 @@
require 'spec_helper'
require 'datamapper4rails/datamapper_store'
-describe DatamapperStore do
-
- before :each do
- load 'lib/datamapper4rails/datamapper_store.rb'
- ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
- end
-
- it 'should initialize with no cache and default session class' do
- store = ActionController::Session::DatamapperStore.new(nil)
- store.class.send(:class_variable_get, :@@cache).should be_nil
- store.class.send(:class_variable_get, :@@session_class).should == DatamapperStore::Session
-
- store = ActionController::Session::DatamapperStore.new(nil, :session_class => :some_class)
- store.class.send(:class_variable_get, :@@cache).should be_nil
- store.class.send(:class_variable_get, :@@session_class).should == DatamapperStore::Session
- end
-
- it 'should initialize with cache and default session class' do
- store = ActionController::Session::DatamapperStore.new(nil, :cache => true)
- store.class.send(:class_variable_get, :@@cache).instance_of?(Hash).should be_true
-
- store = ActionController::Session::DatamapperStore.new(nil)
- store.class.send(:class_variable_get, :@@cache).instance_of?(Hash).should be_true
- end
-
- it 'should initialize with custom session class' do
- store = ActionController::Session::DatamapperStore.new(nil, :session_class => Session)
- store.class.send(:class_variable_get, :@@session_class).should == Session
-
- store = ActionController::Session::DatamapperStore.new(nil, :session_class => :some_class)
- store.class.send(:class_variable_get, :@@session_class).should == Session
- end
-end
-
-describe 'DatamapperStore without cache' do
+describe ActionController::Session::DatamapperStore do
def mock_session(stubs={})
@mock_session ||= mock(Session, stubs)
end
-
+
before :each do
- load 'lib/datamapper4rails/datamapper_store.rb'
- ActionController::Session::DatamapperStore.send(:class_variable_set, :@@cache, nil)
- ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
@store = ActionController::Session::DatamapperStore.new(nil, :session_class => Session)
end
-
- it 'should get the session data' do
- Session.stub!(:get)
- @store.send(:get_session, nil, "sid").should == ["sid",{}]
- Session.stub!(:get).and_return(mock_session)
- mock_session.should_receive(:data).and_return({:id => "id"})
- @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
- end
-
- it 'should set the session data on new session' do
- Session.should_receive(:get)
- Session.should_receive(:new).with(:session_id => "sid").and_return(mock_session)
- mock_session.should_receive(:data=).with({})
- mock_session.should_receive(:dirty?).and_return(true)
- mock_session.should_receive(:updated_at=)
- mock_session.should_receive(:save).and_return(true)
- @store.send(:set_session, nil, "sid", {}).should == true
- end
-
- it 'should set the session data' do
- Session.should_receive(:get).and_return(mock_session)
- mock_session.should_receive(:data=).with({})
- mock_session.should_receive(:dirty?).and_return(true)
- mock_session.should_receive(:updated_at=)
- mock_session.should_receive(:save).and_return(true)
- @store.send(:set_session, nil, "sid", {}).should == true
- end
-end
-
-describe 'DatamapperStore with cache' do
-
- def mock_session(stubs={})
- @mock_session ||= mock(Session, stubs)
- end
-
- before :each do
- load 'lib/datamapper4rails/datamapper_store.rb'
- ActionController::Session::DatamapperStore.send(:class_variable_set, :@@cache, nil)
- ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
- @store = ActionController::Session::DatamapperStore.new(nil, :cache => true, :session_class => Session)
- end
-
- it 'should get the session data from storage' do
- Session.stub!(:get)
- @store.send(:get_session, nil, "sid").should == ["sid",{}]
- Session.stub!(:get).and_return(mock_session)
- mock_session.should_receive(:data).and_return({:id => "id"})
- @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
- end
-
- it 'should get the session data from cache' do
- ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"] = mock_session
- mock_session.should_receive(:data).and_return({:id => "id"})
- @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
- end
-
- it 'should set the session data on new session' do
- Session.should_receive(:get)
- Session.should_receive(:new).with(:session_id => "sid").and_return(mock_session)
- mock_session.should_receive(:data=).with({})
- mock_session.should_receive(:dirty?).and_return(true)
- mock_session.should_receive(:updated_at=)
- mock_session.should_receive(:save).and_return(true)
- @store.send(:set_session, nil, "sid", {}).should == true
- ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"].should == mock_session
-
- end
-
- it 'should set the session data' do
- Session.should_receive(:get).and_return(mock_session)
- mock_session.should_receive(:data=).with({})
- mock_session.should_receive(:dirty?).and_return(false)
- mock_session.should_receive(:save).and_return(true)
- @store.send(:set_session, nil, "sid", {}).should == true
- ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"].should == mock_session
+
+ it 'should create a new session' do
+ Session.should_receive(:create).and_return(mock_session)
+ mock_session.should_receive(:data).and_return({})
+ result = @store.send(:get_session, {}, nil)
+ result[0].should_not be_nil
+ result[1].should == {}
end
+
end
View
13 spec/spec_helper.rb
@@ -17,5 +17,18 @@ class Session
include ::DataMapper::Resource
property :session_id, String, :key => true
+
+ property :updated_at, DateTime
+ property :data, Text, :nullable => false, :default => ::Base64.encode64(Marshal.dump({}))
+
+ def data=(data)
+ attribute_set(:data, ::Base64.encode64(Marshal.dump(data)))
+ end
+
+ def data
+ Marshal.load(::Base64.decode64(attribute_get(:data)))
+ end
end
+
+DataMapper.setup(:default, {:adapter => 'in_memory'})
Please sign in to comment.
Something went wrong with that request. Please try again.