Class: MailManager::Config
- Inherits:
-
Object
- Object
- MailManager::Config
- Defined in:
- lib/mail_manager/config.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(file = nil) ⇒ Config
constructor
A new instance of Config.
- #method_missing(param) ⇒ Object
- #use_file!(file) ⇒ Object
- #use_section!(section) ⇒ Object
Constructor Details
#initialize(file = nil) ⇒ Config
Returns a new instance of Config.
7 8 9 10 11 |
# File 'lib/mail_manager/config.rb', line 7 def initialize(file = nil) @sections = {} @params = {} use_file!(file) if file end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(param) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/mail_manager/config.rb', line 37 def method_missing(param) param = param.to_s if @params.key?(param) @params[param] else Rails.logger.warn "Invalid AppConfig Parameter " + param nil end end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/mail_manager/config.rb', line 5 def params @params end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
5 6 7 |
# File 'lib/mail_manager/config.rb', line 5 def sections @sections end |
Class Method Details
.initialize! ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mail_manager/config.rb', line 47 def self.initialize! standard_file = File.join(Rails.root,'config','mail_manager.yml') local_file = File.join(Rails.root,'config','mail_manager.local.yml') unless File.exists?(standard_file) $stderr.puts "Missing Configuration: either define ::Conf with proper values or create a config/mail_manager.yml with rake mail_manager:default_app_config" end c = ::MailManager::Config.new c.use_file!(standard_file) c.use_file!(local_file) c.use_section!(Rails.env) c end |
Instance Method Details
#use_file!(file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mail_manager/config.rb', line 13 def use_file!(file) begin hash = YAML::load(ERB.new(IO.read(file)).result).with_indifferent_access key = old_val = new_val = nil @sections.merge!(hash) do |key, old_val, new_val| old_val ||= Hash.new.with_indifferent_access new_val ||= Hash.new.with_indifferent_access if old_val.is_a?(Hash) && new_val.is_a?(Hash) old_val.merge(new_val) else new_val end end @params.merge!(@sections['common']) rescue => e Rails.logger.error "Couldn't load config file #{file} #{e.}" nil end end |
#use_section!(section) ⇒ Object
33 34 35 |
# File 'lib/mail_manager/config.rb', line 33 def use_section!(section) @params.merge!(@sections[section.to_s]) if @sections.key?(section.to_s) end |