configpool.rb

class ConfigPool
  class ConfigError < RuntimeError ; end
  
  def initialize
    @readonly_list = []
  end
  
  def self.load(path)
    conf = self.new
    File.open(path) do |f|
      conf.instance_eval(f.read)
    end
    conf
  end
  
  def [](name)
    instance_variable_get("@#{name}") or
      raise ConfigError, "unset value `#{name}'"
  end
  
  def []=(name, val)
    @readonly_list.include?(name.to_sym) and
      raise ConfigError, "read only value `#{name}'"
    instance_variable_set("@#{name}", val)
  end

private
  #以下、設定記述DSL用
  def readonly(name)
    @readonly_list.push name.to_sym
  end
  alias ro readonly
end


if __FILE__ == $0
  conf = ConfigPool.load("config.txt")
  p conf[:bin_dir]
  conf[:bin_dir] = "aaa"
  p conf[:bin_dir]
  conf[:home] = "bbb"
end

__END__
#(config.txt)
@home     = "D:/local_hp/"
@rd_dir   = "#{@home}rd/"
@html_dir = "#{@home}html/"
@bin_dir  = "#{@home}bin/"
@css_dir  = "#{@html_dir}css/"
@include  = "#{@home}include"

@html_ext = "html"
@encording = "sjis"      #sjis|euc|jis
@base_css = "rdstyle.css"  #@css_dir内にあるとされる

@log_level = "INFO"    #FATAL|ERROR|WARN|INFO|DEBUG