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.



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

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



125
126
127
128
129
130
# File 'lib/application_configuration.rb', line 125

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



26
27
28
# File 'lib/application_configuration.rb', line 26

def final_configuration_settings
  @final_configuration_settings
end

#is_railsObject (readonly)

stores whether or not we’re in a rails app



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

def is_rails
  @is_rails
end

#last_reload_timeObject (readonly)

stores the last time settings were reloaded



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

def last_reload_time
  @last_reload_time
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



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

def loaded_files
  @loaded_files
end

#rails_envObject (readonly)

Returns the value of attribute rails_env.



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

def rails_env
  @rails_env
end

#rails_rootObject (readonly)

Returns the value of attribute rails_root.



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

def rails_root
  @rails_root
end

#whiny_config_missingObject

Returns the value of attribute whiny_config_missing.



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

def whiny_config_missing
  @whiny_config_missing
end

Class Method Details

.method_missing(sym, *args) ⇒ Object



15
16
17
18
19
20
21
22
# 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.
  if (Time.now - (inst.reload_settings_every || DEFAULT_RELOAD_SETTINGS_EVERY)) > inst.last_reload_time
    inst.reload
  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



119
120
121
122
123
# File 'lib/application_configuration.rb', line 119

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



113
114
115
116
117
# File 'lib/application_configuration.rb', line 113

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

#load_file(path_to_file) ⇒ Object



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

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



79
80
81
82
83
84
# File 'lib/application_configuration.rb', line 79

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



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

def load_url(path_to_file)
  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



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

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



108
109
110
111
# File 'lib/application_configuration.rb', line 108

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