Class: Shale::Schema::JSONCompiler
- Inherits:
-
Object
- Object
- Shale::Schema::JSONCompiler
- Defined in:
- lib/shale/schema/json_compiler.rb
Overview
Class for compiling JSON schema into Ruby data model
Constant Summary collapse
- DEFAULT_ROOT_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default root type name
'root'
- MODEL_TEMPLATE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Shale model template
ERB.new(<<~TEMPLATE, trim_mode: '-') require 'shale' <%- unless type.references.empty? -%> <%- type.references.each do |property| -%> require_relative '<%= type.relative_path(property.type.file_name) %>' <%- end -%> <%- end -%> <%- type.modules.each_with_index do |name, i| -%> <%= ' ' * i %>module <%= name %> <%- end -%> <%- indent = ' ' * type.modules.length -%> <%= indent %>class <%= type.root_name %> < Shale::Mapper <%- type.properties.each do |property| -%> <%= indent %>attribute :<%= property.attribute_name %>, <%= property.type.name -%> <%- if property.collection? %>, collection: true<% end -%> <%- unless property.default.nil? %>, default: -> { <%= property.default %> }<% end %> <%- end -%> <%= indent %>json do <%- type.properties.each do |property| -%> <%= indent %>map '<%= property.mapping_name %>', to: :<%= property.attribute_name %> <%- end -%> <%= indent %>end <%= indent %>end <%- type.modules.length.times do |i| -%> <%= ' ' * (type.modules.length - i - 1) %>end <%- end -%> TEMPLATE
Instance Method Summary collapse
-
#as_models(schemas, root_name: nil, namespace_mapping: nil) ⇒ Array<Shale::Schema::Compiler::Complex>
Generate Shale models from JSON Schema and return them as a Ruby Array of objects.
-
#to_models(schemas, root_name: nil, namespace_mapping: nil) ⇒ Hash<String, String>
Generate Shale models from JSON Schema.
Instance Method Details
#as_models(schemas, root_name: nil, namespace_mapping: nil) ⇒ Array<Shale::Schema::Compiler::Complex>
Generate Shale models from JSON Schema and return them as a Ruby Array of objects
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/shale/schema/json_compiler.rb', line 75 def as_models(schemas, root_name: nil, namespace_mapping: nil) schemas = schemas.map do |schema| Shale.json_adapter.load(schema) end @root_name = root_name @namespace_mapping = namespace_mapping || {} @schema_repository = {} @types = [] schemas.each do |schema| disassemble_schema(schema) end compile(schemas[0], true) total_duplicates = Hash.new(0) duplicates = Hash.new(0) @types.each do |type| total_duplicates[type.name] += 1 end @types.each do |type| duplicates[type.name] += 1 if total_duplicates[type.name] > 1 type.root_name = format("#{type.root_name}%d", duplicates[type.name]) end end @types.reverse end |
#to_models(schemas, root_name: nil, namespace_mapping: nil) ⇒ Hash<String, String>
Generate Shale models from JSON Schema
123 124 125 126 127 128 129 |
# File 'lib/shale/schema/json_compiler.rb', line 123 def to_models(schemas, root_name: nil, namespace_mapping: nil) types = as_models(schemas, root_name: root_name, namespace_mapping: namespace_mapping) types.to_h do |type| [type.file_name, MODEL_TEMPLATE.result(binding)] end end |