db_session 1.0.0 → 1.0.1
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.
- checksums.yaml +4 -4
- data/README.rdoc +10 -6
- data/lib/db_session.rb +76 -21
- data/lib/db_session/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6714f9b659ebeb71ccf2cf7be8cc5d425eb920c
|
4
|
+
data.tar.gz: 644b8b95f71070950aa4ddf0ae1aa1db7c9c326b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca06dc938618110c8111950cd3c53375b0e37dcfb344a4058dfce7fbb98b8917664f1ee3eb5525bca8f3658f7d7dc127cf88baf5edab3a3eb335d80b4d0c40fd
|
7
|
+
data.tar.gz: 77adcc5a12298d167320a13d29fc2a0b8cb32abb895559c74e44ee227222a487560cc92d4ebbbd34c374d0cda26044d4d0a8bbb27789be58aadd03f5fd57747c
|
data/README.rdoc
CHANGED
@@ -16,6 +16,8 @@ After you install DbSession and add it to your Gemfile, you need to run the gene
|
|
16
16
|
|
17
17
|
The generator will install an initializer which describes DbSession configuration options.
|
18
18
|
|
19
|
+
DbSession uses Sidekiq to perform some asynchronous task. Sidekiq uses Redis to manage its job queue, so you’ll need to install Redis.
|
20
|
+
|
19
21
|
=== Configuration
|
20
22
|
|
21
23
|
You can set up the life span of a session in <i>config/initializers/db_session.rb</i>:
|
@@ -26,24 +28,26 @@ This value represent the time in seconds after which a session expire and can po
|
|
26
28
|
|
27
29
|
=== Usage
|
28
30
|
|
29
|
-
You can call this methods
|
31
|
+
You can call this methods on any controller or view:
|
30
32
|
|
31
|
-
<tt>set_db_session(key,
|
33
|
+
<tt>set_db_session(key, object)</tt>
|
32
34
|
|
33
|
-
This method overwrite the content of the session with the key–
|
35
|
+
This method overwrite the content of the session with the key–object pair.
|
34
36
|
|
35
|
-
<tt>add_to_db_session(key,
|
37
|
+
<tt>add_to_db_session(key, object)</tt>
|
36
38
|
|
37
|
-
This method add the key–
|
39
|
+
This method add the key–object pair to the content of the session.
|
38
40
|
|
39
41
|
<tt>get_from_db_session(key)</tt>
|
40
42
|
|
41
|
-
This method return the
|
43
|
+
This method return the object stored in the session for the specified key. If no key is specified it returns the whole content of the session.
|
42
44
|
|
43
45
|
<tt>clear_db_session</tt>
|
44
46
|
|
45
47
|
This method clear the content of the session.
|
46
48
|
|
49
|
+
Objects are sotred in the database as JSON strings, so in order to be stored an object need to support JSON serialization and deserialization.
|
50
|
+
|
47
51
|
=== <em>That's it folks!</em>
|
48
52
|
|
49
53
|
|
data/lib/db_session.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module DbSession
|
2
2
|
require 'json'
|
3
|
+
require 'colorize'
|
3
4
|
require 'workers/clear_session_stores_worker'
|
4
5
|
|
5
6
|
autoload :DbSessionStore, 'models/db_session_store'
|
@@ -14,50 +15,64 @@ module DbSession
|
|
14
15
|
|
15
16
|
def get_from_db_session(key=nil)
|
16
17
|
db_session_id = session[SESSION_KEY]
|
17
|
-
db_session = DbSessionStore.find_by(id: db_session_id)
|
18
|
+
db_session_id ? db_session = DbSessionStore.find_by(id: db_session_id) : db_session = nil
|
18
19
|
|
19
20
|
if db_session
|
20
21
|
main_data_object = JSON.parse(db_session.serialized_data)
|
21
22
|
|
22
23
|
if key
|
23
|
-
return main_data_object[key]
|
24
|
+
return rebuild_object(main_data_object[key.to_s])
|
24
25
|
else
|
26
|
+
main_data_object.each do |k, v|
|
27
|
+
main_data_object[k] = rebuild_object(main_data_object[k])
|
28
|
+
end
|
25
29
|
return main_data_object
|
26
30
|
end
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
30
|
-
def set_db_session(key,
|
34
|
+
def set_db_session(key, object)
|
31
35
|
clear_db_session
|
32
36
|
|
33
|
-
|
34
|
-
main_data_object[key] = value
|
37
|
+
can_be_stored = can_be_stored?(object)
|
35
38
|
|
36
|
-
|
37
|
-
|
39
|
+
if can_be_stored
|
40
|
+
main_data_object = {}
|
41
|
+
main_data_object[key] = {class: get_class(object), object: object}
|
42
|
+
|
43
|
+
db_session = DbSessionStore.create(serialized_data: main_data_object.to_json)
|
44
|
+
|
45
|
+
session[SESSION_KEY] = db_session.id
|
46
|
+
end
|
38
47
|
|
39
48
|
clear_expired_sessions
|
49
|
+
|
50
|
+
can_be_stored
|
40
51
|
end
|
41
52
|
|
42
|
-
def add_to_db_session(key,
|
43
|
-
|
53
|
+
def add_to_db_session(key, object)
|
54
|
+
can_be_stored = can_be_stored?(object)
|
44
55
|
|
45
|
-
|
46
|
-
|
56
|
+
if can_be_stored
|
57
|
+
db_session_id = session[SESSION_KEY]
|
58
|
+
db_session_id ? db_session = DbSessionStore.find_by(id: db_session_id) : db_session = nil
|
47
59
|
|
48
|
-
|
49
|
-
|
60
|
+
db_session ? main_data_object = JSON.parse(db_session.serialized_data) : main_data_object = {}
|
61
|
+
main_data_object[key] = {class: get_class(object), object: object}
|
50
62
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
63
|
+
if db_session
|
64
|
+
db_session.serialized_data = main_data_object.to_json
|
65
|
+
db_session.save
|
66
|
+
else
|
67
|
+
db_session = DbSessionStore.create(serialized_data: main_data_object.to_json)
|
68
|
+
end
|
57
69
|
|
58
|
-
|
70
|
+
session[SESSION_KEY] = db_session.id
|
71
|
+
end
|
59
72
|
|
60
73
|
clear_expired_sessions
|
74
|
+
|
75
|
+
can_be_stored
|
61
76
|
end
|
62
77
|
|
63
78
|
def clear_db_session
|
@@ -68,6 +83,41 @@ module DbSession
|
|
68
83
|
end
|
69
84
|
end
|
70
85
|
|
86
|
+
private
|
87
|
+
|
88
|
+
def rebuild_object(obj)
|
89
|
+
if obj['class']
|
90
|
+
new = class_from_string(obj['class']).new
|
91
|
+
new.assign_attributes(obj['object'])
|
92
|
+
new
|
93
|
+
else
|
94
|
+
obj['object']
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def get_class(obj)
|
99
|
+
if obj
|
100
|
+
if obj.class.ancestors.include?(ActiveRecord::Base) && obj.respond_to?(:assign_attributes)
|
101
|
+
obj.class.name
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def can_be_stored?(obj)
|
107
|
+
begin
|
108
|
+
JSON.parse(obj.to_json)
|
109
|
+
return true
|
110
|
+
rescue
|
111
|
+
return false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def class_from_string(str)
|
116
|
+
str.split('::').inject(Object) do |mod, class_name|
|
117
|
+
mod.const_get(class_name)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
71
121
|
def clear_expired_sessions
|
72
122
|
begin
|
73
123
|
ClearSessionStoresWorker.perform_async(session_validity)
|
@@ -78,4 +128,9 @@ module DbSession
|
|
78
128
|
end
|
79
129
|
end
|
80
130
|
|
81
|
-
ActionController::Base.send(:include, DbSession)
|
131
|
+
ActionController::Base.send(:include, DbSession)
|
132
|
+
|
133
|
+
ActionController::Base.send(:helper_method, :get_from_db_session)
|
134
|
+
ActionController::Base.send(:helper_method, :set_db_session)
|
135
|
+
ActionController::Base.send(:helper_method, :add_to_db_session)
|
136
|
+
ActionController::Base.send(:helper_method, :clear_db_session)
|
data/lib/db_session/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrea Salomoni
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|