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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a803fca4e763e6665092b29dd9feb4911d605a02
4
- data.tar.gz: 87571df508941c0e5d218172d17b7a8b43b97c81
3
+ metadata.gz: e6714f9b659ebeb71ccf2cf7be8cc5d425eb920c
4
+ data.tar.gz: 644b8b95f71070950aa4ddf0ae1aa1db7c9c326b
5
5
  SHA512:
6
- metadata.gz: c7de04bf441a3c02038548342a01a8c456cdc11cca90951746653036497dbab6487832ee3c78efc9fd153c0514178401cfbaf745ba016821acd57cb1b0f167b0
7
- data.tar.gz: 1c27a541ff9ac09163abc08dc9a804309c725c39813a32d947ba106386fa1b0c1a3462a39708974a03f00b5d808628e718807be305bbdbf0c47655287384ddca
6
+ metadata.gz: ca06dc938618110c8111950cd3c53375b0e37dcfb344a4058dfce7fbb98b8917664f1ee3eb5525bca8f3658f7d7dc127cf88baf5edab3a3eb335d80b4d0c40fd
7
+ data.tar.gz: 77adcc5a12298d167320a13d29fc2a0b8cb32abb895559c74e44ee227222a487560cc92d4ebbbd34c374d0cda26044d4d0a8bbb27789be58aadd03f5fd57747c
@@ -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 inside any controller:
31
+ You can call this methods on any controller or view:
30
32
 
31
- <tt>set_db_session(key, value)</tt>
33
+ <tt>set_db_session(key, object)</tt>
32
34
 
33
- This method overwrite the content of the session with the key–value pair.
35
+ This method overwrite the content of the session with the key–object pair.
34
36
 
35
- <tt>add_to_db_session(key, value)</tt>
37
+ <tt>add_to_db_session(key, object)</tt>
36
38
 
37
- This method add the key–value pair to the content of the session.
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 value stored in the session for the specified key. If no key is specified it returns the whole content of the session.
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
 
@@ -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) if 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, value)
34
+ def set_db_session(key, object)
31
35
  clear_db_session
32
36
 
33
- main_data_object = {}
34
- main_data_object[key] = value
37
+ can_be_stored = can_be_stored?(object)
35
38
 
36
- db_session = DbSessionStore.create(serialized_data: main_data_object.to_json)
37
- session[SESSION_KEY] = db_session.id
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, value)
43
- main_data_object = {}
53
+ def add_to_db_session(key, object)
54
+ can_be_stored = can_be_stored?(object)
44
55
 
45
- db_session_id = session[SESSION_KEY]
46
- db_session = DbSessionStore.find_by(id: db_session_id) if db_session_id
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
- main_data_object = JSON.parse(db_session.serialized_data) if db_session
49
- main_data_object[key] = value
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
- if db_session
52
- db_session.serialized_data = main_data_object.to_json
53
- db_session.save
54
- else
55
- db_session = DbSessionStore.create(serialized_data: main_data_object.to_json)
56
- end
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
- session[SESSION_KEY] = db_session.id
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)
@@ -1,3 +1,3 @@
1
1
  module DbSession
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
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.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-15 00:00:00.000000000 Z
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails