Class: Common::Options
- Inherits:
-
Object
- Object
- Common::Options
- Defined in:
- lib/common/options.rb
Instance Method Summary collapse
-
#initialize(cfg_file = 'config/options.yml', local_cfg_file = 'config/options-local.yml', load_env = nil) ⇒ Options
constructor
A new instance of Options.
-
#read_options(args = {}, update_options = {}) ⇒ Object
Read the options from the config file.
- #reload! ⇒ Object
Constructor Details
#initialize(cfg_file = 'config/options.yml', local_cfg_file = 'config/options-local.yml', load_env = nil) ⇒ Options
Returns a new instance of Options.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/common/options.rb', line 33 def initialize(cfg_file = 'config/options.yml', local_cfg_file = 'config/options-local.yml', load_env = nil) Common::Utils::suppress_warnings do @load_env = load_env @cfg_file = cfg_file @local_cfg_file = local_cfg_file @tmp_cmdl_file = 'tmp/options-cmd-line.yml' @persistent_local_cfg_file = '/etc/options.yml' if defined? Rails @cfg_file = Rails.root.to_s + '/' + @cfg_file @local_cfg_file = Rails.root.to_s + '/' + @local_cfg_file @persistent_local_cfg_file = "/etc/config/#{Rails.root.to_s.split("/")[-1]}/options.yml" end end @options = {} reload! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arg) ⇒ Object (private)
Allows retrieval of option value (i.e. options.option_name) that matches the key name in the config file.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/common/options.rb', line 152 def method_missing method_name, *arg if method_name.to_s =~ /(.*)=$/ key = $1 if @options.has_key? key @options[key] = arg[0] return @options[key] end else key = method_name.to_s if @options.has_key? key return @options[key] end end raise NoMethodError.new("undefined method `#{key}' for Options:Class", "unknown_key") end |
Instance Method Details
#read_options(args = {}, update_options = {}) ⇒ Object
Read the options from the config file. cfg_file is the default config file. If local_cfg_file exists, the options specified there overrides those in cfg_file. If :config_file is specified, it is used in place of local_cfg_file. If :environment is specified, the options in the corresponding environment is loaded and merged with those in ‘default’.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/common/options.rb', line 92 def args={}, ={} args = {:environment => nil, :config_file => nil, :verbose => 'silent' }.update(args) = {} if File.exists? @cfg_file vputs "Loading '#{@cfg_file}'", args[:verbose] = load_file @cfg_file end if args[:config_file] if !File.exists? args[:config_file] vputs "ERROR: Config file '#{args[:config_file]}' not found!", args[:verbose] exit end vputs "Loading '#{args[:config_file]}'", args[:verbose] (args[:config_file], ) elsif @persistent_local_cfg_file && File.exists?(@persistent_local_cfg_file) vputs "Loading '#{@persistent_local_cfg_file}'", args[:verbose] (@persistent_local_cfg_file, ) elsif @local_cfg_file && File.exists?(@local_cfg_file) vputs "Loading '#{@local_cfg_file}'", args[:verbose] (@local_cfg_file, ) end if args[:environment] vputs "Using environment '#{args[:environment]}'", args[:verbose] = (['default']||{}).deep_merge!([args[:environment]]||{}) end .update() ['environment'] = 'development' if ['environment'].nil? ['verbose'] = false if ['verbose'].nil? if args[:verbose] == true len = .keys.map { |k| k.size }.sort.last vputs "Loaded options:", args[:verbose] .each { |k, v| puts " #{k.ljust(len)} => #{v}" } end end |
#reload! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/common/options.rb', line 50 def reload! cmd_line_args = {} if File.exists? @tmp_cmdl_file cmd_line_args = load_file(@tmp_cmdl_file) # Don't remove tmp_cmdl_file if the keep_tmp_cmdl_file flag is set. if defined? KEEP_TMP_CMDL_FILE and KEEP_TMP_CMDL_FILE Object.instance_eval { remove_const :KEEP_TMP_CMDL_FILE } else FileUtils.rm @tmp_cmdl_file end end if defined? Rails cmd_line_args['environment'] = Rails.env.to_s else cmd_line_args['environment'] = @load_env.to_s end if cmd_line_args['environment'] == 'test' and cmd_line_args['verbose'].nil? cmd_line_args['verbose'] = 'silent' end if cmd_line_args['environment'].nil? defaults = ({:environment => 'default', :config_file => cmd_line_args['config-file'], :verbose => 'silent'}) cmd_line_args['environment'] = defaults['environment'] || 'development' end @options = ({:environment => cmd_line_args['environment'], :config_file => cmd_line_args['config-file'], :verbose => cmd_line_args['verbose']}, cmd_line_args) end |