Class: Superstudio::Generators::SchemaMapGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
SchemaReader
Defined in:
lib/generators/superstudio/schema_map_generator.rb

Instance Method Summary collapse

Methods included from SchemaReader

#parse_json_schema, #read_schema, #replace_reference_keys, #schema_maps_directory, #schemas_directory

Instance Method Details

#create_map_fileObject



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
# File 'lib/generators/superstudio/schema_map_generator.rb', line 15

def create_map_file
  file_name = name
  file_name = file_name << ".json.schema" unless name.end_with?(".json.schema")

  if class_arg.nil?
    class_name = file_name.chomp(".json.schema").classify
  else
    class_name = class_arg.classify
  end

  interpreted_schema = Superstudio::SqlJsonBuilder.new(nil, file_name)

  file_data = %Q(=begin
Interpreted schema has the following data bodies. Multiple bodies indicates array nesting - non-included node names should not be included in @json_nodes because they are placeholders for array inserts.
)

  interpreted_hashes = []

  interpreted_schema.template_bodies.each do |key, template|
    inter = JSON.parse("{" << template.slice(1..template.length).chomp("}").gsub('{', '"').gsub('}', '"') << "}")
    temp_string = PP.pp(inter, '')
    interpreted_hashes << inter
    file_data << %Q(
Node Path: #{key.join("->")}
#{temp_string})
  end
  file_data << %Q(=end)

  file_data << %Q(
class #{class_name}Mapper < Superstudio::SqlJsonBuilder
  def map_row)
  interpreted_hashes.each do |hash|
    hash.each do |name, node|
      file_data << %Q(
    @json_nodes[:#{node}] = value_by_column_name("#{name}"))
    end
  end

  file_data << %Q(
  end
end)
  create_file "#{schema_maps_directory}/#{class_name.underscore}_mapper.rb", file_data
end