Module: Kovacs::Nationality

Extended by:
Nationality
Included in:
Nationality
Defined in:
lib/kovacs/nationality.rb

Instance Method Summary collapse

Instance Method Details

#generate(nationality = nil) ⇒ Object



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

def generate(nationality = nil)
  case nationality
  when NilClass
    nationalities = Kovacs::Resources.nationalities
    nationalities[rand(nationalities.size)]
  when Symbol
    validate_symbol(nationality)
    nationality
  when Array
    validate_array(nationality)
    nationality[rand(nationality.size)]
  when String
    nationality
  else
    raise ArgumentError.new("Wrong argument type #{ nationality.class }")
  end
end

#validate_array(nats) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kovacs/nationality.rb', line 30

def validate_array(nats)
  if nats.empty? 
    raise ArgumentError.new("Wrong argument array is empty")
  end
  if nats.any? { |el| not el.is_a? Symbol }
    raise ArgumentError.new("Wrong argument one or more elements of array not symbol")
  end
  resource_path = "#{ Kovacs.root }/resources"
  nationalities = Dir.glob(resource_path + '/*').select { |f| File.directory?(f) }.map { |s| s.split('/')[-1].to_sym }
  ret = nats - nationalities
  unless ret.size === 0
    raise ArgumentError.new("Wrong argument one or more elements of array not exist")
  end
end

#validate_symbol(nat) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/kovacs/nationality.rb', line 22

def validate_symbol(nat)
  resource_path = "#{ Kovacs.root }/resources"
  nationalities = Dir.glob(resource_path + '/*').select { |f| File.directory?(f) }.map { |s| s.split('/')[-1].to_sym }
  unless nationalities.include?(nat)
    raise ArgumentError.new("Wrong argument does not exist #{ nat }")
  end
end