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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FlashHash.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/flash.rb', line 43

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

  @opts = opts
  @store = store
  @store[:__FLASH__] ||= {}

  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

Class Method Details

.default_storeObject



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

def self.default_store
  nil
end

Instance Method Details

#[](key) ⇒ Object

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



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

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.



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

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

#flag!Object

Mark existing entries to allow for sweeping.



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

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)


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

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.



98
99
100
# File 'lib/rack/flash.rb', line 98

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.



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

def now
  cache
end

#sweep!Object

Remove flagged entries from flash session, clear flagged list.



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

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

#to_sObject

Human readable for logging.



103
104
105
# File 'lib/rack/flash.rb', line 103

def to_s
  values.inspect
end