Skip to content

Commit

Permalink
Merge 5eedf69 into 19cd078
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis committed Jul 4, 2014
2 parents 19cd078 + 5eedf69 commit c3ec7a8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/lotus/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def call(env)
#
# @see Lotus::Middleware
def middleware
@middleware ||= Lotus::Middleware.new(self)
@middleware ||= configuration.middleware.load!(self)
end
end
end
10 changes: 7 additions & 3 deletions lib/lotus/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def templates(value = nil)
# # => #<Pathname:/root/path/assets>
def assets(value = nil)
if value
@assets = value
@assets = Config::Assets.new(root, value)
else
Config::Assets.new(root, @assets)
@assets ||= Config::Assets.new(root, nil)
end
end

Expand Down Expand Up @@ -475,6 +475,10 @@ def routes(path = nil, &blk)
end
end

def middleware
@middleware ||= Lotus::Middleware.new(self)
end

# since 0.1.0
# @api private
def mapping(path = nil, &blk)
Expand All @@ -489,7 +493,7 @@ def mapping(path = nil, &blk)
# requirement for the mime type.
#
# The given format must be coercible to a symbol, and be a valid mime type
# alias. If it isn't, at the runtime the framework will raise a
# alias. If it isn't, at the runtime the framework will raise a
# `Lotus::Controller::UnknownFormatError`.
#
# By default this value is `:html`.
Expand Down
67 changes: 58 additions & 9 deletions lib/lotus/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@ module Lotus
# @since 0.1.0
# @api private
class Middleware
include Enumerable

# Instantiate a middleware stack
#
# @param application [Lotus::Application] the application
# @param configuration [Lotus::Configuration] the application's configuration
#
# @return [Lotus::Middleware] the new stack
#
# @since 0.1.0
# @api private
#
# @see Lotus::Configuration
# @see http://rdoc.info/gems/rack/Rack/Builder
def initialize(application)
configuration = application.configuration
routes = application.routes
def initialize(configuration)
# Initialize the default Middleware stack
@middlewares = [
[
Rack::Static,
{ urls: configuration.assets.entries, root: configuration.assets }
]
]
end

# Load the middleware stack
#
# @param application [Lotus::Application] the application loading the middleware
#
# @return [Lotus::Middleware] the loaded middleware stack
#
# @since 0.1.0
# @api private
#
# @see http://rdoc.info/gems/rack/Rack/Builder
def load!(application)
@builder = ::Rack::Builder.new
@builder.use Rack::Static,
urls: configuration.assets.entries,
root: configuration.assets
@builder.run routes
@middlewares.each { |m| @builder.use(*m) }
@builder.run application.routes

self
end

# Process a request.
Expand All @@ -38,5 +56,36 @@ def initialize(application)
def call(env)
@builder.call(env)
end

# Add a middleware to the stack.
#
# @param middleware [Object] a Rack middleware
# @param *args [Array] optional arguments to pass to the Rack middleware
# @param &blk [Proc] an optional block to pass to the Rack middleware
#
# @return [Array] the middleware that was added
#
# @since 0.1.0
def use(middleware, *args, &blk)
@middlewares << [middleware, args, blk]
end

# Iterate over the middleware stack.
#
# @return [Array] the list of middlewares
#
# @since 0.1.0
def each
@middlewares.each { |m| yield m }
end

# Retrieve a middleware by its index in the stack.
#
# @return [Array] the requested middleware
#
# @since 0.1.0
def [](i)
@middlewares[i]
end
end
end
6 changes: 6 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ def to_pathname
end
end

describe '#middleware' do
it 'returns a new instance of Lotus::Middleware' do
@configuration.middleware.must_be_instance_of Lotus::Middleware
end
end

# describe '#mapping' do
# describe 'when a block is given' do
# let(:mapping) { Proc.new { collection :customers do; end } }
Expand Down
43 changes: 43 additions & 0 deletions test/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test_helper'
require 'lotus/middleware'

describe Lotus::Middleware do
before do
module MockApp
class Application < Lotus::Application; end
end
end

let(:configuration) { MockApp::Application.configuration }
let(:middleware) { configuration.middleware }

it 'includes Rack::Static by default' do
middleware.must_include [
Rack::Static,
{ urls: configuration.assets.entries, root: configuration.assets }
]
end

describe '#use' do
it 'inserts a middleware into the stack' do
middleware.use Rack::ETag
middleware.must_include [Rack::ETag, [], nil]
MockApp::Application.new.middleware.must_include [Rack::ETag, [], nil]
end
end

describe '#load' do
it 'loads the middleware into a Rack::Builder' do
middleware.use Rack::ETag
middleware.load!(MockApp::Application.new)
builder = middleware.instance_variable_get(:@builder)

builder.instance_variable_get(:@use).size.must_equal 2
end
end

after do
MockApp.send(:remove_const, :Application)
Object.send(:remove_const, :MockApp)
end
end

0 comments on commit c3ec7a8

Please sign in to comment.