ivey-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 CHANGED
@@ -10,6 +10,13 @@ This plugin is released under the same terms as Rails itself.
10
10
  Hacked together by Michael Ivey <ivey@gweezlebur.com> ... send bug
11
11
  reports and patches to him.
12
12
 
13
+ Official Tree: http://github.com/ivey/merb_has_flash
14
+
15
+ NOTE: Jeremy Nicoll may be taking over this codebase, and his tree is
16
+ http://github.com/eltiare/merb_has_flash ... I'll try to keep master
17
+ updated from him until we decide (IE: I can convince him) to take it
18
+
13
19
  Contributors:
14
20
  Tim Kofol
15
21
  Shay Arnett
22
+ Jeremy Nicoll
data/Rakefile CHANGED
@@ -1,6 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
 
4
+ spec = eval(File.read('merb_has_flash.gemspec'))
5
+
6
+ Rake::GemPackageTask.new(spec) do |pkg|
7
+ pkg.gem_spec = spec
8
+ end
9
+
4
10
  task :install => [:package] do
5
11
  sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
6
12
  end
@@ -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
- @_flash
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
- protected
14
+
15
+ protected
32
16
  def sweep_flash
33
- flash.sweep if request.session
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 []=(k, v)
8
- @flash[k] = v
9
- @flash.discard(k)
10
- v
6
+
7
+ def []=(key, val)
8
+ @flash[key] = val
9
+ @flash.discard key
10
+ val
11
11
  end
12
-
13
- def [](k)
14
- @flash[k]
12
+
13
+ def [](key)
14
+ @flash[key]
15
15
  end
16
16
  end
17
-
18
- class FlashHash < Hash
19
- def initialize #:nodoc:
20
- super
21
- @used = {}
17
+
18
+ class FlashHash
19
+ def initialize(*args) #:nodoc:
20
+ @attrs = Hash.new(*args)
21
+ @keepers = []
22
22
  end
23
-
24
- def []=(k, v) #:nodoc:
25
- keep(k)
26
- super
23
+
24
+ def []=(key, val) #:nodoc:
25
+ @attrs[key] = val
26
+ keep key
27
27
  end
28
-
29
- def update(h) #:nodoc:
30
- h.keys.each{ |k| discard(k) }
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(h) #:nodoc:
37
- @used = {}
38
- super
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, the rest of the flash is discarded
59
- def keep(k = nil)
60
- use(k, false)
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.keep # keep entire flash available for the next action
66
- # flash.discard(:warning) # discard the "warning" entry (it'll still be available for the current action)
67
- def discard(k = nil)
68
- use(k)
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
- # Mark for removal entries that were kept, and delete unkept ones.
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 do |k|
76
- unless @used[k]
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
- private
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
- end
90
+
91
+ end
@@ -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
- require 'merb_has_flash/helper'
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
@@ -1,7 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe "merb_has_flash Controller Extension" do
4
- it "should copy-and-paste from rails" do
5
-
6
- end
4
+ it "should have specs, but doesn't..."
7
5
  end
@@ -5,20 +5,43 @@ describe "merb_has_flash FlashHash" do
5
5
  before(:each) do
6
6
  @hash = MerbHasFlash::FlashHash.new
7
7
  end
8
-
9
- it "should be a kind of Hash" do
10
- @hash.should be_a_kind_of(Hash)
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)
11
12
  end
12
-
13
- it "should initialize an empty @used hash" do
14
- @hash.instance_variable_get(:@used).should == {}
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)
15
24
  end
16
-
17
- it "should have more specs" do
18
-
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)
19
30
  end
20
-
21
- it "should copy-and-paste most of this from Rails" do
22
-
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...')
23
45
  end
46
+
24
47
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ivey-merb_has_flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael D. Ivey
8
+ - Jeremy Nicoll
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-05-06 00:00:00 -07:00
13
+ date: 2008-09-10 00:00:00 -07:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
@@ -63,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
64
  requirements: []
64
65
 
65
66
  rubyforge_project:
66
- rubygems_version: 1.0.1
67
+ rubygems_version: 1.2.0
67
68
  signing_key:
68
69
  specification_version: 2
69
70
  summary: Rails' 'flash' session notification system ported to Merb