Skip to content

Instantly share code, notes, and snippets.

@DAddYE
Created November 13, 2009 14:22
Show Gist options
  • Save DAddYE/233856 to your computer and use it in GitHub Desktop.
Save DAddYE/233856 to your computer and use it in GitHub Desktop.
class AppExample < AppBase
set :app_name, "app_example"
bootstrap!
end
# This file is merely for beginning the boot process, check dependencies.rb for more information
require 'rubygems'
require 'sinatra/base'
require File.dirname(__FILE__) + '/boot'
class AppBase < Sinatra::Application
# Defines basic application settings
set :app_file, Proc.new { root_path("#{app_name}/#{app_name}.rb") }
set :images_path, public_path("images")
set :default_builder, 'StandardFormBuilder'
set :environment, RACK_ENV if defined?(RACK_ENV)
def self.bootstrap!
# Required middleware
use Rack::Session::Cookie
use Rack::Flash
# Requires the initializer modules which configure specific components
Dir[root_path + '/config/initializers/*.rb'].each do |file|
# Each initializer file contains a module called 'XxxxInitializer' (i.e HassleInitializer)
require file
file_class = File.basename(file, '.rb').classify
register "#{file_class}Initializer".constantize
end
# Includes all necessary sinatra_more helpers
register SinatraMore::MarkupPlugin
register SinatraMore::RenderPlugin
register SinatraMore::MailerPlugin
register SinatraMore::WardenPlugin
# Require all helpers
Dir[root + "/helpers/*.rb"].each do |file|
# Each file contains a module called 'XxxxHelper' (i.e ViewHelper)
require file
file_class = File.basename(file, '.rb').classify
helpers "#{file_class}Helper".constantize
end
# Require all routes
Dir[root + "/routes/*.rb"].each { |file| eval(File.read(file)) }
end
end
# This file is merely for beginning the boot process
RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined? RACK_ENV
ROOT_DIR = File.dirname(__FILE__) + '/..' unless defined? ROOT_DIR
# Helper method for file references.
#
# @param args [Array] Path components relative to ROOT_DIR.
# @example Referencing a file in config called settings.yml:
# root_path("config", "settings.yml")
def root_path(*args)
File.join(ROOT_DIR, *args)
end
# Returns the full path to the public folder along with any given additions
# public_path("images")
def public_path(*args)
root_path('public', *args)
end
# Attempts to load/require all dependency libs that we need.
#
# @param paths [Array] Path where is necessary require or load a dependency
# @example For load all our app libs we need to do:
# load_dependencies("#{root_path}/lib/**/*.rb")
def load_dependencies(*paths)
paths.each do |path|
Dir[path].each { |file| RACK_ENV == "production" ? require(file) : load(file) }
end
end
# Attempts to require all dependencies with bundler
begin
require 'bundler'
Bundler::Environment.load(root_path + "/Gemfile").require_env(RACK_ENV)
rescue Bundler::DefaultManifestNotFound => e
puts "You didn't create Bundler Gemfile manifest or you are not in a Sinatra application."
end
# Attempts to load budled gems if it fail we try to use system wide gems
begin
require root_path('/../vendor', 'gems', RACK_ENV)
puts "We use bundled gems"
rescue LoadError => e
puts "We use system wide gems"
end
# Attempts to load all necessary dependencies
load_dependencies("#{root_path}/lib/**/*.rb", "#{root_path}/models/*.rb", "#{root_path}/app_*/models/*.rb")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment