Class: RailsLens::ModelSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/model_source.rb

Overview

Base class for model sources Model sources provide pluggable discovery of different model types (e.g., ActiveRecord, ActiveCypher, etc.)

Gems can register their own model source by defining:

GemName::RailsLensModelSource < RailsLens::ModelSource

Example:

module MyOrm
  class ModelSource < ::RailsLens::ModelSource
    def self.models(options = {})
      # Return array of model classes
    end

    def self.file_patterns
      ['app/my_models/**/*.rb']
    end

    def self.annotate_model(model, options = {})
      # Return { status: :annotated/:skipped/:failed, model: name, ... }
    end

    def self.remove_annotation(model)
      # Return { status: :removed/:skipped, model: name, ... }
    end
  end

  RailsLensModelSource = ModelSource
end

Class Method Summary collapse

Class Method Details

.annotate_model(_model, _options = {}) ⇒ Hash

Annotate a single model

Parameters:

  • model (Class)

    The model class to annotate

  • options (Hash)

    Options passed from CLI

Returns:

  • (Hash)

    Result with :status (:annotated, :skipped, :failed), :model, :file, :message

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/rails_lens/model_source.rb', line 54

def annotate_model(_model, _options = {})
  raise NotImplementedError, "#{name} must implement .annotate_model"
end

.file_patternsArray<String>

Return file patterns for annotation removal Used when removing annotations by filesystem scan

Returns:

  • (Array<String>)

    Glob patterns relative to Rails.root

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/rails_lens/model_source.rb', line 46

def file_patterns
  raise NotImplementedError, "#{name} must implement .file_patterns"
end

.models(_options = {}) ⇒ Array<Class>

Return array of model classes to annotate

Parameters:

  • options (Hash)

    Options passed from CLI

Returns:

  • (Array<Class>)

    Array of model classes

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/rails_lens/model_source.rb', line 39

def models(_options = {})
  raise NotImplementedError, "#{name} must implement .models"
end

.remove_annotation(_model) ⇒ Hash

Remove annotation from a single model

Parameters:

  • model (Class)

    The model class

Returns:

  • (Hash)

    Result with :status (:removed, :skipped), :model, :file

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/rails_lens/model_source.rb', line 61

def remove_annotation(_model)
  raise NotImplementedError, "#{name} must implement .remove_annotation"
end

.source_nameString

Human-readable name for this source (used in logging)

Returns:

  • (String)


67
68
69
# File 'lib/rails_lens/model_source.rb', line 67

def source_name
  name.demodulize.sub(/Source$/, '').underscore.humanize
end