Class: Olelo::FlashHelper::FlashHash

Inherits:
Object
  • Object
show all
Defined in:
lib/olelo/helper.rb

Overview

Implements bracket accessors for storing and retrieving flash entries.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ FlashHash



55
56
57
58
# File 'lib/olelo/helper.rb', line 55

def initialize(session)
  @session = session
  raise 'No session available' if !session
end

Class Method Details

.define_accessors(*accessors) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/olelo/helper.rb', line 31

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) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/olelo/helper.rb', line 42

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

Instance Method Details

#[](key) ⇒ Object

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



62
63
64
65
# File 'lib/olelo/helper.rb', line 62

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.



68
69
70
71
# File 'lib/olelo/helper.rb', line 68

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

#clearObject

Clear the hash



87
88
89
90
# File 'lib/olelo/helper.rb', line 87

def clear
  cache.clear
  @session.delete(:olelo_flash)
end

#include?(key) ⇒ Boolean

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



81
82
83
84
# File 'lib/olelo/helper.rb', line 81

def include?(key)
  key = key.to_sym
  cache.keys.include?(key) || values.keys.include?(key)
end

#nowObject

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



75
76
77
# File 'lib/olelo/helper.rb', line 75

def now
  cache
end