Class: GithubCLI::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/config.rb

Constant Summary collapse

COMMAND_KEY =
'commands'
COMMAND_HELP =
'help'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Config

Returns a new instance of Config.



16
17
18
19
20
# File 'lib/github_cli/config.rb', line 16

def initialize(root, options={})
  @root = root
  @local_config  = local_options_file
  @global_config = global_options_file
end

Instance Attribute Details

#locationObject

Location scope of currently used configuration file.



14
15
16
# File 'lib/github_cli/config.rb', line 14

def location
  @location
end

#rootObject (readonly)

Contains information of where the command is run from.



11
12
13
# File 'lib/github_cli/config.rb', line 11

def root
  @root
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/github_cli/config.rb', line 27

def [](key)
  data[key] || data["#{COMMAND_KEY}.#{key}"] rescue nil
end

#[]=(key, value) ⇒ Object



22
23
24
25
# File 'lib/github_cli/config.rb', line 22

def []=(key, value)
  set_key(key, value)
  @data = nil
end

#allObject



47
48
49
# File 'lib/github_cli/config.rb', line 47

def all
  data
end

#dataObject



39
40
41
# File 'lib/github_cli/config.rb', line 39

def data
  @data ||= self.load
end

#delete(key) ⇒ Object



35
36
37
# File 'lib/github_cli/config.rb', line 35

def delete(key)
  data.delete(key)
end

#fetch(key, default = nil) ⇒ Object



31
32
33
# File 'lib/github_cli/config.rb', line 31

def fetch(key, default=nil)
  self[key] || default || raise(IndexError.new("key #{key} not found"))
end

#keysObject



43
44
45
# File 'lib/github_cli/config.rb', line 43

def keys
  data.keys
end

#loadObject



75
76
77
78
79
80
81
82
83
# File 'lib/github_cli/config.rb', line 75

def load
  yaml = {}
  if File.exists? path
    yaml = File.open(path, 'r') do |file|
      YAML.load(file)
    end
  end
  yaml
end

#pathObject



85
86
87
88
89
90
91
# File 'lib/github_cli/config.rb', line 85

def path
  if location == 'local' || File.exists?(local_options_file)
    local_options_file
  else
    global_options_file
  end
end

#pretty(pattern = "") ⇒ Object



51
52
53
54
55
# File 'lib/github_cli/config.rb', line 51

def pretty(pattern="")
  all.keys.zip(all.values).map do |el|
    el.last.nil? ? [el.first, 'UNDEFINED'] : el
  end.select { |el| el.first =~ /^#{pattern}.*$/i }
end

#save(config) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/github_cli/config.rb', line 62

def save(config)
  Command.all.each do |cmd|
    composite_key = "#{COMMAND_KEY}.#{cmd.namespace}.#{cmd.name}"
    if !cmd.namespace.empty? && cmd.name != COMMAND_HELP &&
       !config.has_key?(composite_key)
      config[composite_key]= {}
    end
  end
  File.open(path, 'w', 0600) do |file|
    YAML.dump(config, file)
  end
end