Module: Ptf::Commands::Group::Add

Defined in:
lib/ptf/commands/group/add.rb

Class Method Summary collapse

Class Method Details

.add(group, abbrev) ⇒ Object



7
8
9
10
11
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
37
38
# File 'lib/ptf/commands/group/add.rb', line 7

def add(group, abbrev)
  if Ptf::Group.group_exist?(group)
    return "Group already exists using the name #{group}."
  elsif Ptf::Group.group_exist?(abbrev)
    return "Group already exists using the abbreviation #{abbrev}."
  end

  if abbrev.length > 4
    return "Abbreviation cannot be longer than 4 characters."
  end

  if group != group.downcase
    return "Group names must be entirely lowercase."
  elsif abbrev != abbrev.upcase
    return "Group abbreviations must be entirely uppercase."
  end

  new_group = Ptf::Group.new(group, abbrev)

  group_file = Ptf::FileSystem.group_list_file

  File.open(group_file, 'a') { |f| f.write("#{new_group.to_s}\n") }

  open_dir = Ptf::FileSystem.
  closed_dir = Ptf::FileSystem.

  [open_dir, closed_dir].each do |d|
    Dir.mkdir(File.join(d, new_group.name), Ptf::FileSystem.file_permission)
  end

  return "Added group #{new_group.to_s} successfully!"
end