class Olelo::FlashHelper::FlashHash

Implements bracket accessors for storing and retrieving flash entries.

Public Class Methods

define_accessors(*accessors) click to toggle source
# File lib/olelo/helper.rb, line 30
def define_accessors(*accessors)
  accessors.compact.each do |key|
    key = key.to_sym
    class_eval do
      define_method(key) {|*a| a.size > 0 ? (self[key] = a[0]) : self[key] }
      define_method("#{key}=") {|val| self[key] = val }
      define_method("#{key}!") {|val| cache[key] = val }
    end
  end
end
define_set_accessors(*accessors) click to toggle source
# File lib/olelo/helper.rb, line 41
def define_set_accessors(*accessors)
  accessors.compact.each do |key|
    key = key.to_sym
    class_eval do
      define_method(key) {|*val| val.size > 0 ? (self[key] ||= Set.new).merge(val) : self[key] }
      define_method("#{key}!") {|*val| val.size > 0 ? (cache[key] ||= Set.new).merge(val) : cache[key] }
    end
  end
end
new(session) click to toggle source
# File lib/olelo/helper.rb, line 54
def initialize(session)
  @session = session
  raise 'No session available' if !session
end

Public Instance Methods

[](key) click to toggle source

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

# File lib/olelo/helper.rb, line 61
def [](key)
  key = key.to_sym
  cache[key] ||= values.delete(key)
end
[]=(key,val) click to toggle source

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

# File lib/olelo/helper.rb, line 67
def []=(key,val)
  key = key.to_sym
  cache[key] = values[key] = val
end
clear() click to toggle source

Clear the hash

# File lib/olelo/helper.rb, line 86
def clear
  cache.clear
  @session.delete(:olelo_flash)
end
include?(key) click to toggle source

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

# File lib/olelo/helper.rb, line 80
def include?(key)
  key = key.to_sym
  cache.keys.include?(key) || values.keys.include?(key)
end
now() click to toggle source

Store a flash entry for only the current request, swept regardless of whether or not it was actually accessed

# File lib/olelo/helper.rb, line 74
def now
  cache
end

Private Instance Methods

cache() click to toggle source

Maintain an instance-level cache of retrieved flash entries. These entries will have been removed from the session, but are still available through the cache.

# File lib/olelo/helper.rb, line 96
def cache
  @cache ||= {}
end
values() click to toggle source

Helper to access flash entries from session value. This key is used to prevent collisions with other user-defined session values.

# File lib/olelo/helper.rb, line 102
def values
  @session[:olelo_flash] ||= {}
end