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


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/flash.rb', line 39

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


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

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.


53
54
55
56
# File 'lib/rack/flash.rb', line 53

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

#[]=(key, val) ⇒ Object

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


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

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

#flag!Object

Mark existing entries to allow for sweeping.


79
80
81
# File 'lib/rack/flash.rb', line 79

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.


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

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

#inspectObject

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


91
92
93
# File 'lib/rack/flash.rb', line 91

def inspect
  '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect]
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.


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

def now
  cache
end

#sweep!Object

Remove flagged entries from flash session, clear flagged list.


84
85
86
87
# File 'lib/rack/flash.rb', line 84

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

#to_sObject

Human readable for logging.


96
97
98
# File 'lib/rack/flash.rb', line 96

def to_s
  values.inspect
end