Class: Generambo::UserPreferences

Inherits:
Object
  • Object
show all
Defined in:
lib/generambo/configuration/user_preferences.rb

Overview

A class that provides methods for working with user-specific information.

Class Method Summary collapse

Class Method Details

.obtain_user_preferences_pathObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generambo/configuration/user_preferences.rb', line 27

def obtain_user_preferences_path
  home_path = Pathname.new(ENV['HOME']).join(GENERAMBO_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

  preferences_path
end

.obtain_usernameObject



8
9
10
11
12
13
14
15
# File 'lib/generambo/configuration/user_preferences.rb', line 8

def obtain_username
  path = obtain_user_preferences_path

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

  preferences[USERNAME_KEY]
end

.save_username(username) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/generambo/configuration/user_preferences.rb', line 17

def save_username(username)
  path = obtain_user_preferences_path

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

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