How to add configuration to Ruby gem

1. Add config into gem definition file directly

1
2
3
4
5
6
7
8
module XXXX
......
class << self
ENV['environment'] = 'production'
end
......
end

2. Use yaml

Create your own yaml files, e.g. config1.yml and config2.yml

Link them by adding the following code into gem definition file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module XXXX
......
def self.configure_with(config_file_path)
logger = Logger.new('XXXX.log')
begin
config = YAML::load(IO.read(config_file_path))
rescue Errno::ENOENT
logger.warn("YAML configuration file couldn't be found. Using defaults."); return
rescue Exception
logger.warn("Cannot load YAML configuration. Using defaults."); return
end
configure(config)
end
configure_with('config/config1.yml')
configure_with('config/config2.yml')
......
end
Share