Sha256: 44d1881a111738691f8620c4e63d842e159134ec30a8135695258ec683a708d5

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'ostruct'
require 'yaml'

class ConfigStruct < OpenStruct

  def initialize(options = nil, input = STDIN, output = STDOUT)
    super(options)
    @input = input
    @output = output
    set_defaults
    prepare_dirs
    addvalues
  end

  def set_defaults
    default :basedir, '/tmp'
    default :basefile, File.join(self.basedir, 'config.yml')
  end

  def prepare_dirs
    FileUtils.mkdir_p self.basedir unless Dir.exist? self.basedir
  end

  def addvalues
    setup unless File.exist? self.basefile
    YAML.load_file(self.basefile).each do |k, v|
      new_ostruct_member(k)
      send("#{k}=", v)
    end
  end

  def setup
    write Hash.new
  end

  def default(var, value)
    send(var).nil? && send("#{var}=", value)
  end

  def write(values)
    File.open(self.basefile, 'w') do |f|
      f.write YAML.dump(values)
    end
  end

  def puts(*string)
    @output.puts *string
  end

  def print(*string)
    @output.print *string
  end

  def printf(string, *args)
    @output.printf string, *args
  end

  def gets(*args)
    @input.gets *args
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
configstruct-0.0.3 lib/configstruct.rb