Class: RailsApiDocs::Inspectors::SchemaInspector
- Inherits:
-
Object
- Object
- RailsApiDocs::Inspectors::SchemaInspector
- Defined in:
- lib/rails-api-docs/inspectors/schema_inspector.rb
Overview
Looks up the ActiveRecord model corresponding to a controller and returns its columns metadata:
{ "name" => { type: :string, null: false, default: nil }, ... }
If the model can’t be resolved or ActiveRecord isn’t loaded, returns an empty hash — schema inference is always best-effort.
Constant Summary collapse
- DEFAULT_RESOLVER =
lambda do |controller| last = controller.split("/").last.to_s model_name = last.singularize.camelize Object.const_get(model_name) if Object.const_defined?(model_name) end
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(controller:, model_resolver: nil) ⇒ SchemaInspector
constructor
A new instance of SchemaInspector.
Constructor Details
#initialize(controller:, model_resolver: nil) ⇒ SchemaInspector
Returns a new instance of SchemaInspector.
19 20 21 22 |
# File 'lib/rails-api-docs/inspectors/schema_inspector.rb', line 19 def initialize(controller:, model_resolver: nil) @controller = controller @model_resolver = model_resolver || DEFAULT_RESOLVER end |
Instance Method Details
#call ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rails-api-docs/inspectors/schema_inspector.rb', line 24 def call model = @model_resolver.call(@controller) return {} unless model && model.respond_to?(:columns_hash) model.columns_hash.each_with_object({}) do |(name, col), acc| acc[name.to_s] = { type: col.type, null: col.null, default: col.default } end rescue StandardError {} end |