lib/rbatch/config.rb in rbatch-2.0.0 vs lib/rbatch/config.rb in rbatch-2.1.0
- old
+ new
@@ -2,14 +2,10 @@
require 'pathname'
module RBatch
module_function
-
- # Alias of RBatch::Config.new
- def config ; Config.new end
-
# Config Reader
#
# Read config file and return hash opject. If the key does not exist in config file, raise RBatch::Config::Exception.
#
# Default config file path is "${RB_HOME}/conf/(program base name).yaml"
@@ -25,21 +21,51 @@
# p RBatch::Config.new
# # or p RBatch::config
# => {"key" => "value", "array" => ["item1", "item2", "item3"]}
class Config
@path
- @config
+ @hash
def initialize
file = Pathname(File.basename(RBatch.program_name)).sub_ext(".yaml").to_s
- @path = File.join(RBatch.run_conf[:conf_dir].gsub("<home>",RBatch.home_dir),file)
- @config = YAML::load_file(@path)
+ @path = File.join(RBatch.conf_dir,file)
+ begin
+ @hash = YAML::load_file(@path)
+ rescue Errno::ENOENT => e
+ @hash = nil
+ end
end
def[](key)
- raise RBatch::Config::Exception, "Value of key=\"#{key}\" is nil" if @config[key].nil?
- @config[key]
+ if @hash.nil?
+ raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
+ end
+ if @hash[key].nil?
+ if key.class == Symbol
+ raise RBatch::Config::Exception, "Value of key(:#{key} (Symbol)) is nil. By any chance, dou you mistake key class Symbol for String?"
+ elsif key.class == String
+ raise RBatch::Config::Exception, "Value of key(\"#{key}\" (String)) is nil"
+ else
+ raise RBatch::Config::Exception, "Value of key(#{key}) is nil."
+ end
+ else
+ @hash[key]
+ end
end
def path ; @path ; end
- def to_s ; @config.to_s ;end
+ def exist? ; ! @hash.nil? ; end
+ def to_h
+ if @hash.nil?
+ raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
+ else
+ @hash
+ end
+ end
+ def to_s
+ if @hash.nil?
+ raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
+ else
+ @hash.to_s
+ end
+ end
end
class RBatch::Config::Exception < Exception; end
end