Class: Doozer::Configs

Inherits:
Object show all
Defined in:
lib/doozer/configs.rb

Overview

This is the main Configs class which loads root/config/app.yml and root/config/database.yml

It also provides a few helper methods like logger, app_path, base_url and app_name

Constant Summary collapse

@@possible_orm =
[:active_record, :data_mapper, :sequel]
@@app_path =
nil
@@static_file =
{}
@@orm_loaded =
false

Class Method Summary collapse

Class Method Details

.appObject

Return the app configuration setting for the loaded environment



121
122
123
# File 'lib/doozer/configs.rb', line 121

def self.app
  return @@config[:app][@@config[:rack_env]]
end

.app_nameObject

Return the app name



131
132
133
# File 'lib/doozer/configs.rb', line 131

def self.app_name
  self.app["name"] || ""
end

.app_pathObject

This is the file path the app was loaded under. Dir.pwd moves to root in daemon mode so we cache this.



67
68
69
70
# File 'lib/doozer/configs.rb', line 67

def self.app_path
  set_app_path if @@app_path.nil?
  return @@app_path
end

.base_urlObject

Return the app base url



126
127
128
# File 'lib/doozer/configs.rb', line 126

def self.base_url
  self.app["base_url"] || ""
end

.clear_static_filesObject



168
169
170
# File 'lib/doozer/configs.rb', line 168

def self.clear_static_files
  @@static_file = {}
end

.dbObject

Return the database configuration setting for the loaded environment



98
99
100
# File 'lib/doozer/configs.rb', line 98

def self.db
  return @@config[:database][@@config[:rack_env]] if not @@config[:database].nil?
end

.db_connObject

Only used for Sequel ORM for getting the db connection after connecting



111
112
113
# File 'lib/doozer/configs.rb', line 111

def self.db_conn
   @@db_conn
end

.db_conn=(conn) ⇒ Object

Only used for Sequel ORM to set the db connection



116
117
118
# File 'lib/doozer/configs.rb', line 116

def self.db_conn=(conn)
   @@db_conn = conn
end

.get(sym = nil) ⇒ Object

Input a symbol and return the config for this sym



84
85
86
# File 'lib/doozer/configs.rb', line 84

def self.get(sym=nil)
  @@config[sym]
end

.internal_server_error_urlObject

Return the app 404 url



146
147
148
# File 'lib/doozer/configs.rb', line 146

def self.internal_server_error_url
  self.app[500] || nil
end

.load(rack_env) ⇒ Object

Load all the config files for the application. Also instantiates a default application Logger.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/doozer/configs.rb', line 17

def self.load(rack_env)
   puts "=> App path #{app_path}" 
   puts "=> Loading configs for #{rack_env}"
   
   @@config = Config::CONFIG
   rack_env = (rack_env.kind_of? String) ? rack_env.to_sym : rack_env
   case rack_env
   when :development
   when :deployment
   when :test, :none
     rack_env = :test
   else
     raise ":development, :deployment, or :test are only environments allowed"
   end

   # set logging for environment
   if [:development, :test].include?(rack_env)
     @@logger = Logger.new(STDOUT)
   else
     @@logger = Logger.new("#{app_path}/log/#{rack_env}.log")
   end

   @@config[:rack_env] = rack_env
   begin
    @@config[:database] = Configs.symbolize_keys( YAML.load(File.read(File.join(app_path,'config/database.yml'))) )
   rescue
    puts "ERROR => Failed to load config/database.yml"
   end

   begin
    @@config[:app] = Configs.symbolize_keys( YAML.load(File.read(File.join(app_path,'config/app.yml'))) )
   rescue
    puts "ERROR => Failed to load config/app.yml"
   end

end

.loggerObject

We initialize the application logger in this Configs. This is then extended through to the ActiveRecord and is also available in ViewHelpers.



55
56
57
# File 'lib/doozer/configs.rb', line 55

def self.logger
  @@logger
end

.ormObject

Return the orm mapping gem name to load



89
90
91
92
93
94
95
# File 'lib/doozer/configs.rb', line 89

def self.orm
  begin 
    return @@config[:database][:orm] 
  rescue 
  end
  return nil
end

.orm_loadedObject



102
103
104
# File 'lib/doozer/configs.rb', line 102

def self.orm_loaded
  @@orm_loaded
end

.orm_loaded=(t) ⇒ Object



106
107
108
# File 'lib/doozer/configs.rb', line 106

def self.orm_loaded=(t)
  @@orm_loaded = t
end

.page_not_found_urlObject

Return the app 404 url



141
142
143
# File 'lib/doozer/configs.rb', line 141

def self.page_not_found_url
  self.app[404] || nil
end

.rack_envObject

Return the rack environment this application was loaded with.



79
80
81
# File 'lib/doozer/configs.rb', line 79

def self.rack_env
 return @@config[:rack_env] if not @@config[:rack_env].nil?
end

.set_app_path(path = nil) ⇒ Object

Hook for setting the application path.

This allows the an application to be initialized from a different location then the project directory.



62
63
64
# File 'lib/doozer/configs.rb', line 62

def self.set_app_path(path=nil)
  @@app_path = path || Dir.pwd
end

.static_rootObject

Return the static root



136
137
138
# File 'lib/doozer/configs.rb', line 136

def self.static_root
  self.app["static_root"] || ""
end

.static_url(path) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/doozer/configs.rb', line 150

def self.static_url(path)
  return path if path.index('http://') or path.index('https://')
  key = "#{@@app_path}/#{static_root}#{path}"
  if not @@static_file[key].nil?
    return "#{path}?#{@@static_file[key]}"
  else
    begin
      time = File.stat(key).mtime
      hash = Digest::SHA1.hexdigest(time.to_s)[0...5]
      @@static_file[key] = hash
      return "#{path}?#{hash}"
    rescue => e
      logger.error(e.to_s)
    end
  end
  return path
end

.symbolize_keys(hash = nil) ⇒ Object

Take a hash and turn all the keys into symbols



73
74
75
76
# File 'lib/doozer/configs.rb', line 73

def self.symbolize_keys(hash=nil)
  out = {}; hash.each { | k, val | out[k.to_sym] = val}
  return out
end