Class: Shale::Schema::JSONGenerator
- Inherits:
-
Object
- Object
- Shale::Schema::JSONGenerator
- Defined in:
- lib/shale/schema/json_generator.rb,
lib/shale/schema/json_generator/ref.rb,
lib/shale/schema/json_generator/base.rb,
lib/shale/schema/json_generator/date.rb,
lib/shale/schema/json_generator/time.rb,
lib/shale/schema/json_generator/float.rb,
lib/shale/schema/json_generator/value.rb,
lib/shale/schema/json_generator/object.rb,
lib/shale/schema/json_generator/schema.rb,
lib/shale/schema/json_generator/string.rb,
lib/shale/schema/json_generator/boolean.rb,
lib/shale/schema/json_generator/integer.rb,
lib/shale/schema/json_generator/collection.rb
Overview
Class for generating JSON schema
Defined Under Namespace
Classes: Base, Boolean, Collection, Date, Float, Integer, Object, Ref, Schema, String, Time, Value
Class Method Summary collapse
-
.get_json_type(shale_type) ⇒ Shale::Schema::JSONGenerator::Base
private
Return JSON type for given Shale type.
-
.register_json_type(shale_type, json_type) ⇒ Object
Register Shale to JSON type mapping.
Instance Method Summary collapse
-
#as_schema(klass, id: nil, title: nil, description: nil) ⇒ Hash
Generate JSON Schema from Shale model and return it as a Ruby Hash.
-
#to_schema(klass, id: nil, title: nil, description: nil, pretty: false) ⇒ String
Generate JSON Schema from Shale model.
Class Method Details
.get_json_type(shale_type) ⇒ Shale::Schema::JSONGenerator::Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return JSON type for given Shale type
48 49 50 |
# File 'lib/shale/schema/json_generator.rb', line 48 def self.get_json_type(shale_type) @json_types[shale_type] end |
.register_json_type(shale_type, json_type) ⇒ Object
Register Shale to JSON type mapping
33 34 35 |
# File 'lib/shale/schema/json_generator.rb', line 33 def self.register_json_type(shale_type, json_type) @json_types[shale_type] = json_type end |
Instance Method Details
#as_schema(klass, id: nil, title: nil, description: nil) ⇒ Hash
Generate JSON Schema from Shale model and return it as a Ruby Hash
73 74 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 108 109 110 111 112 113 114 |
# File 'lib/shale/schema/json_generator.rb', line 73 def as_schema(klass, id: nil, title: nil, description: nil) unless mapper_type?(klass) raise NotAShaleMapperError, "JSON Shema can't be generated for '#{klass}' type" end types = [] collect_complex_types(types, klass) objects = [] types.each do |type| properties = [] type.json_mapping.keys.values.each do |mapping| attribute = type.attributes[mapping.attribute] next unless attribute if mapper_type?(attribute.type) json_type = Ref.new(mapping.name, attribute.type.model.name, schema: mapping.schema) else json_klass = self.class.get_json_type(attribute.type) if attribute.default && !attribute.collection? value = attribute.type.cast(attribute.default.call) default = attribute.type.as_json(value) end json_type = json_klass.new( mapping.name, default: default, schema: mapping.schema ) end json_type = Collection.new(json_type, schema: mapping.schema) if attribute.collection? properties << json_type end objects << Object.new(type.model.name, properties, type.json_mapping.root) end Schema.new(objects, id: id, title: title, description: description).as_json end |
#to_schema(klass, id: nil, title: nil, description: nil, pretty: false) ⇒ String
Generate JSON Schema from Shale model
129 130 131 132 |
# File 'lib/shale/schema/json_generator.rb', line 129 def to_schema(klass, id: nil, title: nil, description: nil, pretty: false) schema = as_schema(klass, id: id, title: title, description: description) Shale.json_adapter.dump(schema, pretty: pretty) end |