From 69a28ac3d1dbe19f28ffdbf5e4f3df57f55f0d2e Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Tue, 1 Jul 2014 17:58:41 +0200 Subject: [PATCH] Add Lotus::Application.load! to eager load configuration and allow one file architecture --- README.md | 2 ++ lib/lotus/application.rb | 45 +++++++++++++++++++++++++-- lib/lotus/loader.rb | 20 +++++++----- test/application_test.rb | 6 ++++ test/fixtures/one_file/application.rb | 21 +++++++++++++ test/integration/one_file_test.rb | 33 ++++++++++++++++++++ 6 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/one_file/application.rb create mode 100644 test/integration/one_file_test.rb diff --git a/README.md b/README.md index a09c85dee..133af8a88 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ module OneFile get '/', to: 'home#index' end end + + load! end module Controllers diff --git a/lib/lotus/application.rb b/lib/lotus/application.rb index 3263e1207..6484a3533 100644 --- a/lib/lotus/application.rb +++ b/lib/lotus/application.rb @@ -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 @@ -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. # diff --git a/lib/lotus/loader.rb b/lib/lotus/loader.rb index e6c9a916b..bb58aff2f 100644 --- a/lib/lotus/loader.rb +++ b/lib/lotus/loader.rb @@ -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) @@ -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 diff --git a/test/application_test.rb b/test/application_test.rb index f8762019b..88a9016e9 100644 --- a/test/application_test.rb +++ b/test/application_test.rb @@ -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 diff --git a/test/fixtures/one_file/application.rb b/test/fixtures/one_file/application.rb new file mode 100644 index 000000000..d812c838b --- /dev/null +++ b/test/fixtures/one_file/application.rb @@ -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 diff --git a/test/integration/one_file_test.rb b/test/integration/one_file_test.rb new file mode 100644 index 000000000..07b29ac04 --- /dev/null +++ b/test/integration/one_file_test.rb @@ -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