Class: Application::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/application_configuration.rb

Defined Under Namespace

Classes: Location, Namespace

Constant Summary collapse

DEFAULT_RELOAD_SETTINGS_EVERY =
1200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/application_configuration.rb', line 36

def initialize
  self.whiny_config_missing = false
  @loaded_files = []
  @final_configuration_settings = {:reload_settings_every => DEFAULT_RELOAD_SETTINGS_EVERY}
  @last_reload_time = Time.now # set the first load time
  @is_rails = Object.const_defined?("RAILS_ENV") # set whether it's rails
  if self.is_rails
    @rails_root = Object.const_defined?("RAILS_ROOT") ? RAILS_ROOT : ""
    @rails_env = Object.const_defined?("RAILS_ENV") ? RAILS_ENV : ""
    self.whiny_config_missing = true unless self.rails_env == "production"
    self.loaded_files << Application::Configuration::Location.new("#{self.rails_root}/config/application_configuration.yml")
    self.loaded_files << Application::Configuration::Location.new("#{self.rails_root}/config/application_configuration_#{self.rails_env}.yml")
    self.loaded_files.uniq!
  end
  reload # do the work!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



128
129
130
131
132
133
# File 'lib/application_configuration.rb', line 128

def method_missing(sym, *args)
  puts %{
WARNING: Tried to access configuration parameter: #{sym}, but there is no configuration parameter defined with this name. This is not an error, just an informative message. There is no need to open a bug report for this. Sometimes developers only use a configuration parameter to help debug in testing or development, and there is no need to litter other configuration files with these unnecessary parameters. Thank you.
  } if self.whiny_config_missing
  return nil
end

Instance Attribute Details

#final_configuration_settingsObject (readonly)

stores the final configuration settings



28
29
30
# File 'lib/application_configuration.rb', line 28

def final_configuration_settings
  @final_configuration_settings
end

#is_railsObject (readonly)

stores whether or not we’re in a rails app



30
31
32
# File 'lib/application_configuration.rb', line 30

def is_rails
  @is_rails
end

#last_reload_timeObject (readonly)

stores the last time settings were reloaded



29
30
31
# File 'lib/application_configuration.rb', line 29

def last_reload_time
  @last_reload_time
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



31
32
33
# File 'lib/application_configuration.rb', line 31

def loaded_files
  @loaded_files
end

#rails_envObject (readonly)

Returns the value of attribute rails_env.



33
34
35
# File 'lib/application_configuration.rb', line 33

def rails_env
  @rails_env
end

#rails_rootObject (readonly)

Returns the value of attribute rails_root.



32
33
34
# File 'lib/application_configuration.rb', line 32

def rails_root
  @rails_root
end

#whiny_config_missingObject

Returns the value of attribute whiny_config_missing.



34
35
36
# File 'lib/application_configuration.rb', line 34

def whiny_config_missing
  @whiny_config_missing
end

Class Method Details

.method_missing(sym, *args) ⇒ Object



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

def method_missing(sym, *args)
  inst = Application::Configuration.instance
  # check to see if the configuration needs to be reloaded.
  unless inst.reload_settings_every == -1
    if (Time.now - (inst.reload_settings_every || DEFAULT_RELOAD_SETTINGS_EVERY)) > inst.last_reload_time
      inst.reload
    end
  end
  inst.send(sym, *args)
end

Instance Method Details

#dump_to_file(file_name = (self.is_rails ? "#{self.rails_root}/config/application_configuration_dump.yml" : "application_configuration_dump.yml")) ⇒ Object



122
123
124
125
126
# File 'lib/application_configuration.rb', line 122

def dump_to_file(file_name = (self.is_rails ? "#{self.rails_root}/config/application_configuration_dump.yml" : "application_configuration_dump.yml"))
  File.open(file_name, "w") {|file| file.puts self.final_configuration_settings.to_yaml}
  puts "Dumped configuration settings to: #{file_name}"
  dump_to_screen
end

#dump_to_screenObject



116
117
118
119
120
# File 'lib/application_configuration.rb', line 116

def dump_to_screen
  y = self.final_configuration_settings.to_yaml
  puts y
  y
end

#load_file(path_to_file) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/application_configuration.rb', line 53

def load_file(path_to_file)
  unless path_to_file.nil?
    begin
      path_to_file = Application::Configuration::Location.new(path_to_file) if path_to_file.is_a? String
      settings = load_from_file(path_to_file.name)
      handle_settings(settings, path_to_file)
      return self
    rescue Exception => e
      puts e.message
      return nil
    end
  end
end

#load_hash(settings, name) ⇒ Object



82
83
84
85
86
87
# File 'lib/application_configuration.rb', line 82

def load_hash(settings, name)
  loc = Application::Configuration::Location.new(name, Application::Configuration::Location::HASH)
  loc.options = settings
  handle_settings(settings, loc)
  return self
end

#load_url(path_to_file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/application_configuration.rb', line 67

def load_url(path_to_file)
  require 'open-uri'
  unless path_to_file.nil?
    begin
      path_to_file = Application::Configuration::Location.new(path_to_file, Application::Configuration::Location::URL) if path_to_file.is_a? String
      settings = load_from_url(path_to_file.name)
      handle_settings(settings, path_to_file)
      return self
    rescue Exception => e
      puts e.message
      return nil
    end
  end
end

#reloadObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/application_configuration.rb', line 89

def reload
  puts "Loading Configuration Settings!"
  @final_configuration_settings = {:reload_settings_every => DEFAULT_RELOAD_SETTINGS_EVERY} # reset the configuration

  self.loaded_files.each do |lf|
    case lf.type
    when Application::Configuration::Location::FILE
      puts "Loading Configuration Settings from file: #{lf.name}"
      load_file(lf)
    when Application::Configuration::Location::URL
      puts "Loading Configuration Settings from url: #{lf.name}"
      load_url(lf)
    when Application::Configuration::Location::HASH
      puts "Loading Configuration Settings from hash: #{lf.name}"
      load_hash(lf.options, lf.name)
    else
      raise TypeError.new("The Application::Configuration::Location type '#{lf.type}' is not supported!")
    end
  end
  @last_reload_time = Time.now
end

#revert(step = 1) ⇒ Object



111
112
113
114
# File 'lib/application_configuration.rb', line 111

def revert(step = 1)
  step.times {self.loaded_files.pop}
  reload
end