class AmoebaDeployTools::Config

Public Class Methods

create(filename, opts={}) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 15
def self.create(filename, opts={})
  opts.merge!({
    filename: File.expand_path(filename),
    create: true
  })

  Config.new.tap do |c|
    c.options(opts)
  end
end
load(filename, opts={}) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 8
def self.load(filename, opts={})
  opts[:filename] = File.expand_path filename
  Config.new.tap do |c|
    c.restore(opts)
  end
end

Public Instance Methods

[](k) click to toggle source
Calls superclass method
# File lib/amoeba_deploy_tools/config.rb, line 67
def [](k)
  chain = k.to_s.split('.')
  cur = self

  return super if chain.count <= 1

  for c in chain[0..-2]
    if cur and cur.key? c
      cur = cur.regular_reader(c)
    else
      return
    end
  end

  cur[chain[-1]]
end
[]=(k, v) click to toggle source
Calls superclass method
# File lib/amoeba_deploy_tools/config.rb, line 84
def []=(k, v)
  chain = k.to_s.split('.')
  cur = self

  return super if chain.count <= 1

  for c in chain[0..-2]
    cur = cur.initializing_reader(c)
  end

  cur[chain[-1]] = v
end
flatten() click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 97
def flatten
  flat = {}

  each do |k1, v1|
    if v1.class == self.class
      v1.flatten.each do |k2, v2|
        flat["#{k1}.#{k2}"] = v2
      end
    else
      flat[k1] = v1
    end
  end

  flat
end
new_file?() click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 49
def new_file?
  !!@new_file
end
options(opts=nil) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 26
def options(opts=nil)
  @opts ||= { format: :yaml }
  @opts.merge! opts if opts
  @opts
end
reload!() click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 45
def reload!
  restore(filename: options[:filename])
end
restore(opts=nil) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 32
def restore(opts=nil)
  options(opts)

  return unless filename = options[:filename]

  self.clear.deep_merge! deserialize(File.read(filename))

  self
rescue Errno::ENOENT
  @new_file = true
  FileUtils.touch(filename) and retry if options[:create]
end
save(opts=nil) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 53
def save(opts=nil)
  options(opts)

  return unless filename = options[:filename]

  File.open(filename, 'w') do |fh|
    fh.write(serialize(self.to_hash))
  end

  self
rescue Errno::ENOENT
  FileUtils.touch(filename) and retry if options[:create]
end
to_s() click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 114
def to_s
  to_hash.to_s
end

Protected Instance Methods

deserialize(d) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 129
def deserialize(d)
  @@formats[options[:format]].load(d) || {}
end
serialize(d) click to toggle source
# File lib/amoeba_deploy_tools/config.rb, line 125
def serialize(d)
  @@formats[options[:format]].dump(d)
end