merb_has_flash 0.9.2 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -1
- data/Rakefile +2 -25
- data/lib/merb_has_flash.rb +5 -3
- data/lib/merb_has_flash/controller_extension.rb +8 -25
- data/lib/merb_has_flash/flash_hash.rb +56 -66
- data/spec/merb_has_flash/controller_extension_spec.rb +5 -0
- data/spec/merb_has_flash/flash_hash_spec.rb +47 -0
- data/spec/spec_helper.rb +2 -0
- metadata +24 -20
data/README
CHANGED
data/Rakefile
CHANGED
@@ -1,30 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake/gempackagetask'
|
3
3
|
|
4
|
-
|
5
|
-
NAME = "merb_has_flash"
|
6
|
-
VERSION = "0.9.2"
|
7
|
-
AUTHOR = "Michael Ivey"
|
8
|
-
EMAIL = "ivey@gweezlebur.com"
|
9
|
-
HOMEPAGE = "http://merb-plugins.rubyforge.org/merb_has_flash/"
|
10
|
-
SUMMARY = "Merb plugin that provides a Rails-style flash"
|
11
|
-
|
12
|
-
spec = Gem::Specification.new do |s|
|
13
|
-
s.name = NAME
|
14
|
-
s.version = VERSION
|
15
|
-
s.platform = Gem::Platform::RUBY
|
16
|
-
s.has_rdoc = true
|
17
|
-
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
18
|
-
s.summary = SUMMARY
|
19
|
-
s.description = s.summary
|
20
|
-
s.author = AUTHOR
|
21
|
-
s.email = EMAIL
|
22
|
-
s.homepage = HOMEPAGE
|
23
|
-
s.add_dependency('merb-core', '>= 0.9.2')
|
24
|
-
s.require_path = 'lib'
|
25
|
-
s.autorequire = PLUGIN
|
26
|
-
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
|
27
|
-
end
|
4
|
+
spec = eval(File.read('merb_has_flash.gemspec'))
|
28
5
|
|
29
6
|
Rake::GemPackageTask.new(spec) do |pkg|
|
30
7
|
pkg.gem_spec = spec
|
@@ -36,4 +13,4 @@ end
|
|
36
13
|
|
37
14
|
task :release => :package do
|
38
15
|
sh %{rubyforge add_release merb-plugins merb_has_flash #{VERSION} pkg/#{NAME}-#{VERSION}.gem}
|
39
|
-
end
|
16
|
+
end
|
data/lib/merb_has_flash.rb
CHANGED
@@ -4,16 +4,18 @@ end
|
|
4
4
|
|
5
5
|
require 'merb_has_flash/flash_hash'
|
6
6
|
|
7
|
+
require 'merb_has_flash/helper'
|
8
|
+
Merb::RenderMixin.send :include, MerbHasFlash::FlashHelperMixin
|
9
|
+
|
7
10
|
require 'merb_has_flash/controller_extension'
|
8
11
|
Merb::Controller.send :include, MerbHasFlash::ControllerExtension
|
9
12
|
|
10
|
-
|
11
|
-
Merb::RenderMixin.send :include, MerbHasFlash::FlashHelperMixin
|
13
|
+
|
12
14
|
|
13
15
|
module MerbHasFlash
|
14
16
|
# The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed
|
15
17
|
# to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action
|
16
|
-
# that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can then expose
|
18
|
+
# that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can then expose
|
17
19
|
# the flash to its template. Actually, that exposure is automatically done. Example:
|
18
20
|
#
|
19
21
|
# class WeblogController < Merb::Controller
|
@@ -2,36 +2,19 @@ module MerbHasFlash
|
|
2
2
|
module ControllerExtension
|
3
3
|
def self.included(base)
|
4
4
|
base.send :include, InstanceMethods
|
5
|
-
|
6
|
-
base.class_eval {
|
7
|
-
after :sweep_flash
|
8
|
-
}
|
5
|
+
base.class_eval { after :sweep_flash }
|
9
6
|
end
|
10
|
-
|
7
|
+
|
11
8
|
module InstanceMethods
|
12
|
-
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
|
13
|
-
# <tt>flash["notice"] = "hello"</tt> to put a new one.
|
14
|
-
# Note that if sessions are disabled only flash.now will work.
|
15
|
-
def flash(refresh = false) #:doc:
|
16
|
-
if !defined?(@_flash) || refresh
|
17
|
-
@_flash =
|
18
|
-
if session.is_a?(Hash)
|
19
|
-
# don't put flash in session if disabled
|
20
|
-
FlashHash.new
|
21
|
-
else
|
22
|
-
# otherwise, session is a CGI::Session or a TestSession
|
23
|
-
# so make sure it gets retrieved from/saved to session storage after request processing
|
24
|
-
session["flash"] ||= FlashHash.new
|
25
|
-
end
|
26
|
-
end
|
27
9
|
|
28
|
-
|
10
|
+
def flash(refresh = false) #:doc:
|
11
|
+
session['flash'] = FlashHash.new unless refresh || session['flash'].is_a?(FlashHash)
|
12
|
+
session['flash']
|
29
13
|
end
|
30
|
-
|
31
|
-
|
14
|
+
|
15
|
+
protected
|
32
16
|
def sweep_flash
|
33
|
-
flash.sweep
|
34
|
-
session["flash"] = flash
|
17
|
+
flash.sweep
|
35
18
|
end
|
36
19
|
end
|
37
20
|
end
|
@@ -3,45 +3,55 @@ module MerbHasFlash
|
|
3
3
|
def initialize(flash)
|
4
4
|
@flash = flash
|
5
5
|
end
|
6
|
-
|
7
|
-
def []=(
|
8
|
-
@flash[
|
9
|
-
@flash.discard
|
10
|
-
|
6
|
+
|
7
|
+
def []=(key, val)
|
8
|
+
@flash[key] = val
|
9
|
+
@flash.discard key
|
10
|
+
val
|
11
11
|
end
|
12
|
-
|
13
|
-
def [](
|
14
|
-
@flash[
|
12
|
+
|
13
|
+
def [](key)
|
14
|
+
@flash[key]
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
18
|
-
class FlashHash
|
19
|
-
def initialize #:nodoc:
|
20
|
-
|
21
|
-
@
|
17
|
+
|
18
|
+
class FlashHash
|
19
|
+
def initialize(*args) #:nodoc:
|
20
|
+
@attrs = Hash.new(*args)
|
21
|
+
@keepers = []
|
22
22
|
end
|
23
|
-
|
24
|
-
def []=(
|
25
|
-
|
26
|
-
|
23
|
+
|
24
|
+
def []=(key, val) #:nodoc:
|
25
|
+
@attrs[key] = val
|
26
|
+
keep key
|
27
27
|
end
|
28
|
-
|
29
|
-
def
|
30
|
-
|
31
|
-
super
|
28
|
+
|
29
|
+
def [](key)
|
30
|
+
@attrs[key]
|
32
31
|
end
|
33
|
-
|
32
|
+
|
33
|
+
def update(hash) #:nodoc:
|
34
|
+
@attrs.update hash
|
35
|
+
hash.keys.each { |key| keep key }
|
36
|
+
end
|
37
|
+
|
34
38
|
alias :merge! :update
|
35
|
-
|
36
|
-
def replace(
|
37
|
-
@
|
38
|
-
|
39
|
+
|
40
|
+
def replace(hash) #:nodoc:
|
41
|
+
@attrs.replace hash
|
42
|
+
keep # keep with no args automatically clears out unused keys and keeps all used ones.
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def method_missing(method_name, *args) #:nodoc:
|
47
|
+
@attrs.send(method_name, *args)
|
39
48
|
end
|
40
|
-
|
49
|
+
|
50
|
+
|
41
51
|
# Sets a flash that will not be available to the next action, only to the current.
|
42
52
|
#
|
43
53
|
# flash.now[:message] = "Hello current action"
|
44
|
-
#
|
54
|
+
#
|
45
55
|
# This method enables you to use the flash as a central messaging system in your app.
|
46
56
|
# When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
|
47
57
|
# When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
|
@@ -49,53 +59,33 @@ module MerbHasFlash
|
|
49
59
|
#
|
50
60
|
# Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
|
51
61
|
def now
|
52
|
-
FlashNow.new self
|
62
|
+
@_fn ||= FlashNow.new self # This way, a new object is not created on every "now."
|
53
63
|
end
|
54
|
-
|
64
|
+
|
55
65
|
# Keeps either the entire current flash or a specific flash entry available for the next action:
|
56
66
|
#
|
57
67
|
# flash.keep # keeps the entire flash
|
58
|
-
# flash.keep(:notice) # keeps only the "notice" entry
|
59
|
-
def keep(
|
60
|
-
|
68
|
+
# flash.keep(:notice) # keeps only the "notice" entry
|
69
|
+
def keep(key = nil)
|
70
|
+
key.nil? ? @keepers = @attrs.keys : @keepers << key unless @keepers.include?(key)
|
61
71
|
end
|
62
|
-
|
72
|
+
|
63
73
|
# Marks the entire flash or a single flash entry to be discarded by the end of the current action
|
64
74
|
#
|
65
|
-
# flash.
|
66
|
-
# flash.discard(:warning) # discard the "warning" entry (
|
67
|
-
def discard(
|
68
|
-
|
75
|
+
# flash.discard # discard the entire hash (it'll still be available for the current action)
|
76
|
+
# flash.discard(:warning) # discard the "warning" entry (still available as above)
|
77
|
+
def discard(key = nil)
|
78
|
+
key.nil? ? @keepers = [] : @keepers.delete(key)
|
69
79
|
end
|
70
|
-
|
71
|
-
#
|
72
|
-
#
|
80
|
+
|
81
|
+
# Clear the keys that are kept, and delete the ones that are currently unkept
|
73
82
|
# This method is called automatically by filters, so you generally don't need to care about it.
|
74
83
|
def sweep #:nodoc:
|
75
|
-
keys.each
|
76
|
-
|
77
|
-
use(k)
|
78
|
-
else
|
79
|
-
delete(k)
|
80
|
-
@used.delete(k)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
(@used.keys - keys).each{|k| @used.delete k } # clean up after keys that could have been left over by calling reject! or shift on the flash
|
84
|
+
@attrs.keys.each { |key| @attrs.delete key unless @keepers.include?(key) }
|
85
|
+
discard
|
85
86
|
end
|
86
|
-
|
87
|
-
|
88
|
-
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
|
89
|
-
# use() # marks the entire flash as used
|
90
|
-
# use('msg') # marks the "msg" entry as used
|
91
|
-
# use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
|
92
|
-
# use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
|
93
|
-
def use(k=nil, v=true)
|
94
|
-
unless k.nil?
|
95
|
-
@used[k] = v
|
96
|
-
else
|
97
|
-
keys.each{|key| use key, v }
|
98
|
-
end
|
99
|
-
end
|
87
|
+
|
88
|
+
|
100
89
|
end
|
101
|
-
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'merb_has_flash/flash_hash'
|
3
|
+
|
4
|
+
describe "merb_has_flash FlashHash" do
|
5
|
+
before(:each) do
|
6
|
+
@hash = MerbHasFlash::FlashHash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be a kind of Hash" do
|
10
|
+
# Merb 0.9.6 now transmutes all Hashes in sessions into Mashes.. destroying the functionality.
|
11
|
+
@hash.should_not be_a_kind_of(Hash)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should keep appropriate variables on sweep" do
|
15
|
+
@hash[:a] = true
|
16
|
+
@hash[:b] = false
|
17
|
+
@hash[:c] = 90928
|
18
|
+
|
19
|
+
@hash.sweep
|
20
|
+
|
21
|
+
@hash[:a].should eql(true)
|
22
|
+
@hash[:b].should eql(false)
|
23
|
+
@hash[:c].should eql(90928)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not keep variables when using now" do
|
27
|
+
@hash.now[:a] = true
|
28
|
+
@hash[:b] = true
|
29
|
+
@hash.instance_variable_get(:@keepers).should_not include(:a)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should discard appropriate variables on sweep" do
|
33
|
+
@hash.now[:a] = true
|
34
|
+
@hash[:b] = false
|
35
|
+
@hash[:c] = 90928
|
36
|
+
@hash.discard(:c)
|
37
|
+
@hash[:d] = 'Sir, the smurfs are eating your lunch...'
|
38
|
+
|
39
|
+
@hash.sweep
|
40
|
+
|
41
|
+
@hash[:a].should be_nil
|
42
|
+
@hash[:b].should eql(false)
|
43
|
+
@hash[:c].should be_nil
|
44
|
+
@hash[:d].should eql('Sir, the smurfs are eating your lunch...')
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,48 +1,50 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb_has_flash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Michael Ivey
|
8
|
-
|
7
|
+
- Michael D. Ivey
|
8
|
+
- Jeremy Nicoll
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2008-
|
13
|
+
date: 2008-09-10 00:00:00 -05:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: merb-core
|
18
|
+
type: :runtime
|
17
19
|
version_requirement:
|
18
20
|
version_requirements: !ruby/object:Gem::Requirement
|
19
21
|
requirements:
|
20
|
-
- - "
|
22
|
+
- - ">"
|
21
23
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.9.
|
24
|
+
version: 0.9.0
|
23
25
|
version:
|
24
|
-
description:
|
26
|
+
description: Rails' 'flash' session notification system ported to Merb
|
25
27
|
email: ivey@gweezlebur.com
|
26
28
|
executables: []
|
27
29
|
|
28
30
|
extensions: []
|
29
31
|
|
30
|
-
extra_rdoc_files:
|
31
|
-
|
32
|
-
- LICENSE
|
33
|
-
- TODO
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
34
|
files:
|
35
35
|
- LICENSE
|
36
|
-
- README
|
37
36
|
- Rakefile
|
37
|
+
- README
|
38
38
|
- TODO
|
39
|
-
- lib/merb_has_flash
|
39
|
+
- lib/merb_has_flash.rb
|
40
40
|
- lib/merb_has_flash/controller_extension.rb
|
41
41
|
- lib/merb_has_flash/flash_hash.rb
|
42
42
|
- lib/merb_has_flash/helper.rb
|
43
|
-
-
|
44
|
-
|
45
|
-
|
43
|
+
- spec/merb_has_flash/controller_extension_spec.rb
|
44
|
+
- spec/merb_has_flash/flash_hash_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: false
|
47
|
+
homepage: http://github.com/ivey/merb_has_flash
|
46
48
|
post_install_message:
|
47
49
|
rdoc_options: []
|
48
50
|
|
@@ -63,9 +65,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
65
|
requirements: []
|
64
66
|
|
65
67
|
rubyforge_project:
|
66
|
-
rubygems_version: 1.0
|
68
|
+
rubygems_version: 1.2.0
|
67
69
|
signing_key:
|
68
70
|
specification_version: 2
|
69
|
-
summary:
|
70
|
-
test_files:
|
71
|
-
|
71
|
+
summary: Rails' 'flash' session notification system ported to Merb
|
72
|
+
test_files:
|
73
|
+
- spec/merb_has_flash/controller_extension_spec.rb
|
74
|
+
- spec/merb_has_flash/flash_hash_spec.rb
|
75
|
+
- spec/spec_helper.rb
|