class Viperaptor::UserPreferences

A class that provides methods for working with user-specific information. Currently it has methods for obtaining and saving username, later it may be improved to something more general.

Public Class Methods

add_template_to_history(template_name) click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 18
def self.add_template_to_history(template_name)
  path = obtain_user_preferences_path

  file_contents = open(path).read
  preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

  history = preferences[USER_PREFERENCES_TEMPLATES_HISTORY_KEY] || []
  if history.count == 0 || history[0] != template_name
    history.unshift(template_name)
    max_history = 60
    if history.count > max_history
      history = history.slice(0, max_history)
    end
  end
  preferences[USER_PREFERENCES_TEMPLATES_HISTORY_KEY] = history

  File.open(path, 'w+') { |f| f.write(preferences.to_yaml) }
end
obtain_custom_catalogs_repos() click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 37
def self.obtain_custom_catalogs_repos
  path = obtain_user_preferences_path

  file_contents = open(path).read
  preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

  return preferences[USER_PREFERENCES_CATALOGS_KEY]
end
obtain_templates_history() click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 9
def self.obtain_templates_history
  path = obtain_user_preferences_path

  file_contents = open(path).read
  preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

  return preferences[USER_PREFERENCES_TEMPLATES_HISTORY_KEY] || []
end
obtain_username() click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 46
def self.obtain_username
  path = obtain_user_preferences_path

  file_contents = open(path).read
  preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

  return preferences[USER_PREFERENCES_USERNAME_KEY]
end
save_username(username) click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 55
def self.save_username(username)
  path = obtain_user_preferences_path

  file_contents = open(path).read
  preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

  preferences[USER_PREFERENCES_USERNAME_KEY] = username
  File.open(path, 'w+') { |f| f.write(preferences.to_yaml) }
end

Private Class Methods

obtain_user_preferences_path() click to toggle source
# File lib/viperaptor/configuration/user_preferences.rb, line 67
def self.obtain_user_preferences_path
  home_path = Pathname.new(ENV['HOME'])
             .join(APP_HOME_DIR)

  path_exists = Dir.exist?(home_path)

  unless path_exists
    FileUtils.mkdir_p home_path
  end

  preferences_path = home_path.join(USER_PREFERENCES_FILE)
  preferences_exist = File.file?(preferences_path)

  unless preferences_exist
    File.open(preferences_path, 'w+') { |f| f.write('') }
  end

  return preferences_path
end