Skip to content

Commit

Permalink
(session) compose session hash
Browse files Browse the repository at this point in the history
Currently seeing a compiler bug when attempting to compile with raven.cr. Although I'm not convinced entirely that this is an issue with action-controller, it does seem to be considered good practice to stick with composition rather than inheritence for this sort of stuff (crystal-lang/crystal#3238 (comment)).

The approach taken was effectively the same as amberframework/amber#930 which fixed the same issue within Amber.
  • Loading branch information
jackturnbull committed Sep 2, 2019
1 parent 6ba2ec4 commit 25062a6
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/action-controller/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "random"
require "./session/message_verifier"
require "./session/message_encryptor"

class ActionController::Session < Hash(String, String | Int64 | Float64 | Bool)
class ActionController::Session
NEVER = 622_080_000 # (~20 years in seconds)
# Cookies can typically store 4096 bytes.
MAX_COOKIE_SIZE = 4096
Expand All @@ -22,6 +22,9 @@ class ActionController::Session < Hash(String, String | Int64 | Float64 | Bool)
getter modified : Bool
property domain : String?
@encoder : MessageEncryptor | MessageVerifier
@store : Hash(String, String | Int64 | Float64 | Bool)

forward_missing_to @store

def initialize
super
Expand All @@ -35,6 +38,8 @@ class ActionController::Session < Hash(String, String | Int64 | Float64 | Bool)
else
@encoder = MessageVerifier.new(settings.secret)
end

@store = {} of String => String | Int64 | Float64 | Bool
end

def self.from_cookies(cookies)
Expand Down Expand Up @@ -82,30 +87,30 @@ class ActionController::Session < Hash(String, String | Int64 | Float64 | Bool)
if value.nil?
delete(key)
else
super(key, value)
@store[key] = value
@modified = true
end
value
end

def clear
@modified = true if @existing
super
@store.clear
end

def delete(key)
@modified = true
super(key)
@store.delete(key)
end

def delete(key, &block)
@modified = true
super(key, &block)
@store.delete(key, &block)
end

def delete_if(&block)
@modified = true
super(&block)
@store.delete_if(&block)
end

def touch
Expand Down

0 comments on commit 25062a6

Please sign in to comment.