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
# File 'lib/flame/flash_array.rb', line 11

def initialize(session, parent = nil, scope = nil)
  @now = session || []
  @next = []
  @parent = parent || self
  @scope = scope
end

Instance Attribute Details

#next ⇒ Object (readonly)

Returns the value of attribute next.



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

def next
  @next
end

#now ⇒ Object (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



40
41
42
43
44
45
46
# File 'lib/flame/flash_array.rb', line 40

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?



30
31
32
33
34
35
36
37
38
# File 'lib/flame/flash_array.rb', line 30

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



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

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

#merge(hash) ⇒ Object

Mass adding to next



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

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

#scope(scope = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/flame/flash_array.rb', line 18

def scope(scope = nil)
  # p 'scope', scope
  return self unless scope
  self.class.new(
    @now.select { |hash| condition(hash, scope: scope) },
    self,
    scope
  )
end