class Esquema::Builder

The Builder class is responsible for building a schema for an ActiveRecord model.

def add_properties_from_associations

Adds properties from associations to the schema.
def add_properties_from_associations
  associations.each do |association|
    next if config.exclude_associations?
    @properties[association.name] ||= Property.new(association)
  end
end

def add_properties_from_columns

Adds properties from columns to the schema.
def add_properties_from_columns
  columns.each do |property|
    next if property.name.end_with?("_id") && config.exclude_foreign_keys?
    required_properties << property.name
    options = enhancement_for(property.name)
    @properties[property.name] ||= Property.new(property, options)
  end
end

def add_virtual_properties

Adds virtual properties to the schema.
def add_virtual_properties
  return unless schema_enhancements[:properties]
  virtual_properties = schema_enhancements[:properties].select { |_k, v| v[:virtual] }
  required_properties.concat(virtual_properties.keys)
  virtual_properties.each do |property_name, options|
    virtual_col = VirtualColumn.new(property_name, options)
    @properties[property_name] = Property.new(virtual_col, options)
  end
end

def associations

Returns:
  • (Array) - The associations of the model.
def associations
  return [] unless model.respond_to?(:reflect_on_all_associations)
  model.reflect_on_all_associations
end

def build_description

Returns:
  • (String) - The built description.
def build_description
  schema_enhancements[:model_description].presence
end

def build_properties

Returns:
  • (Hash) - The built properties.
def build_properties
  add_properties_from_columns
  add_properties_from_associations
  add_virtual_properties
  @properties
end

def build_schema

Returns:
  • (Hash) - The built schema.
def build_schema
  @build_schema ||= {
    title: build_title,
    description: build_description,
    type: build_type,
    properties: build_properties,
    required: required_properties
  }.compact
end

def build_title

Returns:
  • (String) - The built title.
def build_title
  schema_enhancements[:model_title].presence || model.name.demodulize.humanize
end

def build_type

Returns:
  • (String) - The built type.
def build_type
  model.respond_to?(:type) ? model.type : "object"
end

def columns

Returns:
  • (Array) - The columns of the model.
def columns
  model.columns.reject { |c| excluded_column?(c.name) }
end

def config

Returns:
  • (Esquema::Configuration) - The Esquema configuration.
def config
  Esquema.configuration
end

def enhancement_for(property_name)

Returns:
  • (Hash) - The enhancement options for the property.

Parameters:
  • property_name (Symbol) -- The name of the property.
def enhancement_for(property_name)
  schema_enhancements.dig(:properties, property_name.to_sym) || {}
end

def excluded_column?(column_name)

Returns:
  • (Boolean) - True if the column is excluded, false otherwise.

Parameters:
  • column_name (String) -- The name of the column.
def excluded_column?(column_name)
  raise ArgumentError, "Column name must be a string" unless column_name.is_a? String
  config.excluded_columns.include?(column_name.to_sym)
end

def initialize(model)

def initialize(model)
  raise ArgumentError, "Class is not an ActiveRecord model" unless model.ancestors.include? ActiveRecord::Base
  @model = model
  @properties = {}
  @required_properties = []
end

def schema

Returns:
  • (Hash) - The schema for the ActiveRecord model.
def schema
  build_schema
end

def schema_enhancements

Returns:
  • (Hash) - The schema enhancements.
def schema_enhancements
  if model.respond_to?(:schema_enhancements)
    model.schema_enhancements
  else
    {}
  end
end