Class: RubyApp::Session
Defined Under Namespace
Classes: Identity
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
configuration
exists?, method_missing
localize, translate
Constructor Details
#initialize(document = nil) ⇒ Session
Returns a new instance of Session.
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
31
32
33
|
# File 'lib/ruby_app/session.rb', line 31
def data
@data
end
|
#documents ⇒ Object
Returns the value of attribute documents.
30
31
32
|
# File 'lib/ruby_app/session.rb', line 30
def documents
@documents
end
|
#expires ⇒ Object
Returns the value of attribute expires.
28
29
30
|
# File 'lib/ruby_app/session.rb', line 28
def expires
@expires
end
|
#identity ⇒ Object
Returns the value of attribute identity.
33
34
35
|
# File 'lib/ruby_app/session.rb', line 33
def identity
@identity
end
|
#session_id ⇒ Object
Returns the value of attribute session_id.
27
28
29
|
# File 'lib/ruby_app/session.rb', line 27
def session_id
@session_id
end
|
Class Method Details
.generate_session_id(session) ⇒ Object
.get ⇒ Object
155
156
157
|
# File 'lib/ruby_app/session.rb', line 155
def self.get
Thread.current[:_session]
end
|
.get_scripts(path = String.interpolate { RubyApp::Session.configuration.scripts.path }) ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/ruby_app/session.rb', line 208
def self.get_scripts(path = String.interpolate { RubyApp::Session.configuration.scripts.path })
scripts = []
Dir.new(path).each do |item|
unless item.start_with?('.')
_path = File.join(path, item)
if File.directory?(_path)
scripts += self.get_scripts(_path)
elsif _path =~ /\.rb/
name = _path.gsub(String.interpolate { RubyApp::Session.configuration.scripts.path }, '').gsub(/^\//, '').gsub(/\.rb/, '')
scripts.push({:name => name,
:url => "/quit?go=#{CGI.escape("/?script=#{name}")}"})
end
end
end
return scripts.sort { |a, b| a.name <=> b.name }
end
|
.get_session(session_id) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/ruby_app/session.rb', line 129
def self.get_session(session_id)
session = RubyApp::Session.sessions[session_id]
if session
unless session.expired?
session.reset_expiry!
return session
else
session.quit!
return nil
end
else
return nil
end
end
|
.load!(session_id = nil, script_path = nil) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/ruby_app/session.rb', line 159
def self.load!(session_id = nil, script_path = nil)
session = RubyApp::Session.get_session(session_id)
unless session
session = Kernel.eval(RubyApp::Session.configuration._class).new
session.load_script!(script_path) if script_path
RubyApp::Log.debug("SESSION RubyApp::Session.session_id=#{session.session_id.inspect}")
RubyApp::Session.configuration.log.each do |variable|
RubyApp::Log.debug("SESSION RubyApp::Request.environment['#{variable}']=#{RubyApp::Request.environment[variable].inspect}")
end
end
Thread.current[:_session] = session
end
|
.lock_sessions ⇒ Object
123
124
125
126
127
|
# File 'lib/ruby_app/session.rb', line 123
def self.lock_sessions
( @@_lock ||= ::Mutex.new ).synchronize do
yield
end
end
|
.sessions ⇒ Object
119
120
121
|
# File 'lib/ruby_app/session.rb', line 119
def self.sessions
return @@_sessions ||= {}
end
|
.start_thread! ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/ruby_app/session.rb', line 176
def self.start_thread!
unless @@_thread ||= nil
@@_thread = Thread.new do
begin
while true
RubyApp::Session.sessions.values.each do |session|
if session.expired?
RubyApp::Log.debug("SESSION session.session_id=#{session.session_id.inspect}")
RubyApp::Log.debug("SESSION session.quit!")
session.quit!
end
end
sleep(RubyApp::Session.configuration.interval)
end
rescue => exception
RubyApp::Log.exception(RubyApp::Log::ERROR, exception)
end
end
RubyApp::Log.debug("SESSION #{RubyApp::Log.prefix(self, __method__)}")
end
end
|
.stop_thread! ⇒ Object
200
201
202
203
204
205
206
|
# File 'lib/ruby_app/session.rb', line 200
def self.stop_thread!
if @@_thread ||= nil
RubyApp::Log.debug("SESSION #{RubyApp::Log.prefix(self, __method__)}")
@@_thread.exit
@@_thread = nil
end
end
|
.unload! ⇒ Object
172
173
174
|
# File 'lib/ruby_app/session.rb', line 172
def self.unload!
Thread.current[:_session] = nil
end
|
Instance Method Details
#add_step!(_class, &block) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/ruby_app/session.rb', line 75
def add_step!(_class, &block)
caller = Kernel.caller.first.split(':')
@steps.push({:_class => _class,
:file => caller[0].gsub(String.interpolate { RubyApp::Session.configuration.scripts.path }, '').gsub(/^\//, ''),
:line => caller[1].to_i,
:block => block})
end
|
#document ⇒ Object
52
53
54
|
# File 'lib/ruby_app/session.rb', line 52
def document
return @documents.last
end
|
#expired? ⇒ Boolean
60
61
62
|
# File 'lib/ruby_app/session.rb', line 60
def expired?
return @expires <= Time.now
end
|
#load_script!(path) ⇒ Object
70
71
72
73
|
# File 'lib/ruby_app/session.rb', line 70
def load_script!(path)
_path = File.join(String.interpolate { RubyApp::Session.configuration.scripts.path }, "#{path}.rb")
Kernel.eval(File.read(_path), self.send(:binding), _path)
end
|
#process_event!(event) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/ruby_app/session.rb', line 83
def process_event!(event)
event.process!
if RubyApp::Session.configuration.scripts.enabled
step = @steps[@steps_index]
if step
begin
if event.is_a?(step._class)
@steps_index += 1
RubyApp::Log.duration(RubyApp::Log::INFO, "STEP Current #{step._class} #{step.file}:#{step.line}#{step.block ? nil : ' (no block)'}") do
step.block.call(event) if step.block
end
if @steps_index == @steps.length
RubyApp::Log.info('-' * 80)
RubyApp::Log.info("STEP Completed #{@steps.length} steps")
RubyApp::Log.info('-' * 80)
else
step = @steps[@steps_index]
RubyApp::Log.info("STEP Next #{step._class} #{step.file}:#{step.line}#{step.block ? nil : ' (no block)'}")
end
end
rescue => exception
RubyApp::Log.info("STEP Exception occurred at #{step.file}:#{step.line}")
@steps_index = @steps.length
raise
end
end
end
end
|
#reset_script! ⇒ Object
115
116
117
|
# File 'lib/ruby_app/session.rb', line 115
def reset_script!
@steps_index = 0
end
|