Class: Flame::Flash::FlashArray

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/flash_array.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, parent = nil, scope = nil) ⇒ FlashArray

Builds a new FlashHash. It takes the hash for this action’s values as an initialization variable.



11
12
13
14
15
16
17
# 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

Instance Attribute Details

#nextObject (readonly)

Returns the value of attribute next.



7
8
9
# File 'lib/flame/flash_array.rb', line 7

def next
  @next
end

#nowObject (readonly)

Returns the value of attribute now.



7
8
9
# File 'lib/flame/flash_array.rb', line 7

def now
  @now
end

Instance Method Details

#[](type = nil) ⇒ Object



41
42
43
44
45
46
47
# 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) ⇒ Object

We assign to the next hash, but retrieve values from the now hash. Freaky, huh?



31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



49
50
51
# File 'lib/flame/flash_array.rb', line 49

def each(&block)
  @now.each(&block)
end

#merge(hash) ⇒ Object

Mass adding to next



54
55
56
# File 'lib/flame/flash_array.rb', line 54

def merge(hash)
  hash.each { |type, text| self[type] = text }
end

#scope(scope = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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