Class: RailsAutodoc::SchemaMapper
- Inherits:
-
Object
- Object
- RailsAutodoc::SchemaMapper
- Defined in:
- lib/rails_autodoc/schema_mapper.rb
Constant Summary collapse
- TYPE_MAP =
{ string: { type: "string" }, text: { type: "string" }, citext: { type: "string" }, uuid: { type: "string", format: "uuid" }, integer: { type: "integer" }, bigint: { type: "integer", format: "int64" }, float: { type: "number", format: "float" }, decimal: { type: "number", format: "double" }, boolean: { type: "boolean" }, datetime: { type: "string", format: "date-time" }, date: { type: "string", format: "date" }, time: { type: "string", format: "time" }, json: { type: "object" }, jsonb: { type: "object" } }.freeze
Instance Method Summary collapse
- #all_model_schemas ⇒ Object
- #apply_types!(schema, model_name: nil) ⇒ Object
- #infer_model_from_controller(controller_class) ⇒ Object
-
#initialize(schema_path: default_schema_path) ⇒ SchemaMapper
constructor
A new instance of SchemaMapper.
- #model_schema(model_name) ⇒ Object
Constructor Details
#initialize(schema_path: default_schema_path) ⇒ SchemaMapper
Returns a new instance of SchemaMapper.
22 23 24 25 26 27 |
# File 'lib/rails_autodoc/schema_mapper.rb', line 22 def initialize(schema_path: default_schema_path) @schema_path = schema_path @tables = {} @models = {} load_schema if @schema_path&.exist? end |
Instance Method Details
#all_model_schemas ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rails_autodoc/schema_mapper.rb', line 74 def all_model_schemas return {} unless defined?(ActiveRecord::Base) schemas = {} @tables.each_key do |table| model_name = table.classify schema = model_schema(model_name) schemas[model_name] = schema if schema rescue StandardError next end schemas end |
#apply_types!(schema, model_name: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rails_autodoc/schema_mapper.rb', line 29 def apply_types!(schema, model_name: nil) return schema unless schema.is_a?(Hash) schema[:properties]&.each do |field, field_schema| typed = column_schema(model_name, field) if model_name schema[:properties][field] = merge_schemas(field_schema, typed) if typed apply_types!(schema[:properties][field], model_name: model_name) end schema end |
#infer_model_from_controller(controller_class) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/rails_autodoc/schema_mapper.rb', line 67 def infer_model_from_controller(controller_class) name = controller_class.name.split("::").last.sub(/Controller\z/, "").singularize name.constantize.name rescue StandardError nil end |
#model_schema(model_name) ⇒ Object
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 |
# File 'lib/rails_autodoc/schema_mapper.rb', line 41 def model_schema(model_name) return nil unless defined?(ActiveRecord::Base) model = model_name.constantize table = model.table_name columns = @tables[table] return nil unless columns properties = {} required = [] columns.each do |column_name, | next if %w[id created_at updated_at].include?(column_name) properties[column_name] = .dup required << column_name unless [:nullable] end { type: "object", properties: properties, required: required } rescue StandardError nil end |