Class: Brut::FrontEnd::Flash

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(age: 0, messages: {}) ⇒ Flash

Create a new flash of a given age with the given messages initialized

Parameters:

  • age (Integer) (defaults to: 0)

    the age of this flash. See #age!.

  • messages (Hash) (defaults to: {})

    the flash messages to use. Note that :notice and :alert are special. See #notice= and #alert=.



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 !messages.kind_of?(Hash)
    raise ArgumentError,"messages must be a Hash, not a #{messages.class}"
  end
  @messages = messages
end

Class Method Details

.from_h(hash) ⇒ Object

Create a flash from a hash of values.

internally.

Parameters:

  • hash (Hash)

    the values that should comprise the hash. Note that this hash is not exactly how the flash stores itself



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,message)
  @messages[key] = message
  @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

#alertObject

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.

Parameters:

  • alert (String|Array)

    the I18n key of the notice. If this is an array, it will be joined with dots to form an I18n eky.



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

Returns:

  • (Boolean)


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

#noticeObject

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.

Parameters:

  • notice (String|Array)

    the I18n key of the notice. If this is an array, it will be joined with dots to form 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

Returns:

  • (Boolean)


50
# File 'lib/brut/front_end/flash.rb', line 50

def notice? = !!self.notice

#to_hObject

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