Class: Gas::Config
- Inherits:
-
Object
- Object
- Gas::Config
- Defined in:
- lib/gas/config.rb
Overview
Class that keeps track of users
Instance Attribute Summary collapse
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
-
#[](nickname) ⇒ User|nil
Override [] to get hash style acces to users.
-
#add(user) ⇒ Object
Adds a user.
-
#delete(nickname) ⇒ Object
Deletes a user by nickname.
-
#exists?(nickname) ⇒ Boolean
Checks if a user with nickname exists.
-
#get(nickname) ⇒ User|nil
Returns the user with nickname nil if no such user exists.
-
#initialize(users = nil, config = nil) ⇒ Config
constructor
Initializes the object.
-
#is_current_user(name, email, object) ⇒ Object
Scans the @users (a string containing info formatted identical to the gas.author file) …and checks to see if it’s name and email match what you’re looking for.
-
#migrate_to_gas_dir! ⇒ Object
This function checks for a ~/.gas FILE and if it exists, it puts it into memory and deletes it from the HDD then it creates the ~/.gas FOLDER and saves the old .gas file as ~/git.conf.
-
#save! ⇒ Object
Saves the current users to the config file.
-
#to_s ⇒ Object
Override to_s to output correct format.
Constructor Details
#initialize(users = nil, config = nil) ⇒ Config
Initializes the object. If no users are supplied we look for a config file, if none then create it, and parse it to load users
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gas/config.rb', line 36 def initialize(users = nil, config = nil) migrate_to_gas_dir! # Migrates old users to the new configuration file location, how thoughtful of me, I know @config_file = "#{GAS_DIRECTORY}/gas.authors" @gas_dir = GAS_DIRECTORY @config = '' if config.nil? if !File.exists? @config_file Dir::mkdir(@gas_dir) unless File.exists? @gas_dir FileUtils.touch @config_file end @config = File.read(@config_file) else @config = config end if users.nil? @users = [] @config.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nickname, name, email| @users << User.new(name, email, nickname) end else @users = users end end |
Instance Attribute Details
#users ⇒ Object (readonly)
Returns the value of attribute users.
7 8 9 |
# File 'lib/gas/config.rb', line 7 def users @users end |
Instance Method Details
#[](nickname) ⇒ User|nil
Override [] to get hash style acces to users
92 93 94 |
# File 'lib/gas/config.rb', line 92 def [](nickname) get nickname end |
#add(user) ⇒ Object
Adds a user
98 99 100 |
# File 'lib/gas/config.rb', line 98 def add(user) @users << user end |
#delete(nickname) ⇒ Object
Deletes a user by nickname
104 105 106 107 108 |
# File 'lib/gas/config.rb', line 104 def delete(nickname) @users.delete_if do |user| user.nickname == nickname end end |
#exists?(nickname) ⇒ Boolean
Checks if a user with nickname exists
66 67 68 69 70 71 72 73 74 |
# File 'lib/gas/config.rb', line 66 def exists?(nickname) @users.each do |user| if user.nickname == nickname return true; end end false end |
#get(nickname) ⇒ User|nil
Returns the user with nickname nil if no such user exists
79 80 81 82 83 84 85 86 87 |
# File 'lib/gas/config.rb', line 79 def get(nickname) @users.each do |user| if user.nickname == nickname.to_s return user end end nil end |
#is_current_user(name, email, object) ⇒ Object
Scans the @users (a string containing info formatted identical to the gas.author file)
...and checks to see if it's name and email match what you're looking for
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/gas/config.rb', line 136 def is_current_user(name, email, object) object.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nicknamec, namec, emailc| if namec == name and emailc == email # check if ssh is active # TODO: Check if its SSH key is setup, and indicate SSH ACTIVE return true end end return false # could not get a current user's nickname end |
#migrate_to_gas_dir! ⇒ Object
This function checks for a ~/.gas FILE and if it exists, it puts it into memory and deletes it from the HDD then it creates the ~/.gas FOLDER and saves the old .gas file as ~/git.conf
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gas/config.rb', line 12 def migrate_to_gas_dir! old_config_file = GAS_DIRECTORY config_dir = GAS_DIRECTORY new_config_file = GAS_DIRECTORY + "/gas.authors" if File.file? old_config_file file = File.open(old_config_file, "rb") contents = file.read file.close File.delete old_config_file Dir::mkdir(config_dir) file = File.new(new_config_file, "w") file.puts contents file.close end end |
#save! ⇒ Object
Saves the current users to the config file
111 112 113 114 115 |
# File 'lib/gas/config.rb', line 111 def save! File.open @config_file, 'w' do |file| file.write self end end |
#to_s ⇒ Object
Override to_s to output correct format
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gas/config.rb', line 119 def to_s gc = Gitconfig.new users = @users.map do |user| if is_current_user(gc.current_user_object[:name], gc.current_user_object[:email], user.to_s) " ==>" + user.to_s[5,user.to_s.length] else user.to_s end end.join("\n") return users end |