ivey-merb_has_flash 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Original code Copyright (c) 2007 Michael D. Ivey
2
+
3
+ Most of this code is based on the Flash code in Rails/ActionPack:
4
+ Copyright (c) 2004-2006 David Heinemeier Hansson
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
data/README ADDED
@@ -0,0 +1,15 @@
1
+ merb_has_flash
2
+ =================
3
+
4
+ A plugin for the Merb framework that provides Rails-style flash.
5
+
6
+ Most of the code is taken directly from Rails, which is
7
+ Copyright (c) 2004-2006 David Heinemeier Hansson.
8
+ This plugin is released under the same terms as Rails itself.
9
+
10
+ Hacked together by Michael Ivey <ivey@gweezlebur.com> ... send bug
11
+ reports and patches to him.
12
+
13
+ Contributors:
14
+ Tim Kofol
15
+ Shay Arnett
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ task :install => [:package] do
5
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
6
+ end
7
+
8
+ task :release => :package do
9
+ sh %{rubyforge add_release merb-plugins merb_has_flash #{VERSION} pkg/#{NAME}-#{VERSION}.gem}
10
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ TODO:
2
+ Fix LICENSE with your name
3
+ Fix Rakefile with your name and contact info
4
+ Add your code to lib/merb_has_flash.rb
5
+ Add your Merb rake tasks to lib/merb_has_flash/merbtasks.rb
@@ -0,0 +1,38 @@
1
+ module MerbHasFlash
2
+ module ControllerExtension
3
+ def self.included(base)
4
+ base.send :include, InstanceMethods
5
+
6
+ base.class_eval {
7
+ after :sweep_flash
8
+ }
9
+ end
10
+
11
+ 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
+
28
+ @_flash
29
+ end
30
+
31
+ protected
32
+ def sweep_flash
33
+ flash.sweep if request.session
34
+ session["flash"] = flash
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,101 @@
1
+ module MerbHasFlash
2
+ class FlashNow #:nodoc:
3
+ def initialize(flash)
4
+ @flash = flash
5
+ end
6
+
7
+ def []=(k, v)
8
+ @flash[k] = v
9
+ @flash.discard(k)
10
+ v
11
+ end
12
+
13
+ def [](k)
14
+ @flash[k]
15
+ end
16
+ end
17
+
18
+ class FlashHash < Hash
19
+ def initialize #:nodoc:
20
+ super
21
+ @used = {}
22
+ end
23
+
24
+ def []=(k, v) #:nodoc:
25
+ keep(k)
26
+ super
27
+ end
28
+
29
+ def update(h) #:nodoc:
30
+ h.keys.each{ |k| discard(k) }
31
+ super
32
+ end
33
+
34
+ alias :merge! :update
35
+
36
+ def replace(h) #:nodoc:
37
+ @used = {}
38
+ super
39
+ end
40
+
41
+ # Sets a flash that will not be available to the next action, only to the current.
42
+ #
43
+ # flash.now[:message] = "Hello current action"
44
+ #
45
+ # This method enables you to use the flash as a central messaging system in your app.
46
+ # When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
47
+ # When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
48
+ # vanish when the current action is done.
49
+ #
50
+ # Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
51
+ def now
52
+ FlashNow.new self
53
+ end
54
+
55
+ # Keeps either the entire current flash or a specific flash entry available for the next action:
56
+ #
57
+ # 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)
61
+ end
62
+
63
+ # Marks the entire flash or a single flash entry to be discarded by the end of the current action
64
+ #
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)
69
+ end
70
+
71
+ # Mark for removal entries that were kept, and delete unkept ones.
72
+ #
73
+ # This method is called automatically by filters, so you generally don't need to care about it.
74
+ 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
85
+ 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
100
+ end
101
+ end
@@ -0,0 +1,7 @@
1
+ module MerbHasFlash
2
+ module FlashHelperMixin
3
+ def flash
4
+ @web_controller.flash
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ unless defined?(Merb::Plugins)
2
+ raise %q{merb_has_flash says, "Something's not right, bub. You should run me inside Merb, or at least cheat and define a Merb::Plugins constant."}
3
+ end
4
+
5
+ require 'merb_has_flash/flash_hash'
6
+
7
+ require 'merb_has_flash/controller_extension'
8
+ Merb::Controller.send :include, MerbHasFlash::ControllerExtension
9
+
10
+ require 'merb_has_flash/helper'
11
+ Merb::RenderMixin.send :include, MerbHasFlash::FlashHelperMixin
12
+
13
+ module MerbHasFlash
14
+ # The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed
15
+ # 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
17
+ # the flash to its template. Actually, that exposure is automatically done. Example:
18
+ #
19
+ # class WeblogController < Merb::Controller
20
+ # def create
21
+ # # save post
22
+ # flash[:notice] = "Successfully created post"
23
+ # redirect_to :action => "display", :params => { :id => post.id }
24
+ # end
25
+ #
26
+ # def display
27
+ # # doesn't need to assign the flash notice to the template, that's done automatically
28
+ # render
29
+ # end
30
+ # end
31
+ #
32
+ # display.html.erb
33
+ # <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
34
+ #
35
+ # This example just places a string in the flash, but you can put any object in there. And of course, you can put as many
36
+ # as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
37
+ #
38
+ # See docs on the FlashHash class for more details about the flash.
39
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "merb_has_flash Controller Extension" do
4
+ it "should copy-and-paste from rails" do
5
+
6
+ end
7
+ end
@@ -0,0 +1,24 @@
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 be a kind of Hash" do
10
+ @hash.should be_a_kind_of(Hash)
11
+ end
12
+
13
+ it "should initialize an empty @used hash" do
14
+ @hash.instance_variable_get(:@used).should == {}
15
+ end
16
+
17
+ it "should have more specs" do
18
+
19
+ end
20
+
21
+ it "should copy-and-paste most of this from Rails" do
22
+
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ivey-merb_has_flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael D. Ivey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.0
23
+ version:
24
+ description: Rails' 'flash' session notification system ported to Merb
25
+ email: ivey@gweezlebur.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - LICENSE
34
+ - Rakefile
35
+ - README
36
+ - TODO
37
+ - lib/merb_has_flash.rb
38
+ - lib/merb_has_flash/controller_extension.rb
39
+ - lib/merb_has_flash/flash_hash.rb
40
+ - lib/merb_has_flash/helper.rb
41
+ - spec/merb_has_flash/controller_extension_spec.rb
42
+ - spec/merb_has_flash/flash_hash_spec.rb
43
+ - spec/spec_helper.rb
44
+ has_rdoc: false
45
+ homepage: http://github.com/ivey/merb_has_flash
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Rails' 'flash' session notification system ported to Merb
70
+ test_files:
71
+ - spec/merb_has_flash/controller_extension_spec.rb
72
+ - spec/merb_has_flash/flash_hash_spec.rb
73
+ - spec/spec_helper.rb