-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
add_vehicle.rb
executable file
·68 lines (55 loc) · 1.75 KB
/
add_vehicle.rb
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
require 'json'
require 'tty-prompt'
require 'securerandom'
require_relative 'lib/vehicle_creator/cli'
require_relative 'lib/vehicle_creator/data_store'
require_relative 'lib/vehicle_creator/vehicle'
require_relative 'lib/vehicle_creator/charging_details'
require_relative 'lib/vehicle_creator/validators'
class VehicleCreator
def initialize(data_file = nil, prompt = nil)
@data_store = DataStore.new(data_file || default_data_file)
@cli = CLI.new(prompt || TTY::Prompt.new)
end
def run
loop do
vehicle = Vehicle.new
# Brand selection
brand = @cli.select_brand(@data_store.existing_brands)
return if brand == CLI::EXIT_OPTION
next if brand == CLI::BACK_OPTION
vehicle.add_brand(brand, @data_store.find_or_create_brand_id(brand))
# Model selection
model = @cli.select_model(@data_store.existing_models(brand))
return if model == CLI::EXIT_OPTION
next if model == CLI::BACK_OPTION
vehicle.add_model(model)
# Vehicle type
vehicle_type = @cli.select_vehicle_type
return if vehicle_type == CLI::EXIT_OPTION
next if vehicle_type == CLI::BACK_OPTION
vehicle.add_vehicle_type(vehicle_type)
# Collect details
if @cli.collect_and_add_details(vehicle)
if @cli.display_and_confirm_vehicle(vehicle.data)
@data_store.save_vehicle(vehicle.data)
break unless @cli.add_another?
else
next if @cli.start_over?
break
end
else
next if @cli.start_over?
break
end
end
end
private
def default_data_file
File.join(File.dirname(__FILE__), 'data/ev-data.json')
end
end
if __FILE__ == $PROGRAM_NAME
VehicleCreator.new.run
end