Sha256: 11f5219ca9581845f82a2b168397e656b060067f5b949d973764d805a99f898a

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

Contents

class LocalConf < Hash
  include Hash::Slop
  
  def initialize(file)
    r1 = load file.sub(/\.yml$/, ".defaults.yml")
    r2 = load file

    return if r1 || r2
    
    raise Errno::ENOENT, 
      "Missing configuration file #{fullpath file.sub(/\.yml$/, "")}{.defaults}.yml}"
  end

  private
  
  def fullpath(file)
    return file if file.starts_with?("/")
    "#{App.root}/config/#{file}"
  end
  
  def load(file)
    data = YAML::load_file(fullpath(file))

    data.each { |k,v| update k.to_sym => v }

    if h = data["defaults"]
      h.each { |k,v| update k.to_sym => v }
    end
    if h = data[App.env]
      h.each { |k,v| update k.to_sym => v }
    end

    true
  rescue Errno::ENOENT
    false
  end
  
  def method_missing(sym, *args, &block)
    return super unless args.empty? && !block_given? && sym.to_s =~ /(.*)\?/
    !fetch($1.to_sym).blank?
  rescue IndexError
    false
  end

  def self.method_missing(sym, *args, &block)
    App.local_conf.send sym, *args, &block
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vex-0.4.4 lib/vex/base/local_conf.rb