Class: GitConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/gitconfig.rb,
lib/gitconfig/version.rb

Defined Under Namespace

Classes: EscapedProxy

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GitConfig

Returns a new instance of GitConfig.



9
10
11
12
13
14
15
16
17
# File 'lib/gitconfig.rb', line 9

def initialize(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  @git = Git.open(args[0]||'.', opts)
  @gitlib = Git::Lib.new(@git)

  @vars = opts[:vars] || {}
  @opts = opts
  @escaped = EscapedProxy.new(self)
end

Instance Attribute Details

#escapedObject (readonly)

Returns the value of attribute escaped.



7
8
9
# File 'lib/gitconfig.rb', line 7

def escaped
  @escaped
end

#varsObject (readonly)

Returns the value of attribute vars.



7
8
9
# File 'lib/gitconfig.rb', line 7

def vars
  @vars
end

Instance Method Details

#[](key, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gitconfig.rb', line 34

def [](key, opts={})
  opts = @opts.clone.update(opts)
  key = key.to_s

  if !(include?(key) || opts[:noninteractive])
    meta = vars[key] || {}
    default = meta[:default]
    default = default.call(key) if default.respond_to?(:call)
    meta[:description] ||= key
    self[key] = ask(desc, default)
  else
    self.to_hash[key]
  end
end

#[]=(key, value) ⇒ Object



49
50
51
52
53
# File 'lib/gitconfig.rb', line 49

def []=(key, value)
  save_git_env { gitlib.config_set(key, value) }
  @as_hash = nil
  value
end

#ask(desc, default = nil) ⇒ Object



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

def ask(desc, default=nil)
  print "#{desc}? [#{default}] "
  response = gets.strip
  response = '' ? default : response
end

#defvar(name, meta = nil) ⇒ Object



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

def defvar(name, meta=nil)
  vars[name] = meta
end

#described_vars(*names) ⇒ Object



19
20
21
22
# File 'lib/gitconfig.rb', line 19

def described_vars(*names)
  names = vars.keys if names.empty?
  Hash[ names.map { |k| [vars[k][:description] || k, self[k]] } ]
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gitconfig.rb', line 55

def include?(key)
  self.to_hash.include?(key)
end

#to_hashObject



59
60
61
62
63
# File 'lib/gitconfig.rb', line 59

def to_hash
  @as_hash ||= save_git_env { 
    gitlib.parse_config(File.join(git.repo.path, 'config'))
  }
end