Module: Superstudio::SchemaReader

Included in:
Generators::SchemaGenerator, Generators::SchemaMapGenerator, SqlJsonBuilder
Defined in:
lib/superstudio/schema_reader.rb

Instance Method Summary collapse

Instance Method Details

#parse_json_schema(file_name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/superstudio/schema_reader.rb', line 19

def parse_json_schema(file_name)
  directory = schemas_directory
  master_file = directory.join(file_name)
  contents = File.read(master_file)
  schema = JSON.parse(contents)
  return schema
end

#read_schema(file_name) ⇒ Object



46
47
48
49
# File 'lib/superstudio/schema_reader.rb', line 46

def read_schema(file_name)
  json_hash = parse_json_schema(file_name)
  json_hash = replace_reference_keys(json_hash)
end

#replace_reference_keys(json_hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/superstudio/schema_reader.rb', line 27

def replace_reference_keys(json_hash)
  json_hash.each do |key, value|
    if value.is_a?(Hash)
      json_hash[key] = replace_reference_keys(value)
    end
  end

  if json_hash.keys.include?("$ref")
    # split on a hash character - the first piece is the file name
    file_name = json_hash["$ref"].split('#')[0]
    referenced_contents = parse_json_schema(file_name)
    referenced_contents = replace_reference_keys(referenced_contents)
    json_hash.delete("$ref")
    json_hash = json_hash.merge(referenced_contents)
  end
  
  return json_hash
end

#schema_maps_directoryObject



11
12
13
14
15
16
17
# File 'lib/superstudio/schema_reader.rb', line 11

def schema_maps_directory
  if ENV['JSON_SCHEMA_MAPS_DIRECTORY'].nil?
    Rails.root.join('app', 'json_schemas')
  else
    Pathname.new(ENV['JSON_SCHEMA_MAPS_DIRECTORY'])
  end
end

#schemas_directoryObject



3
4
5
6
7
8
9
# File 'lib/superstudio/schema_reader.rb', line 3

def schemas_directory
  if ENV['JSON_SCHEMAS_DIRECTORY'].nil?
    Rails.root.join('app', 'json_schemas')
  else
    Pathname.new(ENV['JSON_SCHEMAS_DIRECTORY'])
  end
end