Module: GitConfigIO

Defined in:
lib/gitconfigio.rb

Class Method Summary collapse

Class Method Details

.dump(path, source = '') ⇒ Object



47
48
49
50
# File 'lib/gitconfigio.rb', line 47

def self.dump(path,source = '')
  source = generate(source) if source.class == Hash
  File.write(File.expand_path(path),source)
end

.generate(hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/gitconfigio.rb', line 36

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) ⇒ Object



32
33
34
# File 'lib/gitconfigio.rb', line 32

def self.load(path)
  parse(File.read(File.expand_path(path)))
end

.parse(source) ⇒ Object



27
28
29
30
# File 'lib/gitconfigio.rb', line 27

def self.parse(source)
  ls = source.split("\n")
  config = parse_node(ls)
end

.parse_node(line_source) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitconfigio.rb', line 10

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) ⇒ Object



5
6
7
8
# File 'lib/gitconfigio.rb', line 5

def self.parse_value(source)
  key,value = source.split('=')
  return key.gsub(/\s/,''),value.sub(/^\s/,'') if key && value
end