Class: RSpecLetAnalyzer::Analyzer
- Inherits:
-
Object
- Object
- RSpecLetAnalyzer::Analyzer
- Defined in:
- lib/rspec_let_analyzer/analyzer.rb
Instance Attribute Summary collapse
-
#factory_stats ⇒ Object
readonly
Returns the value of attribute factory_stats.
-
#nesting_depth ⇒ Object
readonly
Returns the value of attribute nesting_depth.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#totals ⇒ Object
readonly
Returns the value of attribute totals.
Instance Method Summary collapse
- #analyze(path = nil) ⇒ Object
-
#initialize(progress_reporter:, nesting_depth:, track_factories:, parser_type: :prism) ⇒ Analyzer
constructor
A new instance of Analyzer.
- #top(limit, sort_by) ⇒ Object
- #top_factories(limit) ⇒ Object
Constructor Details
#initialize(progress_reporter:, nesting_depth:, track_factories:, parser_type: :prism) ⇒ Analyzer
Returns a new instance of Analyzer.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 7 def initialize(progress_reporter:, nesting_depth:, track_factories:, parser_type: :prism) @results = [] @nesting_depth = nesting_depth @track_factories = track_factories @totals = build_totals_hash @factory_stats = Hash.new(0) if track_factories @progress_reporter = progress_reporter @adapter = Adapter.create(parser_type) @parser_type = parser_type end |
Instance Attribute Details
#factory_stats ⇒ Object (readonly)
Returns the value of attribute factory_stats.
5 6 7 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 5 def factory_stats @factory_stats end |
#nesting_depth ⇒ Object (readonly)
Returns the value of attribute nesting_depth.
5 6 7 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 5 def nesting_depth @nesting_depth end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
5 6 7 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 5 def results @results end |
#totals ⇒ Object (readonly)
Returns the value of attribute totals.
5 6 7 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 5 def totals @totals end |
Instance Method Details
#analyze(path = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 18 def analyze(path = nil) # Build pattern based on path # If path looks like a glob pattern (contains * or ends with .rb), use it directly # Otherwise treat it as a directory and append spec/**/*_spec.rb pattern = if path.nil? 'spec/**/*_spec.rb' elsif path.include?('*') || path.end_with?('.rb') # It's a glob pattern or specific file, use as-is path else # It's a directory, append spec pattern File.join(path, 'spec/**/*_spec.rb') end spec_files = Dir.glob(pattern) total_files = spec_files.size @results = spec_files.each_with_index.map do |file, index| @progress_reporter&.update(current: index + 1, total: total_files, file: file) ast = @adapter.parse_file(file) visitor = create_visitor @adapter.visit_tree(ast, visitor) # Aggregate factory usage if @track_factories && visitor.factory_usage visitor.factory_usage.each do |factory, count| @factory_stats[factory] += count end end file_result = { file: file, root_lets: visitor.root_lets, it_blocks: visitor.total_its, redefinitions: visitor.redefinitions, before_creates: visitor.before_creates } if @nesting_depth visitor.nesting_counts.each_with_index do |count, i| file_result[:"nesting_#{i + 1}"] = count end end # Calculate total score file_result[:total_score] = calculate_file_total_score(file_result, visitor) file_result end @progress_reporter&.clear calculate_totals end |
#top(limit, sort_by) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 73 def top(limit, sort_by) sort_key = case sort_by when 'root' :root_lets when 'it' :it_blocks when 'redef' :redefinitions when 'before' :before_creates when 'total' :total_score end @results.sort_by { |r| -r[sort_key] }.take(limit) end |
#top_factories(limit) ⇒ Object
90 91 92 93 94 |
# File 'lib/rspec_let_analyzer/analyzer.rb', line 90 def top_factories(limit) return nil unless @track_factories @factory_stats.sort_by { |_factory, count| -count }.take(limit) end |