Class: GithubCLI::Config
- Inherits:
-
Object
- Object
- GithubCLI::Config
- Defined in:
- lib/github_cli/config.rb
Constant Summary collapse
- COMMAND_KEY =
'commands'- COMMAND_HELP =
'help'
Instance Attribute Summary collapse
-
#location ⇒ Object
Location scope of currently used configuration file.
-
#root ⇒ Object
readonly
Contains information of where the command is run from.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #all ⇒ Object
- #data ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key, default = nil) ⇒ Object
-
#initialize(root, options = {}) ⇒ Config
constructor
A new instance of Config.
- #keys ⇒ Object
- #load ⇒ Object
- #path ⇒ Object
- #pretty(pattern = "") ⇒ Object
- #save(config) ⇒ Object
Constructor Details
Instance Attribute Details
#location ⇒ Object
Location scope of currently used configuration file.
14 15 16 |
# File 'lib/github_cli/config.rb', line 14 def location @location end |
#root ⇒ Object (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 |
#all ⇒ Object
47 48 49 |
# File 'lib/github_cli/config.rb', line 47 def all data end |
#data ⇒ Object
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 |
#keys ⇒ Object
43 44 45 |
# File 'lib/github_cli/config.rb', line 43 def keys data.keys end |
#load ⇒ Object
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 |
#path ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/github_cli/config.rb', line 85 def path if location == 'local' || File.exists?() else 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 |