joshbuddy-rack-capabilities 0.0.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.
@@ -0,0 +1,49 @@
1
+ = Rack::Capabilities
2
+
3
+ == Usage
4
+
5
+ class CuriousMiddleware
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def curious?
11
+ true #and how!
12
+ end
13
+
14
+ def call(env)
15
+ # who is before me?
16
+ env['rack.capabilities'].before(self)
17
+ # returns Rack::Middleware2
18
+
19
+ # who is after me?
20
+ env['rack.capabilities'].after(self)
21
+ # returns Rack::Middleware4
22
+
23
+ # can I go find Rack::Middleware1?
24
+ env['rack.capabilities'].find(Rack::Middleware1)
25
+ # returns Rack::Middleware1
26
+
27
+ # or just find based on anything!
28
+ env['rack.capabilities'].find{|mw| mw.respond_to?(:curious?)}
29
+ # returns our friend, CuriousMiddleware
30
+
31
+ # And presumably you'd
32
+ # want to do stuff..
33
+ # but thats up to you
34
+
35
+ @app.call(env)
36
+ end
37
+
38
+ end
39
+
40
+ use Rack::Capabilities
41
+ use Rack::Middleware1
42
+ use Rack::Middleware2
43
+ use Rack::CuriousMiddleware
44
+ use Rack::Middleware4
45
+ use Rack::Middleware5
46
+
47
+ == Caveats and Limitations
48
+
49
+ Rack::Capabilities must be required (not autoloaded as it must monkey patch Rack::Builder). The rack.capabilities environment variable is *only* available at <tt>call</tt> time.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 2
@@ -0,0 +1,52 @@
1
+ class Rack::Builder
2
+ def use(middleware, *args, &block)
3
+ @capabilities ||= []
4
+ @ins << lambda { |app|
5
+ mw = middleware.new(app, *args, &block)
6
+ @capabilities.unshift(mw)
7
+ mw.list = @capabilities if Rack::Capabilities === mw
8
+ mw
9
+ }
10
+ end
11
+ end
12
+
13
+ module Rack
14
+
15
+ class Capabilities
16
+
17
+ attr_accessor :list
18
+
19
+ def initialize(app)
20
+ @app = app
21
+ @list = []
22
+ end
23
+
24
+ def call(env)
25
+ env['rack.capabilities'] = self
26
+ @app.call(env)
27
+ end
28
+
29
+ def each(&block)
30
+ list.each {|app| block.call(app) }
31
+ end
32
+
33
+ def find(clazz = nil, &block)
34
+ clazz ? list.find { |mw| clazz === mw } : list.find(&block)
35
+ end
36
+
37
+ def before(mw, distance = 1)
38
+ relative(mw, -distance)
39
+ end
40
+
41
+ def after(mw, distance = 1)
42
+ relative(mw, distance)
43
+ end
44
+
45
+ def relative(mw, pos)
46
+ index = list.index(mw)
47
+ index ? list[index + pos] : nil
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ GenericMiddleware = Class.new do
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ @app.call(env)
10
+ end
11
+ end
12
+
13
+ GenericMiddleware1 = Class.new(GenericMiddleware)
14
+ GenericMiddleware2 = Class.new(GenericMiddleware)
15
+ GenericMiddleware3 = Class.new(GenericMiddleware)
16
+ GenericMiddleware4 = Class.new(GenericMiddleware)
17
+ GenericMiddleware5 = Class.new(GenericMiddleware)
18
+
19
+ describe "Rack Capabilities" do
20
+
21
+ def app(&block)
22
+ @builder = Rack::Builder.new {
23
+ use Rack::Capabilities
24
+ use GenericMiddleware1
25
+ use GenericMiddleware2
26
+ use GenericMiddleware3
27
+ use GenericMiddleware4
28
+ use GenericMiddleware5
29
+ run lambda { |env| block[env]; [200, {}, []] }
30
+ }
31
+
32
+ @builder.call({})
33
+ end
34
+
35
+ it "should find a middleware by class" do
36
+ app do |env|
37
+ env['rack.capabilities'].find(GenericMiddleware3).class.should == GenericMiddleware3
38
+ end
39
+ end
40
+
41
+ it "should find a middleware by proc" do
42
+ app do |env|
43
+ env['rack.capabilities'].find{|mw| mw.class.to_s == 'GenericMiddleware2'}.class.should == GenericMiddleware2
44
+ end
45
+ end
46
+
47
+ it "should find a middleware before" do
48
+ app do |env|
49
+ env['rack.capabilities'].before(env['rack.capabilities'].find(GenericMiddleware3)).class.should == GenericMiddleware2
50
+ end
51
+ end
52
+
53
+ it "should find a middleware after" do
54
+ app do |env|
55
+ env['rack.capabilities'].after(env['rack.capabilities'].find(GenericMiddleware4)).class.should == GenericMiddleware5
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec'
2
+ require 'rubygems'
3
+ require 'rack'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'rack/capabilities'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joshbuddy-rack-capabilities
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Hull
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: joshbuddy@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - VERSION.yml
27
+ - lib/rack
28
+ - lib/rack/capabilities.rb
29
+ - spec/rack_capabilities_spec.rb
30
+ - spec/spec_helper.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/joshbuddy/rack-capabilities
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Discover rack middleware
58
+ test_files: []
59
+