merb_has_flash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2004-2006 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,6 @@
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.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ PLUGIN = "merb_has_flash"
5
+ NAME = "merb_has_flash"
6
+ VERSION = "0.0.1"
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', '>= 0.4.0')
24
+ s.require_path = 'lib'
25
+ s.autorequire = PLUGIN
26
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ task :install => [:package] do
34
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
35
+ 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,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::ViewContext.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,37 @@
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
+ protected
13
+ def sweep_flash
14
+ flash.sweep if @_session
15
+ end
16
+
17
+ # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
18
+ # <tt>flash["notice"] = "hello"</tt> to put a new one.
19
+ # Note that if sessions are disabled only flash.now will work.
20
+ def flash(refresh = false) #:doc:
21
+ if !defined?(@_flash) || refresh
22
+ @_flash =
23
+ if session.is_a?(Hash)
24
+ # don't put flash in session if disabled
25
+ FlashHash.new
26
+ else
27
+ # otherwise, session is a CGI::Session or a TestSession
28
+ # so make sure it gets retrieved from/saved to session storage after request processing
29
+ session["flash"] ||= FlashHash.new
30
+ end
31
+ end
32
+
33
+ @_flash
34
+ end
35
+ end
36
+ end
37
+ 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.send :flash
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: merb_has_flash
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-10-27 00:00:00 -05:00
8
+ summary: Merb plugin that provides a Rails-style flash
9
+ require_paths:
10
+ - lib
11
+ email: ivey@gweezlebur.com
12
+ homepage: http://merb-plugins.rubyforge.org/merb_has_flash/
13
+ rubyforge_project:
14
+ description: Merb plugin that provides a Rails-style flash
15
+ autorequire: merb_has_flash
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Michael Ivey
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - TODO
36
+ - lib/merb_has_flash
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
+ test_files: []
42
+
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files:
46
+ - README
47
+ - LICENSE
48
+ - TODO
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ requirements: []
54
+
55
+ dependencies:
56
+ - !ruby/object:Gem::Dependency
57
+ name: merb
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Version::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.4.0
64
+ version: