Module: Forward::Config
Constant Summary collapse
- DEFAULTS =
{ accounts: {}, default_account: nil, auto_copy: true, auto_open: false }.freeze
Instance Attribute Summary collapse
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#auto_copy ⇒ Object
Returns the value of attribute auto_copy.
-
#auto_open ⇒ Object
Returns the value of attribute auto_open.
-
#default_account ⇒ Object
Returns the value of attribute default_account.
Instance Method Summary collapse
- #add_account(subdomain, api_token) ⇒ Object
- #api_key ⇒ Object
- #auto_copy? ⇒ Boolean
- #auto_open? ⇒ Boolean
-
#config_path ⇒ Object
Returns the location of the forward config file based on the host os.
-
#create_or_load ⇒ Object
Create a config file if it doesn’t exist, load one if it does.
-
#load ⇒ Object
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.
-
#present? ⇒ Boolean
Checks to see if a .forward config file exists.
- #remove_account(subdomain) ⇒ Object
- #set_default!(setting) ⇒ Object
- #set_default_account(subdomain) ⇒ Object
- #set_defaults! ⇒ Object
-
#to_hash ⇒ Object
Converts a Config object to a Hash.
-
#update(attributes) ⇒ Object
Updates an existing Config object.
-
#write ⇒ Object
Write the current config data to ‘config_path’, and the current private_key to ‘key_path’.
Instance Attribute Details
#accounts ⇒ Object
Returns the value of attribute accounts.
11 12 13 |
# File 'lib/forward/config.rb', line 11 def accounts @accounts end |
#auto_copy ⇒ Object
Returns the value of attribute auto_copy.
13 14 15 |
# File 'lib/forward/config.rb', line 13 def auto_copy @auto_copy end |
#auto_open ⇒ Object
Returns the value of attribute auto_open.
14 15 16 |
# File 'lib/forward/config.rb', line 14 def auto_open @auto_open end |
#default_account ⇒ Object
Returns the value of attribute default_account.
12 13 14 |
# File 'lib/forward/config.rb', line 12 def default_account @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 add_account(subdomain, api_token) accounts[subdomain.to_sym] = api_token self.default_account = subdomain write end |
#api_key ⇒ Object
52 53 54 |
# File 'lib/forward/config.rb', line 52 def api_key accounts[default_account.to_sym] end |
#auto_copy? ⇒ Boolean
60 61 62 |
# File 'lib/forward/config.rb', line 60 def auto_copy? auto_copy == true end |
#auto_open? ⇒ Boolean
56 57 58 |
# File 'lib/forward/config.rb', line 56 def auto_open? auto_open == true end |
#config_path ⇒ Object
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_load ⇒ Object
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 |
#load ⇒ Object
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.
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.
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 remove_account(subdomain) accounts.delete(subdomain.to_sym) self.default_account = 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 set_default_account(subdomain) self.default_account = 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_hash ⇒ Object
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 |
#write ⇒ Object
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.}" raise ConfigError, 'Unable to write config file' end |