Class: RailsAiBridge::Introspectors::ModelIntrospector::SourceMacroExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/introspectors/model_introspector/source_macro_extractor.rb

Overview

Extracts source-level macros from an ActiveRecord model's source file.

Scans the model source for Rails macros such as +has_secure_password+, +encrypts+, +normalizes+, Active Storage attachments, Action Text, Turbo broadcasts, token generators, serialization, store accessors, and delegations.

Constant Summary collapse

PATTERNS =
{
  has_secure_password: /\bhas_secure_password\b/,
  encrypts: /\bencrypts\s+:/,
  normalizes: /\bnormalizes\s+:/,
  has_one_attached: /\bhas_one_attached\s+:(\w+)/,
  has_many_attached: /\bhas_many_attached\s+:(\w+)/,
  has_rich_text: /\bhas_rich_text\s+:(\w+)/,
  broadcasts: /\bbroadcasts/,
  generates_token_for: /\bgenerates_token_for\s+:(\w+)/,
  serialize: /\bserialize\s+:(\w+)/,
  store: /\bstore(?:_accessor)?\s+:(\w+)/,
  delegations: /\bdelegate\s+(.+?),\s*to:\s*:(\w+)/,
  delegate_missing_to: /\bdelegate_missing_to\s+:(\w+)/,
  broadcasts_variants: /\b(broadcasts_to|broadcasts_refreshes_to|broadcasts)\b/
}.freeze
SYMBOL_ARG_PREFIXES =
{
  encrypts: /\bencrypts\s+/,
  normalizes: /\bnormalizes\s+/
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ SourceMacroExtractor

Returns a new instance of SourceMacroExtractor.

Parameters:

  • source (String)

    the Ruby source code of the model file



35
36
37
# File 'lib/rails_ai_bridge/introspectors/model_introspector/source_macro_extractor.rb', line 35

def initialize(source)
  @source = source
end

Instance Method Details

#callHash

Returns detected macros (empty arrays removed).

Returns:

  • (Hash)

    detected macros (empty arrays removed)



40
41
42
43
44
# File 'lib/rails_ai_bridge/introspectors/model_introspector/source_macro_extractor.rb', line 40

def call
  build_macros
rescue StandardError
  {}
end