Skip to content

Commit

Permalink
Merge a799dbc into 19cd078
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis committed Jul 5, 2014
2 parents 19cd078 + a799dbc commit b4213d3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 19 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
4 changes: 1 addition & 3 deletions lib/lotus/config/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ module Config
# @since 0.1.0
# @api private
class Assets
DEFAULT_DIRECTORY = 'public'.freeze

def initialize(root, directory)
@path = root.join(directory || DEFAULT_DIRECTORY)
@path = root.join(directory)
end

def entries
Expand Down
64 changes: 58 additions & 6 deletions lib/lotus/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ def templates(value = nil)
# Sets the given value
# @param value [String] the relative path to the assets dir.
#
# @overload assets(false)
# Disables serving static assets
#
# @overload assets
# Gets the value
# @return [Lotus::Config::Assets] assets root
Expand Down Expand Up @@ -348,11 +351,29 @@ def templates(value = nil)
#
# Bookshelf::Application.configuration.assets
# # => #<Pathname:/root/path/assets>
def assets(value = nil)
if value
@assets = value
#
# @example Disabling static assets
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# configure do
# assets false
# end
# end
# end
#
# Bookshelf::Application.configuration.assets
# # => false
def assets(directory = nil)
return @assets if defined?(@assets)

if directory.nil?
@assets = Config::Assets.new(root, 'public')
elsif directory
@assets = Config::Assets.new(root, directory)
else
Config::Assets.new(root, @assets)
@assets = false
end
end

Expand Down Expand Up @@ -475,7 +496,38 @@ def routes(path = nil, &blk)
end
end

# since 0.1.0
# Application middleware.
#
# Specify middleware that your application will use. This method will return
# the application's underlying Middleware stack which you can use to add new
# middleware for your application to use. By default, the middleware stack
# will contain only `Rack::Static`. However, if `assets false` was specified
# in the configuration block, the default Middleware stack will be empty.
#
# @since 0.1.0
#
# @see http://rdoc.info/gems/rack/Rack/Static
# @see Lotus::Middleware#use
#
# @example
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# configure do
# middleware.use Rack::MethodOverride, nil, 'max-age=0, private, must-revalidate'
# middleware.use Rack::ETag
# end
# end
# end
#
#
# @since 0.1.1
def middleware
@middleware ||= Lotus::Middleware.new(self)
end

# @since 0.1.0
# @api private
def mapping(path = nil, &blk)
if path or block_given?
Expand All @@ -489,7 +541,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
49 changes: 40 additions & 9 deletions lib/lotus/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,42 @@ module Lotus
class Middleware
# 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
@stack = []

if configuration.assets
use Rack::Static, {
urls: configuration.assets.entries,
root: configuration.assets
}
end
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
@stack.each { |m, args, block| @builder.use m, *args, &block }
@builder.run application.routes

self
end

# Process a request.
Expand All @@ -38,5 +56,18 @@ 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)
@stack << [middleware, args, blk]
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
53 changes: 53 additions & 0 deletions test/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'test_helper'
require 'lotus/middleware'

Lotus::Middleware.class_eval { attr_reader :stack }

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 'contains only Rack::Static by default' do
middleware.stack.must_equal [
[
Rack::Static,
[{ urls: configuration.assets.entries, root: configuration.assets }],
nil
]
]
end

it 'does not include Rack::Static if configuration.assets is set to false' do
configuration.assets false
middleware.stack.any? { |m| m.first == Rack::Static }.must_equal false
end

describe '#use' do
it 'inserts a middleware into the stack' do
middleware.use Rack::ETag
middleware.stack.must_include [Rack::ETag, [], nil]
MockApp::Application.new.middleware.stack.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 b4213d3

Please sign in to comment.