Module: GitConfigIO

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

Constant Summary collapse

VERSION =
"1.1.2"

Class Method Summary collapse

Class Method Details

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



3
4
5
6
# 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) ⇒ Object



17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
# File 'lib/util.rb', line 23

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

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



49
50
51
52
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



34
35
36
# File 'lib/gitconfigio.rb', line 34

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

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



8
9
10
11
# File 'lib/util.rb', line 8

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

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



13
14
15
# File 'lib/util.rb', line 13

def self.merge!(path,source = '')
  dump(path,merge(path,source))
end

.parse(source) ⇒ Object



29
30
31
32
# File 'lib/gitconfigio.rb', line 29

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

.parse_node(line_source) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



6
7
8
9
10
# 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) ⇒ Object



28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
# File 'lib/util.rb', line 37

def self.write!(path,source)
  config = load(config)
  config = write(config,source)
  dump(path,config)
end