Class: Woods::Extractors::CachingExtractor

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

Overview

CachingExtractor detects caching usage across controllers, models, and views.

Scans ‘app/controllers/*/.rb`, `app/models/*/.rb`, and `app/views/*/.erb` for cache-related patterns: Rails.cache.*, caches_action, fragment cache blocks, cache_key, cache_version, and expires_in. Produces one unit per file that contains any cache calls, identifying the strategy and TTL patterns.

Examples:

extractor = CachingExtractor.new
units = extractor.extract_all
ctrl = units.find { |u| u.identifier == "app/controllers/products_controller.rb" }
ctrl.[:cache_strategy]  # => :low_level
ctrl.[:cache_calls].size # => 3

Constant Summary collapse

SCAN_PATTERNS =

File glob patterns to scan

{
  controller: 'app/controllers/**/*.rb',
  model: 'app/models/**/*.rb',
  view: 'app/views/**/*.erb'
}.freeze
CACHE_PATTERNS =

Patterns that indicate cache usage, grouped by type

{
  fetch: /Rails\.cache\.fetch\s*[(\[]/,
  read: /Rails\.cache\.read\s*[(\[]/,
  write: /Rails\.cache\.write\s*[(\[]/,
  delete: /Rails\.cache\.delete\s*[(\[]/,
  exist: /Rails\.cache\.exist\?\s*[(\[]/,
  caches_action: /\bcaches_action\b/,
  fragment: /\bcache\s+.*?\bdo\b|\bcache\s+do\b|\bcache\s*\(/,
  cache_key: /\bcache_key\b/,
  cache_version: /\bcache_version\b/
}.freeze
TTL_PATTERN =

Patterns for extracting TTL values

/expires_in:\s*([^,\n)]+)/
KEY_PATTERN =

Key-pattern regex (first argument to Rails.cache.*)

/Rails\.cache\.(?:fetch|read|write|delete|exist\?)\s*[(\[]?\s*([^,\n)\]]+)/

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

#initializeCachingExtractor

Returns a new instance of CachingExtractor.



53
54
55
# File 'lib/woods/extractors/caching_extractor.rb', line 53

def initialize
  @rails_root = Rails.root
end

Instance Method Details

#extract_allArray<ExtractedUnit>

Extract caching units from all scanned files.

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/woods/extractors/caching_extractor.rb', line 60

def extract_all
  units = []

  SCAN_PATTERNS.each do |file_type, pattern|
    Dir[@rails_root.join(pattern)].each do |file|
      unit = extract_caching_file(file, file_type)
      units << unit if unit
    end
  end

  units
end

#extract_caching_file(file_path, file_type = nil) ⇒ ExtractedUnit?

Extract a single file for caching patterns.

Returns nil if the file contains no cache calls.

Parameters:

  • file_path (String)

    Absolute path to the file

  • file_type (Symbol) (defaults to: nil)

    :controller, :model, or :view

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/woods/extractors/caching_extractor.rb', line 80

def extract_caching_file(file_path, file_type = nil)
  source = File.read(file_path)

  return nil unless cache_usage?(source)

  file_type ||= infer_file_type(file_path)
  identifier = relative_path(file_path)

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

  unit.namespace   = nil
  unit.source_code = annotate_source(source, identifier, file_type)
  unit.    = (source, file_type)
  unit.dependencies = extract_dependencies(source)

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