Class: Gas::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/gas/users.rb

Overview

Class that keeps track of users

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Users

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

Parameters:

  • config_file (String)

    The path to the file that stores users



11
12
13
14
15
16
# File 'lib/gas/users.rb', line 11

def initialize(config_file)
  @config_file = config_file
  @users = []

  setup!
end

Instance Attribute Details

#usersObject (readonly)

Returns the value of attribute users.



7
8
9
# File 'lib/gas/users.rb', line 7

def users
  @users
end

Instance Method Details

#[](nickname) ⇒ User|nil

Override [] to get hash style acces to users

Parameters:

  • nickname (String|Symbol)

Returns:



47
48
49
# File 'lib/gas/users.rb', line 47

def [](nickname)
  get nickname
end

#add(user) ⇒ Object

Adds a user

Parameters:



53
54
55
# File 'lib/gas/users.rb', line 53

def add(user)
  @users << user
end

#delete(nickname) ⇒ Object

Deletes a user by nickname

Parameters:

  • nickname (String)

    The nickname of the user to delete



59
60
61
62
63
# File 'lib/gas/users.rb', line 59

def delete(nickname)
  @users.delete_if do |user|
    user.nickname == nickname
  end
end

#exists?(nickname) ⇒ Boolean

Checks if a user with nickname exists

Parameters:

  • nickname (String)

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/gas/users.rb', line 21

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

Parameters:

  • nickname (String|Symbol)

Returns:



34
35
36
37
38
39
40
41
42
# File 'lib/gas/users.rb', line 34

def get(nickname)
  users.each do |user|
    if user.nickname == nickname.to_s
      return user
    end
  end

  nil
end

#save!Object

Saves the current users to the config file



66
67
68
69
70
# File 'lib/gas/users.rb', line 66

def save!
  File.open @config_file, 'w' do |file|
    file.write self
  end
end

#setup!Object

Run the setup steps



85
86
87
88
89
# File 'lib/gas/users.rb', line 85

def setup!
  ensure_config_directory_exists!
  load_config
  load_users
end

#to_sObject

Override to_s to output correct format



73
74
75
76
77
78
79
80
81
82
# File 'lib/gas/users.rb', line 73

def to_s
  current_user = GitConfig.current_user
  users.map do |user|
    if current_user == user
      "  ==> #{user.to_s[5,user.to_s.length]}"
    else
      user.to_s
    end
  end.join "\n"
end