Class: SuckySocket::Country

Inherits:
Object
  • Object
show all
Includes:
CountriesSocketsPlugs
Defined in:
lib/sucky_socket/country.rb

Constant Summary

Constants included from CountriesSocketsPlugs

SuckySocket::CountriesSocketsPlugs::COUNTRIES_SOCKETS, SuckySocket::CountriesSocketsPlugs::SOCKETS_PLUGS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CountriesSocketsPlugs

#get_plugs, #get_sockets, #validate_country_name

Constructor Details

#initialize(name = "USA") ⇒ Country

Use USA as default start_country when no country was entered by the user



11
12
13
14
15
16
17
18
19
# File 'lib/sucky_socket/country.rb', line 11

def initialize(name = "USA")
	if validate_country_name(name)
		@name = name
		@sockets = get_sockets("#{@name}")
	else
		puts "Country #{name} was not found in the database."
		exit
	end		
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/sucky_socket/country.rb', line 8

def name
  @name
end

#socketsObject (readonly)

Returns the value of attribute sockets.



8
9
10
# File 'lib/sucky_socket/country.rb', line 8

def sockets
  @sockets
end

Instance Method Details

#needs_adapter_for?(dest_country) ⇒ Boolean

Checks whether an adapter is needed by comparing whether the plugs of the start country match the plugs that work with every particular socket of the destination country

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sucky_socket/country.rb', line 22

def needs_adapter_for?(dest_country)

	dest_plugs = []
	dest_country.sockets.each do |s|
		dest_plugs << get_plugs(s)
	end
	

	problem_plugs = []
	dest_plugs.each do |dp|
		problem_plugs << (sockets - dp)
	end

	if problem_plugs.flatten.size == 0
		puts "\nNo adapter needed when travelling from #{name} to #{dest_country.name}."
		return false
	elsif problem_plugs.select { |pp| pp.sort != sockets }.size == 0 
		puts "\nAdapter is definetely needed when travelling from #{name} to #{dest_country.name} because the plugs in #{name} don't fit any of the sockets in #{dest_country.name}."
		return true
	else
		puts "\nAdapter is needed when travelling from #{name} to #{dest_country.name} because:"
		problem_plugs.each_with_index do |plug, ind|
			if plug.size == 1
				puts "  - The plug type #{plug.join(', ')} does not fit into socket type #{dest_country.sockets[ind]}"
			elsif plug.size > 1
				puts "  - The plug types #{plug.join(', ')} don't fit into socket type #{dest_country.sockets[ind]}"
			end
		end
		return true
	end

end