Class: GitConfig
- Inherits:
-
Object
- Object
- GitConfig
- 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
-
#escaped ⇒ Object
readonly
Returns the value of attribute escaped.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
- #[](key, opts = {}) ⇒ Object
- #[]=(key, value) ⇒ Object
- #ask(desc, default = nil) ⇒ Object
- #defvar(name, meta = nil) ⇒ Object
- #described_vars(*names) ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(*args) ⇒ GitConfig
constructor
A new instance of GitConfig.
- #to_hash ⇒ Object
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
#escaped ⇒ Object (readonly)
Returns the value of attribute escaped.
7 8 9 |
# File 'lib/gitconfig.rb', line 7 def escaped @escaped end |
#vars ⇒ Object (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]) = vars[key] || {} default = [:default] default = default.call(key) if default.respond_to?(:call) [: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, =nil) vars[name] = 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
55 56 57 |
# File 'lib/gitconfig.rb', line 55 def include?(key) self.to_hash.include?(key) end |
#to_hash ⇒ Object
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 |