Class: MailManager::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_manager/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



27
28
29
30
31
32
33
34
35
# File 'lib/mail_manager/config.rb', line 27

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

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/mail_manager/config.rb', line 5

def params
  @params
end

#sectionsObject (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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mail_manager/config.rb', line 37

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!("#{Rails.root}/config/mail_manager.yml")
  c.use_file!("#{Rails.root}/config/mail_manager.local.yml")
  c.use_section!(Rails.env)
  c
end

Instance Method Details

#use_file!(file) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/mail_manager/config.rb', line 13

def use_file!(file)
  begin
    hash = YAML::load(ERB.new(IO.read(file)).result)       
    @sections.merge!(hash) {|key, old_val, new_val| (old_val || new_val).merge new_val }
    @params.merge!(@sections['common'])
  rescue => e
    nil
  end    
end

#use_section!(section) ⇒ Object



23
24
25
# File 'lib/mail_manager/config.rb', line 23

def use_section!(section)
  @params.merge!(@sections[section.to_s]) if @sections.key?(section.to_s)
end