lib/hanami.rb in hanami-2.0.0.beta1.1 vs lib/hanami.rb in hanami-2.0.0.beta2
- old
+ new
@@ -7,10 +7,61 @@
# @see http://hanamirb.org
module Hanami
@_mutex = Mutex.new
@_bundled = {}
+ # Finds and loads the Hanami app file (`config/app.rb`).
+ #
+ # Raises an exception if the app file cannot be found.
+ #
+ # @return [Hanami::App] the loaded app class
+ #
+ # @api public
+ # @since 2.0.0
+ def self.setup(raise_exception: true)
+ return app if app?
+
+ app_path = self.app_path
+
+ if app_path
+ require(app_path)
+ app
+ elsif raise_exception
+ raise(
+ AppLoadError,
+ "Could not locate your Hanami app file.\n\n" \
+ "Your app file should be at `config/app.rb` in your project's root directory."
+ )
+ end
+ end
+
+ # Finds and returns the absolute path for the Hanami app file (`config/app.rb`).
+ #
+ # Searches within the given directory, then searches upwards through parent directories until the
+ # app file can be found.
+ #
+ # @param dir [String] The directory from which to start searching. Defaults to the current
+ # directory.
+ #
+ # @return [String, nil] the app file path, or nil if not found.
+ #
+ # @api public
+ # @since 2.0.0
+ def self.app_path(dir = Dir.pwd)
+ dir = Pathname(dir).expand_path
+ path = dir.join(APP_PATH)
+
+ if path.file?
+ path.to_s
+ elsif !dir.root?
+ app_path(dir.parent)
+ end
+ end
+
+ APP_PATH = "config/app.rb"
+ private_constant :APP_PATH
+
def self.app
@_mutex.synchronize do
unless defined?(@_app)
raise AppLoadError,
"Hanami.app is not yet configured. " \
@@ -20,15 +71,15 @@
@_app
end
end
def self.app?
- defined?(@_app)
+ instance_variable_defined?(:@_app)
end
def self.app=(klass)
@_mutex.synchronize do
- if defined?(@_app)
+ if instance_variable_defined?(:@_app)
raise AppLoadError, "Hanami.app is already configured."
end
@_app = klass unless klass.name.nil?
end