Module: JulikState

Defined in:
lib/pasaporte/julik_state.rb

Overview

Camping session module is a wretched piece of junk, so we just replace it

Defined Under Namespace

Classes: State

Constant Summary collapse

RC =
[*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_schemaObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pasaporte/julik_state.rb', line 6

def self.create_schema
  return if State.table_exists?
  ActiveRecord::Schema.define do
    create_table :julik_states do | s |
      s.column :blob, :text
      s.column :sid, :string, :limit => 32
      s.column :app, :string, :null => false, :limit => 50
      s.column :modified_at, :datetime
      s.column :created_at, :datetime
    end
    add_index :julik_states, [:sid, :app], :unique => true
    State.reset_column_information
  end
end

Instance Method Details

#_appnObject



22
# File 'lib/pasaporte/julik_state.rb', line 22

def _appn; self.class.to_s.split(/::/).shift; end

#_sidObject



21
# File 'lib/pasaporte/julik_state.rb', line 21

def _sid; (0...32).inject("") { |ret,_| ret << RC[rand(RC.length)] }; end

#force_session_save!Object



24
25
26
27
# File 'lib/pasaporte/julik_state.rb', line 24

def force_session_save!
  res = @js_rec.update_attributes :blob => @state, :sid => @cookies.jsid
  raise "Cannot save session" unless res
end

#initialize_session!(with = ) ⇒ Object



35
36
37
38
39
# File 'lib/pasaporte/julik_state.rb', line 35

def initialize_session!(with = Camping::H[{}])
  @js_rec = State.find_by_sid_and_app(@cookies.jsid, _appn) || State.new(
    :app => _appn, :blob => with, :sid => (@cookies.jsid = _sid))
  @state = @js_rec.blob.dup
end

#reset_session!Object



29
30
31
32
33
# File 'lib/pasaporte/julik_state.rb', line 29

def reset_session!
  @cookies.jsid = _sid
  @js_rec.destroy unless @js_rec.new_record?
  initialize_session!
end

#service(*a) ⇒ Object



41
42
43
44
45
# File 'lib/pasaporte/julik_state.rb', line 41

def service(*a)
  initialize_session!
  @msg = @state.delete(:msg)
  returning(super(*a)) { force_session_save! }
end