Class: Woods::Extractors::ValidatorExtractor

Inherits:
Object
  • Object
show all
Includes:
SharedDependencyScanner, SharedUtilityMethods
Defined in:
lib/woods/extractors/validator_extractor.rb

Overview

ValidatorExtractor handles custom validator class extraction.

Custom validators encapsulate reusable validation logic that applies across multiple models. They inherit from ‘ActiveModel::Validator` or `ActiveModel::EachValidator` and live in `app/validators/`.

We extract:

  • Validator name and namespace

  • Base class (Validator vs EachValidator)

  • Validation rules (what they check)

  • Models they operate on (from source references)

  • Dependencies (what models/services they reference)

Examples:

extractor = ValidatorExtractor.new
units = extractor.extract_all
email = units.find { |u| u.identifier == "EmailFormatValidator" }

Constant Summary collapse

VALIDATOR_DIRECTORIES =

Directories to scan for custom validators

%w[
  app/validators
].freeze

Constants included from SharedDependencyScanner

SharedDependencyScanner::FORM_ACTION_HELPER, SharedDependencyScanner::ROUTE_HELPER_PATTERN

Instance Method Summary collapse

Methods included from SharedDependencyScanner

#extract_constantize_targets, #scan_common_dependencies, #scan_form_dependencies, #scan_job_dependencies, #scan_mailer_dependencies, #scan_model_dependencies, #scan_navigation_dependencies, #scan_service_dependencies

Methods included from SharedUtilityMethods

#app_source?, #condition_label, #count_loc, #detect_entry_points, #extract_action_filter_actions, #extract_callback_conditions, #extract_class_methods, #extract_class_name, #extract_custom_errors, #extract_initialize_params, #extract_namespace, #extract_parent_class, #extract_public_methods, #resolve_source_location, #skip_file?

Constructor Details

#initializeValidatorExtractor

Returns a new instance of ValidatorExtractor.



35
36
37
38
# File 'lib/woods/extractors/validator_extractor.rb', line 35

def initialize
  @directories = VALIDATOR_DIRECTORIES.map { |d| Rails.root.join(d) }
                                      .select(&:directory?)
end

Instance Method Details

#extract_allArray<ExtractedUnit>

Extract all custom validators

Returns:



43
44
45
46
47
48
49
# File 'lib/woods/extractors/validator_extractor.rb', line 43

def extract_all
  @directories.flat_map do |dir|
    Dir[dir.join('**/*.rb')].filter_map do |file|
      extract_validator_file(file)
    end
  end
end

#extract_validator_file(file_path) ⇒ ExtractedUnit?

Extract a single validator file

Parameters:

  • file_path (String)

    Path to the validator file

Returns:

  • (ExtractedUnit, nil)

    The extracted unit or nil if not a validator



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/woods/extractors/validator_extractor.rb', line 55

def extract_validator_file(file_path)
  source = File.read(file_path)
  class_name = extract_class_name(file_path, source, 'validators')

  return nil unless class_name
  return nil unless validator_file?(source)

  unit = ExtractedUnit.new(
    type: :validator,
    identifier: class_name,
    file_path: file_path
  )

  unit.namespace = extract_namespace(class_name)
  unit.source_code = annotate_source(source, class_name)
  unit. = (source, class_name)
  unit.dependencies = extract_dependencies(source)

  unit
rescue StandardError => e
  Rails.logger.error("Failed to extract validator #{file_path}: #{e.message}")
  nil
end