Class: Maveric::SimpleSessions::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.



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

def initialize duration=DURATION
  @duration, @data = duration, {}
  touch
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



44
45
46
# File 'lib/maveric/sessions.rb', line 44

def data
  @data
end

#durationObject (readonly)

Returns the value of attribute duration.



44
45
46
# File 'lib/maveric/sessions.rb', line 44

def duration
  @duration
end

#expiresObject (readonly)

Returns the value of attribute expires.



44
45
46
# File 'lib/maveric/sessions.rb', line 44

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.



54
55
56
57
58
# File 'lib/maveric/sessions.rb', line 54

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

#idObject

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



47
48
49
# File 'lib/maveric/sessions.rb', line 47

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?



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/maveric/sessions.rb', line 66

def to_s env=nil
  touch
  c = "#{::Maveric::SimpleSessions::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



61
62
63
# File 'lib/maveric/sessions.rb', line 61

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