Skip to content

Commit

Permalink
Register Lotus::Application subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Dec 16, 2014
1 parent cebf626 commit a06fef2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/lotus/application.rb
Expand Up @@ -33,6 +33,22 @@ def self.inherited(base)
class_attribute :configuration
self.configuration = Configuration.new
end

synchronize do
applications.add(base)
end
end

# Registry of Lotus applications in the current Ruby process
#
# @return [Set] a set of all the registered applications
#
# @since x.x.x
# @api private
def self.applications
synchronize do
@@applications ||= Set.new
end
end

# Configure the application.
Expand Down Expand Up @@ -129,6 +145,17 @@ def self.load!(application = self)
Lotus::Loader.new(application).load!
end

# Preload all the registered applications
#
# @return [void]
#
# @since x.x.x
def self.preload!
synchronize do
applications.each(&:load!)
end
end

# Return the configuration for this application
#
# @since 0.1.0
Expand Down Expand Up @@ -174,5 +201,17 @@ def call(env)
def middleware
@middleware ||= configuration.middleware
end

private

# Yields the given block in a critical section
#
# @since x.x.x
# @api private
def self.synchronize
Mutex.new.synchronize do
yield
end
end
end
end
34 changes: 34 additions & 0 deletions test/application_test.rb
Expand Up @@ -5,6 +5,10 @@
@application = CoffeeShop::Application.new
end

it 'register subclasses' do
Lotus::Application.applications.must_include(CoffeeShop::Application)
end

it 'instantiate new configuration object when inherited' do
backend_app = Backend::Application.new
frontend_app = Frontend::Application.new
Expand All @@ -21,6 +25,36 @@
end
end

describe '.preload!' do
before do
Lotus::Application.applications.clear

module Foo
class Application < Lotus::Application
def self.load!(application = self)
@@loaded = true
end

def self.loaded?
@@loaded
end

configure { }
end
end

Lotus::Application.preload!
end

after do
Object.__send__(:remove_const, :Foo)
end

it 'preloads registered subclasses' do
Foo::Application.must_be :loaded?
end
end

describe '#configuration' do
it 'returns class configuration' do
@application.configuration.must_equal @application.class.configuration
Expand Down

0 comments on commit a06fef2

Please sign in to comment.