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



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

#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



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.message}"
    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