Module: CampingFlash

Defined in:
lib/tarpaulin/camping/flash.rb

Overview

class Hash

def diff(other)
  self.keys.inject({}) do |memo, key|
    unless self[key] == other[key]
      memo[key] = [self[key], other[key]] 
    end
    memo
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tarpaulin/camping/flash.rb', line 25

def self.included(base)
  # self is CampingFlash
  # base is TheApp
  Camping::H.class_eval <<-EOT
    def diff(other)
      self.keys.inject({}) do |memo, key|
        unless self[key] == other[key]
          memo[key] = [self[key], other[key]] 
        end
        memo
      end
    end
  EOT
end

Instance Method Details

#initialize(env, m) ⇒ Object

:nodoc:



64
65
66
67
# File 'lib/tarpaulin/camping/flash.rb', line 64

def initialize(env, m) #:nodoc:
  super(env,m)
  @_state = @state.clone
end

#to_aObject

:nodoc:



40
41
42
43
44
45
46
47
# File 'lib/tarpaulin/camping/flash.rb', line 40

def to_a #:nodoc:
  if @state.respond_to? :diff
    diff = @_state.diff(@state)
    diff.each {|k,v| @env['rack.session'].delete(k) }
  end
  #super
  until_to_a_gets_into_camping
end

#until_to_a_gets_into_campingObject

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tarpaulin/camping/flash.rb', line 49

def until_to_a_gets_into_camping #:nodoc:
  # overwrite the session with how @state is now
  # depeding on whether diff is defined for H or not
  if @state.respond_to? :diff
    @env['rack.session'].merge! Hash[@state]
  else
    @env['rack.session'] = Hash[@state]
  end
  r = Rack::Response.new(@body, @status, @headers)
  @cookies._n.each do |k, v|
    r.set_cookie(k, v)
  end
  r.to_a
end