Class: Abt::GitConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/abt/git_config.rb

Defined Under Namespace

Classes: UnsafeNamespaceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = "local", namespace = "") ⇒ GitConfig

Returns a new instance of GitConfig.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/abt/git_config.rb', line 9

def initialize(scope = "local", namespace = "")
  @namespace = namespace

  raise ArgumentError, 'scope must be "local" or "global"' unless %w[local global].include?(scope)

  @scope = scope
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/abt/git_config.rb', line 5

def namespace
  @namespace
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/abt/git_config.rb', line 5

def scope
  @scope
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/abt/git_config.rb', line 31

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



35
36
37
# File 'lib/abt/git_config.rb', line 35

def []=(key, value)
  set(key, value)
end

#available?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/abt/git_config.rb', line 17

def available?
  unless instance_variables.include?(:available)
    @available = begin
      success = false
      Open3.popen3(availability_check_call) do |_i, _o, _e, thread|
        success = thread.value.success?
      end
      success
    end
  end

  @available
end

#clear(output: nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/abt/git_config.rb', line 50

def clear(output: nil)
  raise UnsafeNamespaceError, "Keys can only be cleared within a namespace" if namespace.empty?

  keys.each do |key|
    output&.puts "Clearing #{scope}: #{key_with_namespace(key)}"
    self[key] = nil
  end
end

#full_keysObject



44
45
46
47
48
# File 'lib/abt/git_config.rb', line 44

def full_keys
  ensure_scope_available!

  `git config --#{scope} --get-regexp --name-only ^#{namespace}`.lines.map(&:strip)
end

#keysObject



39
40
41
42
# File 'lib/abt/git_config.rb', line 39

def keys
  offset = namespace.length + 1
  full_keys.map { |key| key[offset..-1] }
end