Class: Woods::Extractors::ConcernExtractor
- Inherits:
-
Object
- Object
- Woods::Extractors::ConcernExtractor
- Includes:
- SharedDependencyScanner, SharedUtilityMethods
- Defined in:
- lib/woods/extractors/concern_extractor.rb
Overview
ConcernExtractor handles ActiveSupport::Concern module extraction.
Concerns are mixins that extend model and controller behavior. They live in ‘app/models/concerns/` and `app/controllers/concerns/`, as well as nested directories like `app/models/gateway/stripe/concerns/`.
We extract:
-
Module name and namespace
-
Included/extended hooks and class methods block
-
Instance methods and class methods added by the concern
-
Dependencies on models and other concerns
Constant Summary collapse
- CONCERN_DIRECTORIES =
Canonical concern directories (used as fallback if glob finds nothing).
%w[ app/models/concerns app/controllers/concerns ].freeze
Constants included from SharedDependencyScanner
SharedDependencyScanner::FORM_ACTION_HELPER, SharedDependencyScanner::ROUTE_HELPER_PATTERN
Instance Method Summary collapse
-
#extract_all ⇒ Array<ExtractedUnit>
Extract all concern modules.
-
#extract_concern_file(file_path) ⇒ ExtractedUnit?
Extract a single concern file.
-
#initialize ⇒ ConcernExtractor
constructor
A new instance of ConcernExtractor.
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
#initialize ⇒ ConcernExtractor
Returns a new instance of ConcernExtractor.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/woods/extractors/concern_extractor.rb', line 35 def initialize # Discover all concerns/ directories under app/, including deeply nested ones # like app/models/gateway/stripe/webhook/concerns/. @directories = Dir[Rails.root.join('app/**/concerns')].map { |d| Pathname.new(d) } .select(&:directory?) # Fall back to canonical directories if glob finds nothing. return unless @directories.empty? @directories = CONCERN_DIRECTORIES.map { |d| Rails.root.join(d) } .select(&:directory?) end |
Instance Method Details
#extract_all ⇒ Array<ExtractedUnit>
Extract all concern modules
50 51 52 53 54 55 56 |
# File 'lib/woods/extractors/concern_extractor.rb', line 50 def extract_all @directories.flat_map do |dir| Dir[dir.join('**/*.rb')].filter_map do |file| extract_concern_file(file) end end end |
#extract_concern_file(file_path) ⇒ ExtractedUnit?
Extract a single concern file
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/woods/extractors/concern_extractor.rb', line 62 def extract_concern_file(file_path) source = File.read(file_path) module_name = extract_module_name(file_path, source) return nil unless module_name return nil unless concern_module?(source) unit = ExtractedUnit.new( type: :concern, identifier: module_name, file_path: file_path ) unit.namespace = extract_namespace(module_name) unit.source_code = annotate_source(source, module_name) unit. = (source, file_path) unit.dependencies = extract_dependencies(source) unit rescue StandardError => e Rails.logger.error("Failed to extract concern #{file_path}: #{e.}") nil end |