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
69
70
71
72
73
74
75
|
# File 'lib/goon_model_gen/source/loader.rb', line 24
def load_source_yaml(context, path)
erb = ERB.new(::File.read(path), nil, "-")
erb.filename = path
txt = erb.result
raw = YAML.load(txt)
f = File.new(path).tap do |f|
f.context = context
context.files.push(f)
end
(raw['types'] || {}).each do |name, t|
source_type =
if t['fields'].is_a?(Hash)
load_struct(f, name, t).tap do |s|
if g = t['goon']
s.id_name = g['id_name']
s.id_type = g['id_type']
end
end
elsif t['enum_map'].is_a?(Hash) && t['base']
f.new_enum(name, t['base'], t['enum_map'].map{|k,v| {k => v}})
elsif t['enum'].is_a?(Array) && t['base']
f.new_enum(name, t['base'], t['enum'])
elsif t['slice_of'].is_a?(String)
f.new_named_slice(name, t['slice_of'])
else
raise "Unsupported type definition named '#{name}': #{t.inspect}"
end
source_type.generators =
case t['generates']
when false then Hash.new({}) when nil, true then Hash.new(nil) when Hash then
t['generates'].each_with_object({}) do |(k,v), d|
d[k] =
case v
when false then {}
when nil, true then nil
when Array then v.each_with_object({}){|i,d| d[i] = true}
when Hash then
v.default = v.delete('default')
v
else raise "Invalid generates value in #{k.inspect}: #{v.inspect}"
end
end
when Array then t['generates'].each_with_object({}){|i,d| d[i] = true}
else raise "Invalid generates: #{t['generates'].inspect}"
end
end
end
|