Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

use datastore from rack_datamapper

  • Loading branch information...
commit e9d35fef9e9e7951a819e0f14d2d4f876414f3de 1 parent cf71c32
Christian Meier authored
4  Rakefile
@@ -9,10 +9,10 @@ require 'pathname'
9 9
 
10 10
 require './lib/datamapper4rails/version.rb'
11 11
 
12  
-Hoe.new('datamapper4rail-ixtlan', Datamapper4rails::VERSION) do |p|
  12
+Hoe.new('datamapper4rail', Datamapper4rails::VERSION) do |p|
13 13
   p.rubyforge_name = 'datamapper4rail'
14 14
   p.developer('mkristian', 'm.kristian@web.de')
15  
-  p.extra_deps = ['slf4r']
  15
+  p.extra_deps = ['slf4r', 'rack_datamapper', 'rails_datamapper']
16 16
   p.remote_rdoc_dir = '' # Release to root
17 17
 end
18 18
 
14  lib/datamapper4rails.rb
... ...
@@ -1 +1,13 @@
1  
-require 'datamapper4rails/database_config'
  1
+#require 'datamapper4rails/database_config'
  2
+require 'rails_datamapper'
  3
+
  4
+# keep this here until rails_datamapper has it included
  5
+module DataMapper
  6
+  module Validate
  7
+    class ValidationErrors
  8
+      def count
  9
+        size
  10
+      end
  11
+    end
  12
+  end
  13
+end
54  lib/datamapper4rails/database_config.rb
... ...
@@ -1,54 +0,0 @@
1  
-require 'dm-core'
2  
-
3  
-def config_file()
4  
-  RAILS_ROOT + "/config/database.yml"
5  
-end
6  
-
7  
-def create_connection()
8  
-  conf = config.dup
9  
-  if repositories = conf.delete(:repositories)
10  
-    repositories.each do |repo, conf|
11  
-      ::DataMapper.setup(repo, conf) unless conf.empty?
12  
-    end
13  
-  else
14  
-    ::DataMapper.setup(:default, conf) unless conf.empty?
15  
-  end
16  
-end
17  
-
18  
-def get_config_for_environment
19  
-  if hash = full_config[RAILS_ENV]
20  
-    symbolize_keys(hash)
21  
-  elsif hash = full_config[RAILS_ENV.to_sym]
22  
-    hash
23  
-  else
24  
-    raise ArgumentError, "missing environment '#{RAILS_ENV}' in config file #{config_file}"
25  
-  end
26  
-end
27  
-
28  
-def full_config
29  
-  @full_config ||= YAML::load(ERB.new(IO.read(config_file)).result)
30  
-end
31  
-
32  
-def config
33  
-  @config ||= get_config_for_environment
34  
-end
35  
-
36  
-def symbolize_keys(h)
37  
-  config = {}
38  
-
39  
-  h.each do |k, v|
40  
-    if k == 'port'
41  
-      config[k.to_sym] = v.to_i
42  
-    elsif k == 'adapter' && v == 'postgresql'
43  
-      config[k.to_sym] = 'postgres'
44  
-    elsif v.is_a?(Hash)
45  
-      config[k.to_sym] = symbolize_keys(v)
46  
-    else
47  
-      config[k.to_sym] = v
48  
-    end
49  
-  end
50  
-
51  
-  config
52  
-end
53  
-
54  
-create_connection()
60  lib/datamapper4rails/datamapper_store.rb
... ...
@@ -1,4 +1,6 @@
1 1
 require 'dm-core'
  2
+require 'rack_datamapper/session/datamapper'
  3
+require 'active_support'
2 4
 
3 5
 module ActionController
4 6
   module Session
@@ -6,63 +8,21 @@ class DatamapperStore < AbstractStore
6 8
 
7 9
       def initialize(app, options = {})
8 10
         super
9  
-        if options.delete(:cache)
10  
-          @@cache = {}
11  
-        else
12  
-          @@cache = nil unless self.class.class_variable_defined? :@@cache
  11
+        id_generator = Proc.new do
  12
+          ::ActiveSupport::SecureRandom.hex(16)
13 13
         end
14  
-        @@session_class = options.delete(:session_class) || ::DatamapperStore::Session unless (self.class.class_variable_defined? :@@session_class and @@session_class)
  14
+        @store = ::DataMapper::Session::Abstract::Store.new(app, options, id_generator)
  15
+        @options = options
15 16
       end
16  
-      
  17
+
17 18
       private
18 19
       def get_session(env, sid)
19  
-        sid ||= generate_sid
20  
-        session = 
21  
-          if @@cache
22  
-            @@cache[sid] || @@session_class.get(sid)
23  
-          else
24  
-            @@session_class.get(sid)
25  
-          end
26  
-        [sid, session.nil? ? {} : session.data]
  20
+        @store.get_session(env, sid)
27 21
       end
28  
-      
  22
+
29 23
       def set_session(env, sid, session_data)
30  
-        session = 
31  
-          if @@cache
32  
-            @@cache[sid] || @@session_class.get(sid)
33  
-          else
34  
-            @@session_class.get(sid)
35  
-          end || @@session_class.new(:session_id => sid)
36  
-        session.data = session_data || {}
37  
-        session.updated_at = Time.now if session.dirty?
38  
-        @@cache[sid] = session if @@cache
39  
-        session.save
  24
+        @store.set_session(env, sid, session_data, @options)
40 25
       end
41 26
     end
42 27
   end
43 28
 end
44  
-
45  
-module DatamapperStore
46  
-  class Session
47  
-    
48  
-    include ::DataMapper::Resource
49  
-    
50  
-    def self.name
51  
-      "session"
52  
-    end
53  
-    
54  
-    property :session_id, String, :key => true
55  
-    
56  
-    property :data, Text, :nullable => false, :default => ::Base64.encode64(Marshal.dump({}))
57  
-    
58  
-    property :updated_at, DateTime, :nullable => true, :index => true
59  
-    
60  
-    def data=(data)
61  
-      attribute_set(:data, ::Base64.encode64(Marshal.dump(data)))
62  
-    end
63  
-
64  
-    def data
65  
-      Marshal.load(::Base64.decode64(attribute_get(:data)))
66  
-    end
67  
-  end
68  
-end
124  spec/datamapper_store_spec.rb
@@ -2,126 +2,22 @@
2 2
 require 'spec_helper'
3 3
 require 'datamapper4rails/datamapper_store'
4 4
 
5  
-describe DatamapperStore do
6  
-
7  
-  before :each do
8  
-    load 'lib/datamapper4rails/datamapper_store.rb'
9  
-    ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
10  
-  end
11  
-
12  
-  it 'should initialize with no cache and default session class' do
13  
-    store = ActionController::Session::DatamapperStore.new(nil)
14  
-    store.class.send(:class_variable_get, :@@cache).should be_nil
15  
-    store.class.send(:class_variable_get, :@@session_class).should == DatamapperStore::Session
16  
-
17  
-    store = ActionController::Session::DatamapperStore.new(nil, :session_class => :some_class)
18  
-    store.class.send(:class_variable_get, :@@cache).should be_nil
19  
-    store.class.send(:class_variable_get, :@@session_class).should == DatamapperStore::Session
20  
-  end
21  
-
22  
-  it 'should initialize with cache and default session class' do
23  
-    store = ActionController::Session::DatamapperStore.new(nil, :cache => true)
24  
-    store.class.send(:class_variable_get, :@@cache).instance_of?(Hash).should be_true
25  
-
26  
-    store = ActionController::Session::DatamapperStore.new(nil)
27  
-    store.class.send(:class_variable_get, :@@cache).instance_of?(Hash).should be_true
28  
-  end
29  
-
30  
-  it 'should initialize with custom session class' do
31  
-    store = ActionController::Session::DatamapperStore.new(nil, :session_class => Session)
32  
-    store.class.send(:class_variable_get, :@@session_class).should == Session
33  
-
34  
-    store = ActionController::Session::DatamapperStore.new(nil, :session_class => :some_class)
35  
-    store.class.send(:class_variable_get, :@@session_class).should == Session
36  
-  end
37  
-end
38  
-
39  
-describe 'DatamapperStore without cache' do
  5
+describe ActionController::Session::DatamapperStore do
40 6
 
41 7
   def mock_session(stubs={})
42 8
     @mock_session ||= mock(Session, stubs)
43 9
   end
44  
-
  10
+  
45 11
   before :each do
46  
-    load 'lib/datamapper4rails/datamapper_store.rb'
47  
-    ActionController::Session::DatamapperStore.send(:class_variable_set, :@@cache, nil)
48  
-    ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
49 12
     @store = ActionController::Session::DatamapperStore.new(nil, :session_class => Session)
50 13
   end
51  
-
52  
-  it 'should get the session data' do
53  
-    Session.stub!(:get)
54  
-    @store.send(:get_session, nil, "sid").should == ["sid",{}]
55  
-    Session.stub!(:get).and_return(mock_session)
56  
-    mock_session.should_receive(:data).and_return({:id => "id"})
57  
-    @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
58  
-  end
59  
-
60  
-  it 'should set the session data on new session' do
61  
-    Session.should_receive(:get)
62  
-    Session.should_receive(:new).with(:session_id => "sid").and_return(mock_session)
63  
-    mock_session.should_receive(:data=).with({})
64  
-    mock_session.should_receive(:dirty?).and_return(true)
65  
-    mock_session.should_receive(:updated_at=)
66  
-    mock_session.should_receive(:save).and_return(true)
67  
-    @store.send(:set_session, nil, "sid", {}).should == true
68  
-  end
69  
-
70  
-  it 'should set the session data' do
71  
-    Session.should_receive(:get).and_return(mock_session)
72  
-    mock_session.should_receive(:data=).with({})
73  
-    mock_session.should_receive(:dirty?).and_return(true)
74  
-    mock_session.should_receive(:updated_at=)
75  
-    mock_session.should_receive(:save).and_return(true)
76  
-    @store.send(:set_session, nil, "sid", {}).should == true
77  
-  end
78  
-end
79  
-
80  
-describe 'DatamapperStore with cache' do
81  
-
82  
-  def mock_session(stubs={})
83  
-    @mock_session ||= mock(Session, stubs)
84  
-  end
85  
-
86  
-  before :each do
87  
-    load 'lib/datamapper4rails/datamapper_store.rb'
88  
-    ActionController::Session::DatamapperStore.send(:class_variable_set, :@@cache, nil)
89  
-    ActionController::Session::DatamapperStore.send(:class_variable_set, :@@session_class, nil)
90  
-    @store = ActionController::Session::DatamapperStore.new(nil, :cache => true, :session_class => Session)
91  
-  end
92  
-
93  
-  it 'should get the session data from storage' do
94  
-    Session.stub!(:get)
95  
-    @store.send(:get_session, nil, "sid").should == ["sid",{}]
96  
-    Session.stub!(:get).and_return(mock_session)
97  
-    mock_session.should_receive(:data).and_return({:id => "id"})
98  
-    @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
99  
-  end
100  
-
101  
-  it 'should get the session data from cache' do
102  
-    ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"] = mock_session
103  
-    mock_session.should_receive(:data).and_return({:id => "id"})
104  
-    @store.send(:get_session, nil, "sid").should == ["sid",{:id => "id"}]
105  
-  end
106  
-
107  
-  it 'should set the session data on new session' do
108  
-    Session.should_receive(:get)
109  
-    Session.should_receive(:new).with(:session_id => "sid").and_return(mock_session)
110  
-    mock_session.should_receive(:data=).with({})
111  
-    mock_session.should_receive(:dirty?).and_return(true)
112  
-    mock_session.should_receive(:updated_at=)
113  
-    mock_session.should_receive(:save).and_return(true)
114  
-    @store.send(:set_session, nil, "sid", {}).should == true
115  
-    ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"].should == mock_session
116  
-    
117  
-  end
118  
-
119  
-  it 'should set the session data' do
120  
-    Session.should_receive(:get).and_return(mock_session)
121  
-    mock_session.should_receive(:data=).with({})
122  
-    mock_session.should_receive(:dirty?).and_return(false)
123  
-    mock_session.should_receive(:save).and_return(true)
124  
-    @store.send(:set_session, nil, "sid", {}).should == true
125  
-    ActionController::Session::DatamapperStore.send(:class_variable_get, :@@cache)["sid"].should == mock_session
  14
+  
  15
+  it 'should create a new session' do
  16
+    Session.should_receive(:create).and_return(mock_session)
  17
+    mock_session.should_receive(:data).and_return({})
  18
+    result = @store.send(:get_session, {}, nil)
  19
+    result[0].should_not be_nil
  20
+    result[1].should == {}
126 21
   end
  22
+  
127 23
 end
13  spec/spec_helper.rb
@@ -17,5 +17,18 @@ class Session
17 17
   include ::DataMapper::Resource
18 18
 
19 19
   property :session_id, String, :key => true
  20
+ 
  21
+  property :updated_at, DateTime
20 22
 
  23
+  property :data, Text, :nullable => false, :default => ::Base64.encode64(Marshal.dump({}))
  24
+  
  25
+  def data=(data)
  26
+    attribute_set(:data, ::Base64.encode64(Marshal.dump(data)))
  27
+  end
  28
+  
  29
+  def data
  30
+    Marshal.load(::Base64.decode64(attribute_get(:data)))
  31
+  end 
21 32
 end
  33
+
  34
+DataMapper.setup(:default, {:adapter  => 'in_memory'})

0 notes on commit e9d35fe

Please sign in to comment.
Something went wrong with that request. Please try again.