Class: Hub::Context::GitReader

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/context.rb

Overview

Shells out to git to get output of its commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable = nil, &read_proc) ⇒ GitReader

Returns a new instance of GitReader.



17
18
19
20
21
22
23
24
25
# File 'lib/hub/context.rb', line 17

def initialize(executable = nil, &read_proc)
  @executable = executable || 'git'
  # caches output when shelling out to git
  read_proc ||= lambda { |cache, cmd|
    result = %x{#{command_to_string(cmd)} 2>#{NULL}}.chomp
    cache[cmd] = $?.success? && !result.empty? ? result : nil
  }
  @cache = Hash.new(&read_proc)
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



15
16
17
# File 'lib/hub/context.rb', line 15

def executable
  @executable
end

Instance Method Details

#add_exec_flags(flags) ⇒ Object



27
28
29
# File 'lib/hub/context.rb', line 27

def add_exec_flags(flags)
  @executable = Array(executable).concat(flags)
end

#read(cmd) ⇒ Object



37
38
39
# File 'lib/hub/context.rb', line 37

def read(cmd)
  @cache[cmd]
end

#read_config(cmd, all = false) ⇒ Object



31
32
33
34
35
# File 'lib/hub/context.rb', line 31

def read_config(cmd, all = false)
  config_cmd = ['config', (all ? '--get-all' : '--get'), *cmd]
  config_cmd = config_cmd.join(' ') unless cmd.respond_to? :join
  read config_cmd
end

#stub!(values) ⇒ Object



49
50
51
# File 'lib/hub/context.rb', line 49

def stub!(values)
  @cache.update values
end

#stub_command_output(cmd, value) ⇒ Object



45
46
47
# File 'lib/hub/context.rb', line 45

def stub_command_output(cmd, value)
  @cache[cmd] = value.nil? ? nil : value.to_s
end

#stub_config_value(key, value, get = '--get') ⇒ Object



41
42
43
# File 'lib/hub/context.rb', line 41

def stub_config_value(key, value, get = '--get')
  stub_command_output "config #{get} #{key}", value
end