-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
36 lines (29 loc) · 813 Bytes
/
model.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
require 'json'
# launch model structs
Payload = Struct.new(:name, :country, :operator, :orbit, :function, :decay, :outcome)
Rocket = Struct.new(:name, :country)
LaunchSite = Struct.new(:name, :country)
LaunchServiceProvider = Struct.new(:name, :country)
Launch = Struct.new(:date, :rocket, :launch_site, :lsp, :payloads, :remarks)
# generate
class Struct
def to_map
map = Hash.new
self.members.each { |m| map[m] = self[m] }
map
end
def to_json(*a)
to_map.to_json(*a)
end
end
# load
class Launch
def self.from_json json
l = Launch.new *json.values
l.rocket = Rocket.new *l.rocket.values
l.launch_site = LaunchSite.new *l.launch_site.values
l.lsp = LaunchServiceProvider.new *l.lsp.values
l.payloads = l.payloads.map { |p| Payload.new *p.values }
l
end
end