Module: WDSinatra::AppLoader

Defined in:
lib/wd_sinatra/app_loader.rb

Class Method Summary collapse

Class Method Details

.console(root_path) ⇒ Object

Boot in console mode



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wd_sinatra/app_loader.rb', line 26

def console(root_path)
  @root = root_path
  unless @booted
    set_env
    set_loadpath
    load_environment
    load_lib_dependencies
    load_app_config
    load_models
    load_apis
    @booted =  true
  end
end

.load_apisObject

DSL routes are located in the api folder



101
102
103
104
105
# File 'lib/wd_sinatra/app_loader.rb', line 101

def load_apis
  Dir.glob(File.join(root_path, "api", "**", "*.rb")).each do |api|
    require api
  end
end

.load_app_configObject



90
91
92
# File 'lib/wd_sinatra/app_loader.rb', line 90

def load_app_config
  require File.join(root_path, 'config', 'app')
end

.load_environment(env = RACK_ENV) ⇒ Object

Loads an environment specific config if available, the config file is where the logger should be set if it was not, we are using stdout.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wd_sinatra/app_loader.rb', line 58

def load_environment(env=RACK_ENV)
  # Load the default which can be overwritten or extended by specific
  # env config files.
  require File.join(root_path, 'config', 'environments', 'default.rb')
  env_file = File.join(root_path, "config", "environments", "#{env}.rb")
  if File.exist?(env_file)
    require env_file
  else
    debug_msg = "Environment file: #{env_file} couldn't be found, using only the default environment config instead." unless env == 'development'
  end
  # making sure we have a LOGGER constant defined.
  unless Object.const_defined?(:LOGGER)
    Object.const_set(:LOGGER, Logger.new($stdout))
  end
  LOGGER.debug(debug_msg) if debug_msg
end

.load_lib_dependenciesObject



82
83
84
85
86
87
88
# File 'lib/wd_sinatra/app_loader.rb', line 82

def load_lib_dependencies
  # WeaselDiesel is the web service DSL gem used to define services.
  require 'weasel_diesel'
  require 'sinatra'
  require 'wd_sinatra/sinatra_ext'
  # TODO: hook to custom app dependencies
end

.load_middlewareObject



111
112
113
# File 'lib/wd_sinatra/app_loader.rb', line 111

def load_middleware
  require File.join(root_path, 'config', 'middleware')
end

.load_modelsObject



94
95
96
97
98
# File 'lib/wd_sinatra/app_loader.rb', line 94

def load_models
  Dir.glob(File.join(root_path, "models", "**", "*.rb")).each do |model|
    require model
  end
end

.root_pathObject



40
41
42
# File 'lib/wd_sinatra/app_loader.rb', line 40

def root_path
  @root
end

.server(root_path) ⇒ Object

Boot in server mode



15
16
17
18
19
20
21
22
23
# File 'lib/wd_sinatra/app_loader.rb', line 15

def server(root_path)
  @root = root_path
  unless @booted
    console(root_path)
    load_middleware
    set_sinatra_routes
    set_sinatra_settings
  end
end

.set_envObject

Sets the environment (RACK_ENV) based on some env variables.



47
48
49
50
51
52
53
# File 'lib/wd_sinatra/app_loader.rb', line 47

def set_env
  if !Object.const_defined?(:RACK_ENV)
    ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || 'development'
    Object.const_set(:RACK_ENV, ENV['RACK_ENV'])
  end
  puts "Running in #{RACK_ENV} mode" if RACK_ENV == 'development'
end

.set_loadpath(root = nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/wd_sinatra/app_loader.rb', line 75

def set_loadpath(root=nil)
  root ||= root_path
  $: << root
  $: << File.join(root, 'lib')
  $: << File.join(root, 'models')
end

.set_sinatra_routesObject



107
108
109
# File 'lib/wd_sinatra/app_loader.rb', line 107

def set_sinatra_routes
  WSList.all.sort.each{|api| api.load_sinatra_route }
end

.set_sinatra_settingsObject



115
116
117
# File 'lib/wd_sinatra/app_loader.rb', line 115

def set_sinatra_settings
  require File.join(root_path, 'config', 'sinatra_config')
end