Class: Harbor::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/session.rb,
lib/harbor/session/cookie.rb,
lib/harbor/session/abstract.rb

Direct Known Subclasses

Test::Session

Defined Under Namespace

Classes: Abstract, Cookie

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :key => "harbor.session",
  :domain => nil,
  :path => "/",
  :expire_after => nil,
  :store => Cookie
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Session

Returns a new instance of Session.



34
35
36
37
38
39
40
# File 'lib/harbor/session.rb', line 34

def initialize(request)
  @options = self.class.options.dup
  @cookie = request.cookies[@options[:key]]
  @store = self.class.options[:store]

  @data ||= @store.load_session(@cookie)
end

Class Method Details

.configure {|@options| ... } ⇒ Object

Configures non-default session settings.

Harbor::Session.configure do |session|
  session[:domain] = "*.domain.com"
  session[:store] = Custom::Session::Store
end

Yields:



23
24
25
26
27
# File 'lib/harbor/session.rb', line 23

def self.configure #:yields: default_options
  @options = DEFAULT_OPTIONS.dup
  yield(@options)
  @options
end

.optionsObject



29
30
31
32
# File 'lib/harbor/session.rb', line 29

def self.options
  @options ||= DEFAULT_OPTIONS.dup
  @options
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/harbor/session.rb', line 50

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



46
47
48
# File 'lib/harbor/session.rb', line 46

def []=(key, value)
  @data[key] = value
end

#dataObject



54
55
56
# File 'lib/harbor/session.rb', line 54

def data
  @data
end

#destroyObject



71
72
73
# File 'lib/harbor/session.rb', line 71

def destroy
  @data.clear
end

#idObject



58
59
60
# File 'lib/harbor/session.rb', line 58

def id
  @data[:session_id]
end

#keyObject



42
43
44
# File 'lib/harbor/session.rb', line 42

def key
  @options[:key]
end

#saveObject



62
63
64
65
66
67
68
69
# File 'lib/harbor/session.rb', line 62

def save
  cookie = {}
  cookie[:domain] = @options[:domain]
  cookie[:path] = @options[:path]
  cookie[:expires] = Time.now + @options[:expire_after] unless @options[:expire_after].nil?
  cookie[:value] = @store.commit_session(@data)
  cookie
end