nakajima-rack-flash 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rack/flash.rb +61 -58
  2. metadata +1 -11
data/lib/rack/flash.rb CHANGED
@@ -1,33 +1,46 @@
1
- require 'metaid'
2
-
3
1
  module Rack
4
2
  class Flash
3
+ # Raised when the session passed to FlashHash initialize is nil. This
4
+ # is usually an indicator that session middleware is not in use.
5
5
  class SessionUnavailable < StandardError; end
6
-
6
+
7
7
  # Implements bracket accessors for storing and retrieving flash entries.
8
8
  class FlashHash
9
9
  attr_reader :flagged
10
-
10
+
11
11
  def initialize(store, opts={})
12
12
  raise Rack::Flash::SessionUnavailable \
13
13
  .new('Rack::Flash depends on session middleware.') unless store
14
-
14
+
15
15
  @opts = opts
16
16
  @store = store
17
17
  @store[:__FLASH__] ||= {}
18
+
19
+ if accessors = @opts[:accessorize]
20
+ accessors.each { |opt| def_accessor(opt) }
21
+ end
18
22
  end
19
23
 
20
24
  # Remove an entry from the session and return its value. Cache result in
21
25
  # the instance cache.
22
26
  def [](key)
23
- get(key.to_sym)
27
+ key = key.to_sym
28
+ cache[key] ||= values.delete(key)
24
29
  end
25
30
 
26
31
  # Store the entry in the session, updating the instance cache as well.
27
32
  def []=(key,val)
28
- set(key.to_sym, val)
33
+ key = key.to_sym
34
+ cache[key] = values[key] = val
35
+ end
36
+
37
+ # Store a flash entry for only the current request, swept regardless of
38
+ # whether or not it was actually accessed. Useful for AJAX requests, where
39
+ # you want a flash message, even though you're response isn't redirecting.
40
+ def now
41
+ cache
29
42
  end
30
-
43
+
31
44
  # Checks for the presence of a flash entry without retrieving or removing
32
45
  # it from the cache or store.
33
46
  def has?(key)
@@ -38,62 +51,29 @@ module Rack
38
51
  def flag!
39
52
  @flagged = values.keys
40
53
  end
41
-
54
+
42
55
  # Remove flagged entries from flash session, clear flagged list.
43
56
  def sweep!
44
57
  Array(flagged).each { |key| values.delete(key) }
45
58
  flagged.clear
46
59
  end
47
-
60
+
48
61
  # Hide the underlying :__FLASH__ session key and only expose values stored
49
62
  # in the flash.
50
63
  def inspect
51
64
  '#<FlashHash @values=%s>' % [values.inspect]
52
65
  end
53
-
66
+
54
67
  # Human readable for logging.
55
68
  def to_s
56
69
  values.inspect
57
70
  end
58
-
59
- # Allow more convenient style for accessing flash entries (This isn't really
60
- # necessary for Sinatra, since it provides the flash[:foo] hash that we're all
61
- # used to. This is for vanilla Rack apps where it can be difficult to define
62
- # such helpers as middleware).
63
- def method_missing(sym, *args)
64
- super unless @opts[:accessorize]
65
- key = sym.to_s =~ /\w=$/ ? sym.to_s[0..-2] : sym
66
- def_accessors(key)
67
- send(sym, *args)
68
- end
69
71
 
70
72
  private
71
-
72
- def get(key)
73
- raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
74
- cache[key] ||= values.delete(key)
75
- end
76
-
77
- def set(key, val)
78
- raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
79
- cache[key] = values[key] = val
80
- end
81
-
82
- def def_accessors(key)
83
- return if respond_to?(key)
84
-
85
- meta_def(key) do
86
- self[key]
87
- end
88
-
89
- meta_def("#{key}=") do |val|
90
- self[key] = val
91
- end
92
- end
93
73
 
94
- # Maintain an instance-level cache of retrieved flash entries. These entries
95
- # will have been removed from the session, but are still available through
96
- # the cache.
74
+ # Maintain an instance-level cache of retrieved flash entries. These
75
+ # entries will have been removed from the session, but are still available
76
+ # through the cache.
97
77
  def cache
98
78
  @cache ||= {}
99
79
  end
@@ -103,23 +83,46 @@ module Rack
103
83
  def values
104
84
  @store[:__FLASH__]
105
85
  end
86
+
87
+ # Generate accessor methods for the given entry key if :accessorize is true.
88
+ def def_accessor(key)
89
+ raise ArgumentError.new('Invalid entry type: %s' % key) if respond_to?(key)
90
+
91
+ class << self; self end.class_eval do
92
+ define_method(key) { |*args| val = args.first; val ? (self[key]=val) : self[key] }
93
+ define_method("#{key}=") { |val| self[key] = val }
94
+ define_method("#{key}!") { |val| cache[key] = val }
95
+ end
96
+ end
106
97
  end
107
-
98
+
99
+ # -------------------------------------------------------------------------
100
+ # - Rack Middleware implementation
101
+
108
102
  def initialize(app, opts={})
103
+ if defined?(Sinatra::Base)
104
+ Sinatra::Base.class_eval do
105
+ def flash; env['rack-flash'] end
106
+ end
107
+ end
108
+
109
109
  @app, @opts = app, opts
110
110
  end
111
111
 
112
112
  def call(env)
113
- env['rack-flash'] = Rack::Flash::FlashHash.new(env['rack.session'], @opts)
114
- @app.call(env)
115
- end
116
- end
117
- end
113
+ env['rack-flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts)
114
+
115
+ if @opts[:sweep]
116
+ env['rack-flash'].flag!
117
+ end
118
+
119
+ res = @app.call(env)
118
120
 
119
- if defined?(Sinatra::Base)
120
- Sinatra::Base.class_eval do
121
- def flash
122
- env['rack-flash']
121
+ if @opts[:sweep]
122
+ env['rack-flash'].sweep!
123
+ end
124
+
125
+ res
123
126
  end
124
127
  end
125
- end
128
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-rack-flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima
@@ -22,16 +22,6 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
- - !ruby/object:Gem::Dependency
26
- name: metaid
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
34
- version:
35
25
  description:
36
26
  email: patnakajima@gmail.com
37
27
  executables: []