Class: Brut::FrontEnd::Flash
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Flash
- Defined in:
- lib/brut/front_end/flash.rb
Overview
A hash that can be used to pass short-lived information across requests. Generally, this is useful for storing error and status
messages. Generally, you won't create instances of this class. You may subclass it, to provide your own additional API for your
app's needs. To do that, you must call Brut.container.override("flash_class",«your class»)
.
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
Create a flash from a hash of values.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access an arbitrary flash message.
-
#[]=(key, message) ⇒ Object
Set an arbitrary flash message.
-
#age! ⇒ Object
Age this flash.
-
#alert ⇒ Object
Access the alert.
-
#alert=(alert) ⇒ Object
Set the "alert", which is an important error message.
-
#alert? ⇒ Boolean
True if there is an alert.
-
#clear! ⇒ Object
Clear the flash and reset its age to 0.
-
#initialize(age: 0, messages: {}) ⇒ Flash
constructor
Create a new flash of a given age with the given messages initialized.
-
#notice ⇒ Object
Access the notice.
-
#notice=(notice) ⇒ Object
Set the "notice", which is an informational message.
-
#notice? ⇒ Boolean
True if there is a notice.
-
#to_h ⇒ Object
Conver this flash into a hash, suitable for passing to Flash.from_h.
Constructor Details
#initialize(age: 0, messages: {}) ⇒ Flash
Create a new flash of a given age with the given messages initialized
22 23 24 25 26 27 28 |
# File 'lib/brut/front_end/flash.rb', line 22 def initialize(age: 0, messages: {}) @age = age.to_i if !.kind_of?(Hash) raise ArgumentError,"messages must be a Hash, not a #{.class}" end @messages = end |
Class Method Details
.from_h(hash) ⇒ Object
Create a flash from a hash of values.
internally.
10 11 12 13 14 15 16 |
# File 'lib/brut/front_end/flash.rb', line 10 def self.from_h(hash) hash ||= {} self.new( age: hash[:age] || 0, messages: hash[:messages] || {} ) end |
Instance Method Details
#[](key) ⇒ Object
Access an arbitrary flash message
78 79 80 |
# File 'lib/brut/front_end/flash.rb', line 78 def [](key) @messages[key] end |
#[]=(key, message) ⇒ Object
Set an arbitrary flash message. This resets the flash's age by one request.
83 84 85 86 |
# File 'lib/brut/front_end/flash.rb', line 83 def []=(key,) @messages[key] = @age = [0,@age-1].max end |
#age! ⇒ Object
Age this flash. The flash's age is the number of requests in the session it has existed for. This implementation prevents a flash from being more than 1 request old. This is usually sufficient for a handler to send information across a redirect.
69 70 71 72 73 74 75 |
# File 'lib/brut/front_end/flash.rb', line 69 def age! @age += 1 if @age > 1 @age = 0 @messages = {} end end |
#alert ⇒ Object
Access the alert. See #alert=
63 64 |
# File 'lib/brut/front_end/flash.rb', line 63 def alert = self[:alert] # True if there is an alert |
#alert=(alert) ⇒ Object
Set the "alert", which is an important error message. The value is intended to be an I18N key.
55 56 57 58 59 60 61 |
# File 'lib/brut/front_end/flash.rb', line 55 def alert=(alert) self[:alert] = if alert Array(alert).map(&:to_s).join(".") else alert end end |
#alert? ⇒ Boolean
True if there is an alert
65 |
# File 'lib/brut/front_end/flash.rb', line 65 def alert? = !!self.alert |
#clear! ⇒ Object
Clear the flash and reset its age to 0.
31 32 33 34 |
# File 'lib/brut/front_end/flash.rb', line 31 def clear! @age = 0 @messages = {} end |
#notice ⇒ Object
Access the notice. See #notice=
47 |
# File 'lib/brut/front_end/flash.rb', line 47 def notice = self[:notice] |
#notice=(notice) ⇒ Object
Set the "notice", which is an informational message. The value is intended to be an I18N key.
39 40 41 42 43 44 45 |
# File 'lib/brut/front_end/flash.rb', line 39 def notice=(notice) self[:notice] = if notice Array(notice).map(&:to_s).join(".") else notice end end |
#notice? ⇒ Boolean
True if there is a notice
50 |
# File 'lib/brut/front_end/flash.rb', line 50 def notice? = !!self.notice |
#to_h ⇒ Object
Conver this flash into a hash, suitable for passing to from_h
89 90 91 92 93 94 |
# File 'lib/brut/front_end/flash.rb', line 89 def to_h { age: @age, messages: @messages, } end |