Class: Ext::Flash::Flasher

Inherits:
Object show all
Defined in:
lib/ext/flash.rb

Overview

The flash class. Stores the data temporarily in the cookies. Only store Strings.

Instance Method Summary collapse

Constructor Details

#initialize(cookies_in, cookies_out) ⇒ Flasher

Returns a new instance of Flasher.



61
62
63
64
65
66
# File 'lib/ext/flash.rb', line 61

def initialize(cookies_in, cookies_out)
  @in, @out, @tmp = cookies_in.dup, cookies_out
  @in.each do |k,v|
    @in[k] = nil if v.empty?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Allows fancy flash.xy or flash.xy = ‘sdsd’



82
83
84
85
86
87
88
89
# File 'lib/ext/flash.rb', line 82

def method_missing(m, *args)
  if /=$/ =~ m.to_s
    send(:[]=, m.to_s.sub(/=$/,''), *args)
  else
    raise NoMethodError, "Cannot pass arguments to #{m}, use = to assign data" unless args.empty?
    send(:[], m)
  end
end

Instance Method Details

#[](key) ⇒ Object



68
69
70
71
72
# File 'lib/ext/flash.rb', line 68

def [](key)
  key = convert_key(key)
  @out[key] = '' if @out.has_key? key
  return @in[key]
end

#[]=(key, value) ⇒ Object



74
75
76
77
78
79
# File 'lib/ext/flash.rb', line 74

def []=(key, value)
  unless value.nil? or value.empty?
    key = convert_key(key)
    @in[key] = @out[key] = value
  end
end