Class: Viperaptor::UserPreferences
- Inherits:
-
Object
- Object
- Viperaptor::UserPreferences
- Defined in:
- lib/viperaptor/configuration/user_preferences.rb
Overview
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.
Class Method Summary collapse
- .add_template_to_history(template_name) ⇒ Object
- .obtain_custom_catalogs_repos ⇒ Object
- .obtain_templates_history ⇒ Object
- .obtain_username ⇒ Object
- .save_username(username) ⇒ Object
Class Method Details
.add_template_to_history(template_name) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 ⇒ Object
37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
9 10 11 12 13 14 15 16 |
# 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 ⇒ Object
46 47 48 49 50 51 52 53 |
# 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) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# 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 |