Module: Forward::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/forward/config.rb

Constant Summary collapse

DEFAULTS =
{
  accounts: {},
  default_account: nil,
  auto_copy: true,
  auto_open: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accountsObject

Returns the value of attribute accounts.



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

def accounts
  @accounts
end

#auto_copyObject

Returns the value of attribute auto_copy.



13
14
15
# File 'lib/forward/config.rb', line 13

def auto_copy
  @auto_copy
end

#auto_openObject

Returns the value of attribute auto_open.



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

def auto_open
  @auto_open
end

#default_accountObject

Returns the value of attribute default_account.



12
13
14
# File 'lib/forward/config.rb', line 12

def 
  @default_account
end

Instance Method Details

#add_account(subdomain, api_token) ⇒ Object



34
35
36
37
38
39
# File 'lib/forward/config.rb', line 34

def (subdomain, api_token)
  accounts[subdomain.to_sym] = api_token
  self.       = subdomain

  write
end

#api_keyObject



52
53
54
# File 'lib/forward/config.rb', line 52

def api_key
  accounts[.to_sym]
end

#auto_copy?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/forward/config.rb', line 60

def auto_copy?
  auto_copy == true
end

#auto_open?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/forward/config.rb', line 56

def auto_open?
  auto_open == true
end

#config_pathObject

Returns the location of the forward config file based on the host os.

Returns the String path.



102
103
104
# File 'lib/forward/config.rb', line 102

def config_path
  File.join(ENV['HOME'], '.forwardrc')
end

#create_or_loadObject

Create a config file if it doesn’t exist, load one if it does.

Returns the resulting Config object.



116
117
118
119
120
121
122
123
# File 'lib/forward/config.rb', line 116

def create_or_load
  if present?
    load
  else
    set_defaults!
    write
  end
end

#loadObject

It initializes a new Config instance, updates it with the config values from the config file, and raises an error if there’s not a config or if the config options aren’t valid.

Returns the Config object.

Raises:



130
131
132
133
134
135
# File 'lib/forward/config.rb', line 130

def load
  Forward.logger.debug('[config] loading')
  raise ConfigError, "Unable to find a forward config file at `#{config_path}'" unless present?

  update(YAML.load_file(config_path).symbolize_keys)
end

#present?Boolean

Checks to see if a .forward config file exists.

Returns true or false based on the existence of the config file.

Returns:

  • (Boolean)


109
110
111
# File 'lib/forward/config.rb', line 109

def present?
  File.exist? config_path
end

#remove_account(subdomain) ⇒ Object



41
42
43
44
45
46
# File 'lib/forward/config.rb', line 41

def (subdomain)
  accounts.delete(subdomain.to_sym)
  self. = accounts.empty? ? nil : accounts.keys.first

  write
end

#set_default!(setting) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/forward/config.rb', line 20

def set_default!(setting)
  value = DEFAULTS[setting.to_sym]
  value = value.dup if value.is_a?(Hash) || value.is_a?(Array)
  self.send("#{setting}=", value)

  value
end

#set_default_account(subdomain) ⇒ Object



28
29
30
31
32
# File 'lib/forward/config.rb', line 28

def (subdomain)
  self. = subdomain

  write
end

#set_defaults!Object



16
17
18
# File 'lib/forward/config.rb', line 16

def set_defaults!
  DEFAULTS.keys.each { |setting| set_default!(setting) }
end

#to_hashObject

Converts a Config object to a Hash.

Returns a Hash representation of the object



79
80
81
# File 'lib/forward/config.rb', line 79

def to_hash
  Hash[instance_variables.map { |var| [var[1..-1].to_sym, instance_variable_get(var)] }]
end

#update(attributes) ⇒ Object

Updates an existing Config object.

Returns the updated Config object.



67
68
69
70
71
72
73
74
# File 'lib/forward/config.rb', line 67

def update(attributes)
  Forward.logger.debug('[config] updating')
  attributes.each do |key, value|
    self.send(:"#{key}=", value)
  end

  self
end

#writeObject

Write the current config data to ‘config_path’, and the current private_key to ‘key_path’.

Returns the Config object.



87
88
89
90
91
92
93
94
95
96
# File 'lib/forward/config.rb', line 87

def write
  Forward.logger.debug('[config] writing')

  File.open(config_path, 'w') { |f| f.write(YAML.dump(to_hash)) }

  self
rescue Exception => e
  Forward.logger.fatal "#{e.message}"
  raise ConfigError, 'Unable to write config file'
end