Skip to content

Commit

Permalink
Add Lotus::Application.load! to eager load configuration and allow on…
Browse files Browse the repository at this point in the history
…e file architecture
  • Loading branch information
jodosha committed Jul 1, 2014
1 parent d4ed542 commit 69a28ac
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ module OneFile
get '/', to: 'home#index'
end
end

load!
end

module Controllers
Expand Down
45 changes: 42 additions & 3 deletions lib/lotus/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,43 @@ def self.configure(&blk)
#
# @since 0.1.0
def initialize
@loader = Lotus::Loader.new(self)
@loader.load!

self.class.load!(self)
@rendering_policy = RenderingPolicy.new(configuration)
end

# Eager load the application configuration, by activating the framework
# duplication mechanisms.
#
# @since 0.1.1
#
# @example
# require 'lotus'
#
# module OneFile
# class Application < Lotus::Application
# configure do
# routes do
# get '/', to: 'dashboard#index'
# end
# end
#
# load!
# end
#
# module Controllers::Dashboard
# include Bookshelf::Controller
#
# action 'Index' do
# def call(params)
# self.body = 'Hello!'
# end
# end
# end
# end
def self.load!(recipient = self)
Lotus::Loader.new(recipient).load!
end

# Return the configuration for this application
#
# @since 0.1.0
Expand All @@ -89,6 +120,14 @@ def configuration
self.class.configuration
end

# Return the application name
#
# @since 0.1.1
# @api private
def name
self.class.name
end

# Process a request.
# This method makes Lotus applications compatible with the Rack protocol.
#
Expand Down
20 changes: 13 additions & 7 deletions lib/lotus/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def load_frameworks!

def load_application!
configuration.load_paths.load!(configuration.root)
load_rack!
end

def finalize!
application_module.module_eval %{
#{ application_module }::View.load!
}
end

def load_rack!
return if application.is_a?(Class)

namespace = configuration.namespace || application_module

resolver = Lotus::Routing::EndpointResolver.new(pattern: configuration.controller_pattern, namespace: namespace)
Expand All @@ -70,22 +82,16 @@ def load_application!
)

application.middleware # preload
end

def finalize!
unless application_module.const_defined?('Routes')
routes = Lotus::Routes.new(application.routes)
application_module.const_set('Routes', routes)
end

application_module.module_eval %{
#{ application_module }::View.load!
}
end

def application_module
@application_module ||= Utils::Class.load!(
Utils::String.new(application.class).namespace
Utils::String.new(application.name).namespace
)
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
@application.routes.must_be_kind_of(Lotus::Router)
end
end

describe '#name' do
it 'returns the class name' do
@application.name.must_equal @application.class.name
end
end
end
21 changes: 21 additions & 0 deletions test/fixtures/one_file/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module OneFile
class Application < Lotus::Application
configure do
routes do
get '/', to: 'dashboard#index'
end
end

load!
end

module Controllers::Dashboard
include OneFile::Controller

action 'Index' do
def call(params)
self.body = 'Hello'
end
end
end
end
33 changes: 33 additions & 0 deletions test/integration/one_file_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'
require 'rack/test'
require 'fixtures/one_file/application'

describe 'A one file Lotus application' do
include Rack::Test::Methods

before do
@current_dir = Dir.pwd
Dir.chdir FIXTURES_ROOT.join('one_file')
@app = OneFile::Application.new
end

after do
Dir.chdir @current_dir
@current_dir = nil
end

def app
@app
end

def response
last_response
end

it 'returns a successful response for the root path' do
get '/'

response.status.must_equal 200
response.body.must_equal %(Hello)
end
end

0 comments on commit 69a28ac

Please sign in to comment.