Class: Rack::Flash::FlashHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/flash.rb

Overview

Implements bracket accessors for storing and retrieving flash entries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, opts = {}) ⇒ FlashHash

Returns a new instance of FlashHash.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rack/flash.rb', line 12

def initialize(store, opts={})
  raise Rack::Flash::SessionUnavailable \
    .new('Rack::Flash depends on session middleware.') unless store

  @opts = opts
  @store = store

  if accessors = @opts[:accessorize]
    accessors.each { |opt| def_accessor(opt) }
  end
end

Instance Attribute Details

#flaggedObject (readonly)

Returns the value of attribute flagged.



10
11
12
# File 'lib/rack/flash.rb', line 10

def flagged
  @flagged
end

Instance Method Details

#[](key) ⇒ Object

Remove an entry from the session and return its value. Cache result in the instance cache.



26
27
28
29
# File 'lib/rack/flash.rb', line 26

def [](key)
  key = key.to_s
  cache[key] ||= values.delete(key)
end

#[]=(key, val) ⇒ Object

Store the entry in the session, updating the instance cache as well.



32
33
34
35
# File 'lib/rack/flash.rb', line 32

def []=(key,val)
  key = key.to_s
  cache[key] = values[key] = val
end

#flag!Object

Mark existing entries to allow for sweeping.



56
57
58
# File 'lib/rack/flash.rb', line 56

def flag!
  @flagged = values.keys
end

#has?(key) ⇒ Boolean Also known as: include?

Checks for the presence of a flash entry without retrieving or removing it from the cache or store.

Returns:

  • (Boolean)


46
47
48
# File 'lib/rack/flash.rb', line 46

def has?(key)
  [cache, values].any? { |store| store.keys.include?(key.to_s) }
end

#inspectObject

Hide the underlying :__FLASH__ session key and only expose values stored in the flash.



68
69
70
# File 'lib/rack/flash.rb', line 68

def inspect
  '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect]
end

#keysObject



51
52
53
# File 'lib/rack/flash.rb', line 51

def keys
  cache.keys | values.keys
end

#nowObject

Store a flash entry for only the current request, swept regardless of whether or not it was actually accessed. Useful for AJAX requests, where you want a flash message, even though you’re response isn’t redirecting.



40
41
42
# File 'lib/rack/flash.rb', line 40

def now
  cache
end

#sweep!Object

Remove flagged entries from flash session, clear flagged list.



61
62
63
64
# File 'lib/rack/flash.rb', line 61

def sweep!
  Array(flagged).each { |key| values.delete(key) }
  flagged.clear
end

#to_sObject

Human readable for logging.



73
74
75
# File 'lib/rack/flash.rb', line 73

def to_s
  values.inspect
end