Module: GitConfigIO

Defined in:
lib/gitconfigio.rb,
lib/gitconfigio/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.concat(hash, source = '') ⇒ Object



52
53
54
55
# File 'lib/gitconfigio.rb', line 52

def self.concat(hash,source = '')
  source = parse(source) if source.class == String
  hash.merge source
end

.delete(source, subject) ⇒ Object



66
67
68
69
70
# File 'lib/gitconfigio.rb', line 66

def self.delete(source,subject)
  source = source.class == String ? parse(source) : source.dup
  source.delete(subject)
  generate(source)
end

.delete!(path, subject) ⇒ Object



72
73
74
75
# File 'lib/gitconfigio.rb', line 72

def self.delete!(path,subject)
  config = load(path)
  dump(path,delete(config,subject))
end

.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

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



57
58
59
60
# File 'lib/gitconfigio.rb', line 57

def self.merge(path,source = '')
  config = load(path)
  concat(config, source)
end

.merge!(path, source = '') ⇒ Object



62
63
64
# File 'lib/gitconfigio.rb', line 62

def self.merge!(path,source = '')
  dump(path,merge(path,source))
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