Class: MTCLI::Config
Overview
configuration file class.
Constant Summary collapse
- CONFIG_DIRECTORY =
'.mtcli'.freeze
- CURRENT_BASENAME =
'.CURRENT'.freeze
- YAML_EXTENSION =
'.yml'.freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#endpoints ⇒ Object
Returns the value of attribute endpoints.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .all ⇒ Object
- .config_dir ⇒ Object
- .create(basename, hash) ⇒ Object
- .current ⇒ Object
- .current=(basename) ⇒ Object
- .current_file_path ⇒ Object
- .delete(basename) ⇒ Object
- .delete_current ⇒ Object
- .file_path(basename) ⇒ Object
- .get(basename) ⇒ Object
- .real_current_file_path ⇒ Object
- .rename(basename, new_basename) ⇒ Object
- .update(basename, hash) ⇒ Object
Instance Method Summary collapse
- #current? ⇒ Boolean
-
#initialize(file_path, hash = {}) ⇒ Config
constructor
A new instance of Config.
- #logged_in? ⇒ Boolean
- #save ⇒ Object
- #set_current ⇒ Object
- #to_s ⇒ Object
Methods included from Util
Constructor Details
#initialize(file_path, hash = {}) ⇒ Config
Returns a new instance of Config.
17 18 19 20 21 |
# File 'lib/mtcli/config.rb', line 17 def initialize(file_path, hash = {}) @file_path = file_path @version = MT::DataAPI::Client::EndpointManager::DEFAULT_API_VERSION set(hash) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
15 16 17 |
# File 'lib/mtcli/config.rb', line 15 def access_token @access_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
15 16 17 |
# File 'lib/mtcli/config.rb', line 15 def base_url @base_url end |
#endpoints ⇒ Object
Returns the value of attribute endpoints.
15 16 17 |
# File 'lib/mtcli/config.rb', line 15 def endpoints @endpoints end |
#version ⇒ Object
Returns the value of attribute version.
15 16 17 |
# File 'lib/mtcli/config.rb', line 15 def version @version end |
Class Method Details
.all ⇒ Object
23 24 25 26 27 28 |
# File 'lib/mtcli/config.rb', line 23 def self.all pattern = File.join(config_dir, '*' + YAML_EXTENSION) Dir.glob(pattern).map do |file_path| get(file_path) end end |
.config_dir ⇒ Object
80 81 82 |
# File 'lib/mtcli/config.rb', line 80 def self.config_dir File.join(ENV['HOME'], CONFIG_DIRECTORY) end |
.create(basename, hash) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/mtcli/config.rb', line 37 def self.create(basename, hash) get(basename) && (return nil) f = file_path(basename) config = new(f, hash) config.save config end |
.current ⇒ Object
64 65 66 |
# File 'lib/mtcli/config.rb', line 64 def self.current get(real_current_file_path) end |
.current=(basename) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/mtcli/config.rb', line 68 def self.current=(basename) (config = get(basename)) || (return nil) return config if config.current? config.set_current config end |
.current_file_path ⇒ Object
89 90 91 |
# File 'lib/mtcli/config.rb', line 89 def self.current_file_path file_path(CURRENT_BASENAME) end |
.delete(basename) ⇒ Object
52 53 54 55 56 |
# File 'lib/mtcli/config.rb', line 52 def self.delete(basename) (config = get(basename)) || (return nil) config.delete config end |
.delete_current ⇒ Object
75 76 77 78 |
# File 'lib/mtcli/config.rb', line 75 def self.delete_current return false unless File.exist?(current_file_path) File.delete(current_file_path) == 1 || raise end |
.file_path(basename) ⇒ Object
84 85 86 87 |
# File 'lib/mtcli/config.rb', line 84 def self.file_path(basename) return basename if !basename || basename[0] == '/' File.join(config_dir, basename + YAML_EXTENSION) end |
.get(basename) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/mtcli/config.rb', line 30 def self.get(basename) f = file_path(basename) return nil unless File.exist?(f) hash = YAML.load_file(f) new(f, hash) end |
.real_current_file_path ⇒ Object
93 94 95 96 97 |
# File 'lib/mtcli/config.rb', line 93 def self.real_current_file_path f = current_file_path return nil unless File.exist?(f) && File.ftype(f) == 'link' File.readlink(f) end |
.rename(basename, new_basename) ⇒ Object
58 59 60 61 62 |
# File 'lib/mtcli/config.rb', line 58 def self.rename(basename, new_basename) (config = get(basename)) || (return nil) config.rename(new_basename) || (return nil) config end |
.update(basename, hash) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/mtcli/config.rb', line 45 def self.update(basename, hash) (config = get(basename)) || (return nil) config.set(hash) config.save config end |
Instance Method Details
#current? ⇒ Boolean
126 127 128 |
# File 'lib/mtcli/config.rb', line 126 def current? @file_path == self.class.real_current_file_path ? true : false end |
#logged_in? ⇒ Boolean
122 123 124 |
# File 'lib/mtcli/config.rb', line 122 def logged_in? @access_token && !@access_token.empty? ? true : false end |
#save ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/mtcli/config.rb', line 99 def save raise unless @base_url || @base_url.empty? File.open(@file_path, 'w') do |file| YAML.dump(to_hash, file) end true end |
#set_current ⇒ Object
107 108 109 110 |
# File 'lib/mtcli/config.rb', line 107 def set_current self.class.delete_current File.symlink(@file_path, self.class.current_file_path).zero? || raise end |
#to_s ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/mtcli/config.rb', line 112 def to_s hash = stringify_keys(basename => { base_url: @base_url, current: current?, login: logged_in?, version: @version }) YAML.dump(hash) end |