module GitConfigIO

Constants

VERSION

Public Class Methods

concat(hash,source = '') click to toggle source
# File lib/util.rb, line 3
def self.concat(hash,source = '')
  source = parse(source) if source.class == String
  hash.merge source
end
delete(source,subject) click to toggle source
# File lib/util.rb, line 17
def self.delete(source,subject)
  source = source.class == String ? parse(source) : source.dup
  source.delete(subject)
  generate(source)
end
delete!(path,subject) click to toggle source
# File lib/util.rb, line 23
def self.delete!(path,subject)
  config = load(path)
  dump(path,delete(config,subject))
end
dump(path,source = '') click to toggle source
# File lib/gitconfigio.rb, line 49
def self.dump(path,source = '')
  source = generate(source) if source.class == Hash
  File.write(File.expand_path(path),source)
end
generate(hash) click to toggle source
# File lib/gitconfigio.rb, line 38
def self.generate(hash)
  str = ''
  hash.keys.each do |key|
    str << "[#{key}]\n"
    hash[key].each do |k,v|
      str << "\t#{k} = #{v}\n"
    end
  end
  str
end
load(path) click to toggle source
# File lib/gitconfigio.rb, line 34
def self.load(path)
  parse(File.read(File.expand_path(path)))
end
merge(path,source = '') click to toggle source
# File lib/util.rb, line 8
def self.merge(path,source = '')
  config = load(path)
  concat(config, source)
end
merge!(path,source = '') click to toggle source
# File lib/util.rb, line 13
def self.merge!(path,source = '')
  dump(path,merge(path,source))
end
parse(source) click to toggle source
# File lib/gitconfigio.rb, line 29
def self.parse(source)
  ls = source.split("\n")
  config = parse_node(ls)
end
parse_node(line_source) click to toggle source
# File lib/gitconfigio.rb, line 12
def self.parse_node(line_source)
  config = {}
  head = nil
  line_source.each do |l|
    if /^\[(.+)\]$/ === l
      head = {}
      config[l.match(/^\[(.+)\]$/)[1]] = head
    elsif /^#/ === l
      next
    elsif !!head
      key,value = parse_value(l)
      head[key] = value if key
    end
  end
  config
end
parse_value(source) click to toggle source
# File lib/gitconfigio.rb, line 6
def self.parse_value(source)
  key,*value = source.split('=')
  value = value.join('=')
  return key.gsub(/\s/,''),value.sub(/^\s/,'') if key && value
end
write(config,source) click to toggle source
# File lib/util.rb, line 28
def self.write(config,source)
  config = parse(config) if config.class == String
  source = parse(source) if source.class == String
  source.each do |k,v|
    config[k] = v
  end
  config
end
write!(path,source) click to toggle source
# File lib/util.rb, line 37
def self.write!(path,source)
  config = load(config)
  config = write(config,source)
  dump(path,config)
end