Sha256: ffa701f1c56d259882f8e5d89f2b0f857b8252722e54e3051b91af0ad9c18772

Contents?: true

Size: 1.07 KB

Versions: 16

Compression:

Stored size: 1.07 KB

Contents

require 'yaml'
require 'pathname'

module RBatch

  module_function

  # Alias of RBatch::Config.new
  def config ; Config.new end

  # Read config file and return hash opject.
  # 
  # Default config file path is "../conf/(Program Base name).yaml"
  # ==== Sample
  # config : ./conf/sample2.yaml
  #  key: value
  #  array:
  #   - item1
  #   - item2
  #   - item3
  # script : ./bin/sample2.rb
  #  require 'rbatch'
  #  p RBatch::Config.new
  #  # or  p RBatch::config
  #  => {"key" => "value", "array" => ["item1", "item2", "item3"]}
  class Config
    @path
    @config
    def initialize
      file = Pathname(File.basename(RBatch.program_name)).sub_ext(".yaml").to_s
      dir = File.join(File.join(File.dirname(RBatch.program_name),".."),"conf")
      @path = File.join(dir,file)
      @config = YAML::load_file(@path)
    end
    def[](key)
      raise RBatch::Config::Exception, "Value of key=\"#{key}\" is nil" if @config[key].nil?
      @config[key]
    end
    def path ; @path ; end
    def to_s ; @config.to_s ;end
  end

  class RBatch::Config::Exception < Exception; end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rbatch-1.12.2 lib/rbatch/config.rb
rbatch-1.12.1 lib/rbatch/config.rb
rbatch-1.12.0 lib/rbatch/config.rb
rbatch-1.11.0 lib/rbatch/config.rb
rbatch-1.10.0 lib/rbatch/config.rb
rbatch-1.9.0 lib/rbatch/config.rb
rbatch-1.8.2 lib/rbatch/config.rb
rbatch-1.8.1 lib/rbatch/config.rb
rbatch-1.8.0 lib/rbatch/config.rb
rbatch-1.7.0 lib/rbatch/config.rb
rbatch-1.6.6 lib/rbatch/config.rb
rbatch-1.6.4 lib/rbatch/config.rb
rbatch-1.6.3 lib/rbatch/config.rb
rbatch-1.6.2 lib/rbatch/config.rb
rbatch-1.6.1 lib/rbatch/config.rb
rbatch-1.6.0 lib/rbatch/config.rb