rack-flash2 0.0.1

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.
@@ -0,0 +1,14 @@
1
+ module Rack
2
+ class Flash
3
+ def self.fake_session
4
+ @fake_session ||= {}
5
+ end
6
+
7
+ alias_method :old_call, :call
8
+ def new_call(env)
9
+ env['rack.session'] ||= Rack::Flash.fake_session
10
+ old_call(env)
11
+ end
12
+ alias_method :call, :new_call
13
+ end
14
+ end
data/lib/rack/flash.rb ADDED
@@ -0,0 +1,113 @@
1
+ module Rack
2
+ class Builder
3
+ attr :ins
4
+ def use(middleware, *args, &block)
5
+ middleware.instance_variable_set "@rack_builder", self
6
+ def middleware.rack_builder
7
+ @rack_builder
8
+ end
9
+ @ins << lambda { |app|
10
+ middleware.new(app, *args, &block)
11
+ }
12
+ end
13
+
14
+ def run(app)
15
+ klass = app.class
16
+ klass.instance_variable_set "@rack_builder", self
17
+ def klass.rack_builder
18
+ @rack_builder
19
+ end
20
+ @ins << app #lambda { |nothing| app }
21
+ end
22
+
23
+ def leaf_app
24
+ ins.last
25
+ end
26
+ end
27
+ end
28
+
29
+ module Rack
30
+ class Flash
31
+ # Raised when the session passed to FlashHash initialize is nil. This
32
+ # is usually an indicator that session middleware is not in use.
33
+ class SessionUnavailable < StandardError; end
34
+
35
+ # Implements bracket accessors for storing and retrieving flash entries.
36
+ class FlashHash
37
+ attr_reader :flagged
38
+
39
+ def initialize(store, opts={})
40
+ raise Rack::Flash::SessionUnavailable \
41
+ .new('Rack::Flash depends on session middleware.') unless store
42
+
43
+ @opts = opts
44
+ @store = store
45
+ end
46
+
47
+ # Remove an entry from the session and return its value. Cache result in
48
+ # the instance cache.
49
+ def [](key)
50
+ key = key.to_sym
51
+ values[key]
52
+ end
53
+
54
+ # Store the entry in the session, updating the instance cache as well.
55
+ def []=(key,val)
56
+ key = key.to_sym
57
+ values[key] = val
58
+ end
59
+
60
+ # Remove flagged entries from flash session, clear flagged list.
61
+ def sweep!
62
+ values.clear
63
+ end
64
+
65
+ # Hide the underlying :__FLASH__ session key and only expose values stored
66
+ # in the flash.
67
+ def inspect
68
+ '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect]
69
+ end
70
+
71
+ # Human readable for logging.
72
+ def to_s
73
+ values.inspect
74
+ end
75
+
76
+ private
77
+
78
+ # Helper to access flash entries from :__FLASH__ session value. This key
79
+ # is used to prevent collisions with other user-defined session values.
80
+ def values
81
+ @store[:__FLASH__] ||= {}
82
+ end
83
+ end
84
+
85
+ # -------------------------------------------------------------------------
86
+ # - Rack Middleware implementation
87
+
88
+ def initialize(app, opts={})
89
+ if klass = app_class(app, opts)
90
+ klass.class_eval do
91
+ def flash; env['x-rack.flash'] end
92
+ end
93
+ end
94
+
95
+ @app, @opts = app, opts
96
+ end
97
+
98
+ def call(env)
99
+ env['x-rack.flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts)
100
+
101
+ @app.call(env)
102
+ end
103
+
104
+ private
105
+
106
+ def app_class(app, opts)
107
+ return nil if opts.has_key?(:helper) and not opts[:helper]
108
+ opts[:flash_app_class] ||
109
+ defined?(Sinatra::Base) && Sinatra::Base ||
110
+ self.class.rack_builder.leaf_app.class
111
+ end
112
+ end
113
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), *%w[rack flash])
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-flash2
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Adrien Husson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-20 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description:
34
+ email: pangel.neu@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/rack-flash2.rb
43
+ - lib/rack/flash.rb
44
+ - lib/rack/flash/test.rb
45
+ has_rdoc: true
46
+ homepage: https://github.com/pangel/rack-flash2
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: 2nd Flash hash implementation for Rack apps.
77
+ test_files: []
78
+