module Forward::Config

Constants

DEFAULTS

Attributes

accounts[RW]
auto_copy[RW]
auto_open[RW]
default_account[RW]

Public Instance Methods

add_account(subdomain, api_token) click to toggle source
# File lib/forward/config.rb, line 34
def add_account(subdomain, api_token)
  accounts[subdomain.to_sym] = api_token
  self.default_account       = subdomain

  write
end
api_key() click to toggle source
# File lib/forward/config.rb, line 52
def api_key
  accounts[default_account.to_sym]
end
auto_copy?() click to toggle source
# File lib/forward/config.rb, line 60
def auto_copy?
  auto_copy == true
end
auto_open?() click to toggle source
# File lib/forward/config.rb, line 56
def auto_open?
  auto_open == true
end
config_path() click to toggle source

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

Returns the String path.

# File lib/forward/config.rb, line 102
def config_path
  File.join(ENV['HOME'], '.forwardrc')
end
create_or_load() click to toggle source

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

Returns the resulting Config object.

# File lib/forward/config.rb, line 116
def create_or_load
  if present?
    load
  else
    set_defaults!
    write
  end
end
load() click to toggle source

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.

# 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?() click to toggle source

Checks to see if a .forward config file exists.

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

# File lib/forward/config.rb, line 109
def present?
  File.exist? config_path
end
remove_account(subdomain) click to toggle source
# File lib/forward/config.rb, line 41
def remove_account(subdomain)
  accounts.delete(subdomain.to_sym)
  self.default_account = accounts.empty? ? nil : accounts.keys.first

  write
end
set_default!(setting) click to toggle source
# 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) click to toggle source
# File lib/forward/config.rb, line 28
def set_default_account(subdomain)
  self.default_account = subdomain

  write
end
set_defaults!() click to toggle source
# File lib/forward/config.rb, line 16
def set_defaults!
  DEFAULTS.keys.each { |setting| set_default!(setting) }
end
to_hash() click to toggle source

Converts a Config object to a Hash.

Returns a Hash representation of the object

# 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) click to toggle source

Updates an existing Config object.

Returns the updated Config object.

# 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
write() click to toggle source

Write the current config data to `config_path', and the current private_key to `key_path'.

Returns the Config object.

# 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