class Flame::Flash::FlashArray
A subclass of Array that “remembers forward” by exactly one action. Tastes just like the API of Rails's ActionController::Flash::FlashHash, but with fewer calories.
Attributes
next[R]
now[R]
Public Class Methods
new(session, parent = nil, scope = nil)
click to toggle source
Builds a new FlashHash. It takes the hash for this action's values as an initialization variable.
# File lib/flame/flash_array.rb, line 11 def initialize(session, parent = nil, scope = nil) @now = session || [] fix_messages_as_array @next = [] @parent = parent || self @scope = scope end
Public Instance Methods
[](type = nil)
click to toggle source
# File lib/flame/flash_array.rb, line 41 def [](type = nil) selected = @parent.now.select do |hash| condition(hash, type: type, scope: @scope) end # p 'flash[]', type, @scope, selected selected.map { |hash| hash[:text] } end
[]=(type, text)
click to toggle source
We assign to the next hash, but retrieve values from the now hash. Freaky, huh?
# File lib/flame/flash_array.rb, line 31 def []=(type, text) return text.each { |el| self[type] = el } if text.is_a?(Array) hash = { type: type, text: text } # p @parent == self, @scope hash[:scope] = @scope if @parent != self # p hash @parent.next.push(hash) # p @parent.next end
each(&block)
click to toggle source
# File lib/flame/flash_array.rb, line 49 def each(&block) @now.each(&block) end
merge(hash)
click to toggle source
Mass adding to next
# File lib/flame/flash_array.rb, line 54 def merge(hash) hash.each { |type, text| self[type] = text } end
scope(scope = nil)
click to toggle source
# File lib/flame/flash_array.rb, line 19 def scope(scope = nil) # p 'scope', scope return self unless scope self.class.new( @now.select { |hash| condition(hash, scope: scope) }, self, scope ) end
Private Instance Methods
condition(hash, options = {})
click to toggle source
# File lib/flame/flash_array.rb, line 60 def condition(hash, options = {}) # kind, section) options.reject { |key, val| hash[key] == val || val.nil? }.empty? end
fix_messages_as_array()
click to toggle source
# File lib/flame/flash_array.rb, line 64 def fix_messages_as_array @now.each_with_index do |hash, ind| next unless hash[:text].is_a?(Array) hash = @now.delete(hash) @now.insert(ind, hash[:text].map { |text| hash.merge(text: text) }) end end