Class: Gas::Config

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

Overview

Class that keeps track of users

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • users (Array<User>) (defaults to: nil)

    The override users

  • config (String) (defaults to: nil)

    The override config



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gas/config.rb', line 12

def initialize(users = nil, config = nil)
  @config_file = "#{GAS_DIRECTORY}/gas.authors"
  @gas_dir = GAS_DIRECTORY
  @config = ''

  if config.nil?
    if !File.exists? @config_file
      Dir::mkdir(@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

#usersObject (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

Parameters:

  • nickname (String|Symbol)

Returns:



67
68
69
# File 'lib/gas/config.rb', line 67

def [](nickname)
  get nickname
end

#add(user) ⇒ Object

Adds a user

Parameters:



73
74
75
# File 'lib/gas/config.rb', line 73

def add(user)
  @users << user
end

#delete(nickname) ⇒ Object

Deletes a user by nickname

Parameters:

  • nickname (String)

    The nickname of the user to delete



79
80
81
82
83
# File 'lib/gas/config.rb', line 79

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)


41
42
43
44
45
46
47
48
49
# File 'lib/gas/config.rb', line 41

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:



54
55
56
57
58
59
60
61
62
# File 'lib/gas/config.rb', line 54

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


111
112
113
114
115
116
117
118
119
120
# File 'lib/gas/config.rb', line 111

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

#save!Object

Saves the current users to the config file



86
87
88
89
90
# File 'lib/gas/config.rb', line 86

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

#to_sObject

Override to_s to output correct format



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gas/config.rb', line 94

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