Class: Innate::Session::Flash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/innate/session/flash.rb

Overview

The purpose of this class is to act as a unifier of the previous and current flash.

Flash means pairs of keys and values that are held only over one request/response cycle. So you can assign a key/value in the current session and retrieve it in the current and following request.

Please see the Innate::Helper::Flash for details on the usage in your application.

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Flash

Returns a new instance of Flash.



16
17
18
# File 'lib/innate/session/flash.rb', line 16

def initialize(session)
  @session = session
end

Instance Method Details

#[](key) ⇒ Object

flash in your Controller



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

def [](key)
  combined[key]
end

#[]=(key, value) ⇒ Object

flash = value in your Controller



47
48
49
50
51
# File 'lib/innate/session/flash.rb', line 47

def []=(key, value)
  prev = session[:FLASH] || {}
  prev[key] = value
  session[:FLASH] = prev
end

#combinedObject

combined key/value pairs of previous and current current keys overshadow the old ones.



37
38
39
# File 'lib/innate/session/flash.rb', line 37

def combined
  previous.merge(current)
end

#currentObject

the current session



31
32
33
# File 'lib/innate/session/flash.rb', line 31

def current
  session[:FLASH] ||= {}
end

#delete(key) ⇒ Object

Delete a key



59
60
61
62
# File 'lib/innate/session/flash.rb', line 59

def delete(key)
  previous.delete(key)
  current.delete(key)
end

#each(&block) ⇒ Object

iterate over the combined session



21
22
23
# File 'lib/innate/session/flash.rb', line 21

def each(&block)
  combined.each(&block)
end

#empty?Boolean

check if combined is empty

Returns:

  • (Boolean)


65
66
67
# File 'lib/innate/session/flash.rb', line 65

def empty?
  combined.empty?
end

#inspectObject

Inspects combined



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

def inspect
  combined.inspect
end

#merge(hash) ⇒ Object

merge on current



75
76
77
# File 'lib/innate/session/flash.rb', line 75

def merge(hash)
  current.merge(hash)
end

#merge!(hash) ⇒ Object

merge into current



70
71
72
# File 'lib/innate/session/flash.rb', line 70

def merge!(hash)
  current.merge!(hash)
end

#previousObject

the current session



26
27
28
# File 'lib/innate/session/flash.rb', line 26

def previous
  session[:FLASH_PREVIOUS] || {}
end

#rotate!Object

Rotation means current values are assigned as old values for the next request.



81
82
83
84
# File 'lib/innate/session/flash.rb', line 81

def rotate!
  old = session.delete(:FLASH)
  session[:FLASH_PREVIOUS] = old if old
end