Class: GithubCLI::Config
- Inherits:
-
Object
- Object
- GithubCLI::Config
- Defined in:
- lib/github_cli/config.rb
Constant Summary collapse
- COMMAND_KEY =
'commands'.freeze
- COMMAND_HELP =
'help'.freeze
- DEFAULT_NAME =
'.githubrc'.freeze
Instance Attribute Summary collapse
-
#filename ⇒ Object
Name for the confiugration file.
-
#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
Initialize a Config.
- #keys ⇒ Object
- #load ⇒ Object
- #path ⇒ Object
- #pretty(pattern = "") ⇒ Object
- #save(config) ⇒ Object
Constructor Details
#initialize(root, options = {}) ⇒ Config
Initialize a Config
24 25 26 27 28 29 |
# File 'lib/github_cli/config.rb', line 24 def initialize(root, ={}) @root = root @filename = .fetch(:filename) { DEFAULT_NAME } @local_config = @global_config = end |
Instance Attribute Details
#filename ⇒ Object
Name for the confiugration file
19 20 21 |
# File 'lib/github_cli/config.rb', line 19 def filename @filename end |
#location ⇒ Object
Location scope of currently used configuration file.
16 17 18 |
# File 'lib/github_cli/config.rb', line 16 def location @location end |
#root ⇒ Object (readonly)
Contains information of where the command is run from.
13 14 15 |
# File 'lib/github_cli/config.rb', line 13 def root @root end |
Instance Method Details
#[](key) ⇒ Object
36 37 38 |
# File 'lib/github_cli/config.rb', line 36 def [](key) data[key] || data["#{COMMAND_KEY}.#{key}"] rescue nil end |
#[]=(key, value) ⇒ Object
31 32 33 34 |
# File 'lib/github_cli/config.rb', line 31 def []=(key, value) set_key(key, value) @data = nil end |
#all ⇒ Object
56 57 58 |
# File 'lib/github_cli/config.rb', line 56 def all data end |
#data ⇒ Object
48 49 50 |
# File 'lib/github_cli/config.rb', line 48 def data @data ||= self.load end |
#delete(key) ⇒ Object
44 45 46 |
# File 'lib/github_cli/config.rb', line 44 def delete(key) data.delete(key) end |
#fetch(key, default = nil) ⇒ Object
40 41 42 |
# File 'lib/github_cli/config.rb', line 40 def fetch(key, default=nil) self[key] || default || raise(IndexError.new("key #{key} not found")) end |
#keys ⇒ Object
52 53 54 |
# File 'lib/github_cli/config.rb', line 52 def keys data.keys end |
#load ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/github_cli/config.rb', line 84 def load yaml = {} if File.exists? path yaml = File.open(path, 'r') do |file| YAML.load(file) end end yaml end |
#path ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/github_cli/config.rb', line 94 def path if location == 'local' || File.exists?() else end end |
#pretty(pattern = "") ⇒ Object
60 61 62 63 64 |
# File 'lib/github_cli/config.rb', line 60 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
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/github_cli/config.rb', line 71 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]= nil end end File.open(path, 'w', 0600) do |file| YAML.dump(config, file) end end |