Class: RolesCounter

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

Class Method Summary collapse

Class Method Details

.counter(roles_string) ⇒ Object

Static method that receives the string with all the roles in the database stored in a hash separated by commas. The string is split and is returned in a hash indicating the role and its ocurrences.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rolesCounter.rb', line 5

def self.counter(roles_string)
  @roles_string = roles_string
  # Checks if the string is valid calling the method
  # stringCheck, if it is will return the hash
  # with the roles and the corresponding count.
  if stringCheck(@roles_string) then
    @roles_array = Array.new
    @roles_array = @roles_string.split(", ")
    return @roles_array.group_by(&:to_s).map {
      |a| [a[0], a[1].count]
    }.to_h
  else
    return "Invalid group of roles."
  end
end

.stringCheck(string_check) ⇒ Object

Method that checks if the given string is a valid string separated by commas and a space after the comma.



23
24
25
26
# File 'lib/rolesCounter.rb', line 23

def self.stringCheck(string_check)
  @string_check = string_check
  return @string_check =~ /\A[a-zA-Z\s]+(,[\sa-zA-Z]+)*\z/
end