Class: Woods::Extractors::PolicyExtractor
- Inherits:
-
Object
- Object
- Woods::Extractors::PolicyExtractor
- Includes:
- SharedDependencyScanner, SharedUtilityMethods
- Defined in:
- lib/woods/extractors/policy_extractor.rb
Overview
PolicyExtractor handles domain policy class extraction.
Policy classes encode business eligibility rules — “can this user upgrade?”, “is this order refundable?”. These are NOT Pundit authorization policies. They live in ‘app/policies/`.
We extract:
-
Policy name and namespace
-
Decision methods (allowed?, eligible?, valid?, etc.)
-
Models they evaluate (from initializer params and method bodies)
-
Dependencies (what models/services they reference)
Constant Summary collapse
- POLICY_DIRECTORIES =
Directories to scan for policy classes
%w[ app/policies ].freeze
- DECISION_METHOD_PATTERN =
Method name patterns that indicate decision/eligibility logic
/\b(allowed|eligible|valid|permitted|can_\w+|should_\w+|qualifies|meets_\w+|satisfies)\?/
Constants included from SharedDependencyScanner
SharedDependencyScanner::FORM_ACTION_HELPER, SharedDependencyScanner::ROUTE_HELPER_PATTERN
Instance Method Summary collapse
-
#extract_all ⇒ Array<ExtractedUnit>
Extract all policy classes.
-
#extract_policy_file(file_path) ⇒ ExtractedUnit?
Extract a single policy file.
-
#initialize ⇒ PolicyExtractor
constructor
A new instance of PolicyExtractor.
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 ⇒ PolicyExtractor
Returns a new instance of PolicyExtractor.
37 38 39 40 |
# File 'lib/woods/extractors/policy_extractor.rb', line 37 def initialize @directories = POLICY_DIRECTORIES.map { |d| Rails.root.join(d) } .select(&:directory?) end |
Instance Method Details
#extract_all ⇒ Array<ExtractedUnit>
Extract all policy classes
45 46 47 48 49 50 51 |
# File 'lib/woods/extractors/policy_extractor.rb', line 45 def extract_all @directories.flat_map do |dir| Dir[dir.join('**/*.rb')].filter_map do |file| extract_policy_file(file) end end end |
#extract_policy_file(file_path) ⇒ ExtractedUnit?
Extract a single policy file
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/woods/extractors/policy_extractor.rb', line 57 def extract_policy_file(file_path) source = File.read(file_path) class_name = extract_class_name(file_path, source, 'policies') return nil unless class_name return nil if skip_file?(source) unit = ExtractedUnit.new( type: :policy, 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, class_name) unit rescue StandardError => e Rails.logger.error("Failed to extract policy #{file_path}: #{e.}") nil end |