Class: MTCLI::Config

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/mtcli/config.rb

Overview

configuration file class.

Constant Summary collapse

CONFIG_DIRECTORY =
'.mtcli'.freeze
CURRENT_BASENAME =
'.CURRENT'.freeze
YAML_EXTENSION =
'.yml'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

symbolize_keys

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_tokenObject

Returns the value of attribute access_token.



15
16
17
# File 'lib/mtcli/config.rb', line 15

def access_token
  @access_token
end

#base_urlObject

Returns the value of attribute base_url.



15
16
17
# File 'lib/mtcli/config.rb', line 15

def base_url
  @base_url
end

#endpointsObject

Returns the value of attribute endpoints.



15
16
17
# File 'lib/mtcli/config.rb', line 15

def endpoints
  @endpoints
end

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/mtcli/config.rb', line 15

def version
  @version
end

Class Method Details

.allObject



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_dirObject



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

.currentObject



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_pathObject



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_currentObject



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_pathObject



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

Returns:

  • (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

Returns:

  • (Boolean)


122
123
124
# File 'lib/mtcli/config.rb', line 122

def logged_in?
  @access_token && !@access_token.empty? ? true : false
end

#saveObject



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_currentObject



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_sObject



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