Class: Maveric::Sessions::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/maveric/sessions.rb

Overview

Because apparently sessions are handy or helpful or something like that. Implemented as that the hex of their #object_id (the one you’d see in an Object#inspect) is treated as a session id.

Constant Summary collapse

DURATION =
15*60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration = DURATION) ⇒ Session

The default session length is 15 minutes. Simple to change.



25
26
27
28
29
30
# File 'lib/maveric/sessions.rb', line 25

def initialize duration=DURATION
  ::Maveric.type_check :duration, duration, Integer
  @duration, @data = duration, {}
  touch
  ::Maveric.log.debug self
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



31
32
33
# File 'lib/maveric/sessions.rb', line 31

def data
  @data
end

#durationObject (readonly)

Returns the value of attribute duration.



31
32
33
# File 'lib/maveric/sessions.rb', line 31

def duration
  @duration
end

#expiresObject (readonly)

Returns the value of attribute expires.



31
32
33
# File 'lib/maveric/sessions.rb', line 31

def expires
  @expires
end

Instance Method Details

#<=>(obj) ⇒ Object

One session is less than another if it expires sooner. Useful if we are managing in a manner akin to a priority stack.



37
# File 'lib/maveric/sessions.rb', line 37

def <=> obj; Session === obj ? self.expires <=> obj.expires : super; end

#idObject

Returns an upcased hexed object_id, not #object_id.to_s(16) however.



33
# File 'lib/maveric/sessions.rb', line 33

def id; @id||=[object_id<<1].pack('i').unpack('I')[0].to_s(16).upcase; end

#to_s(env = nil) ⇒ Object

Perhaps better named as #to_cookie for clarity?



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maveric/sessions.rb', line 41

def to_s env=nil
  touch
  c = "#{::Maveric::Sessions::COOKIE_NAME}=#{id}" #required
  c << "; expires=#{expires.httpdate}"
  return c unless env
  c << "; domain=#{env['SERVER_NAME']};"
  # need to determine a good way to discern proper path.
  # pertinant Maveric?
  c << "; secure" if false
  c
end

#touchObject

Set this session’s expiry to @duration+Time.now



39
# File 'lib/maveric/sessions.rb', line 39

def touch; @expires = Time.now.gmtime + Integer(@duration); end