Module: Gitgo::Git::Utils

Included in:
Gitgo::Git
Defined in:
lib/gitgo/git/utils.rb

Overview

A set of utility functions split out for ease of testing.

Class Method Summary collapse

Class Method Details

.nil_or_empty?(obj) ⇒ Boolean

Returns true if the object is nil, or responds to empty and is empty.

Returns:

  • (Boolean)


54
55
56
# File 'lib/gitgo/git/utils.rb', line 54

def nil_or_empty?(obj)
  obj.nil? || (obj.respond_to?(:empty?) && obj.empty?)
end

.nil_or_empty_string?(obj) ⇒ Boolean

Returns true if the object is nil, or empty, and assumes the object is a string (ie that it responds to empty?).

Returns:

  • (Boolean)


49
50
51
# File 'lib/gitgo/git/utils.rb', line 49

def nil_or_empty_string?(obj)
  obj.nil? || obj.empty?
end

.split(path) ⇒ Object

Splits a path along slashes into an array, stripping empty strings from each end. An array may be provided in place of path; it will be duplicated before being stripped of nil/empty entries.



40
41
42
43
44
45
# File 'lib/gitgo/git/utils.rb', line 40

def split(path)
  array = path.kind_of?(String) ? path.split("/") : path.dup
  array.shift if nil_or_empty_string?(array[0])
  array.pop   if nil_or_empty_string?(array[-1])
  array
end

.with_env(env = {}) ⇒ Object

Executes the block having set the env variables. All ENV variables that start with GIT_ will be removed regardless of whether they are specified in env or not.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitgo/git/utils.rb', line 11

def with_env(env={})
  overrides = {}
  begin
    ENV.keys.each do |key|
      if key =~ /^GIT_/
        overrides[key] = ENV.delete(key)
      end
    end

    env.each_pair do |key, value|
      overrides[key] ||= nil
      ENV[key] = value
    end

    yield
  ensure
    overrides.each_pair do |key, value|
      if value
        ENV[key] = value
      else
        ENV.delete(key)
      end
    end
  end
end