Class: RubynCode::Learning::Shortcut

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/learning/shortcut.rb

Overview

Uses learned instincts to skip redundant discovery steps. For example, if we know the project uses FactoryBot, skip checking test_helper.rb and looking for factories/ — just generate FactoryBot-style specs.

Constant Summary collapse

SHORTCUT_RULES =
{
  'uses_factory_bot' => {
    skip: %w[test_helper factories_check],
    apply: { spec_template: :factory_bot_rspec }
  },
  'uses_rspec' => {
    skip: %w[framework_detection],
    apply: { test_framework: :rspec }
  },
  'uses_minitest' => {
    skip: %w[framework_detection],
    apply: { test_framework: :minitest }
  },
  'uses_service_objects' => {
    skip: %w[pattern_detection],
    apply: { service_pattern: 'app/services/**/*_service.rb' }
  },
  'uses_devise' => {
    skip: %w[auth_detection],
    apply: { auth_framework: :devise }
  },
  'uses_grape' => {
    skip: %w[api_detection],
    apply: { api_framework: :grape }
  },
  'uses_sidekiq' => {
    skip: %w[job_detection],
    apply: { job_framework: :sidekiq }
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShortcut

Returns a new instance of Shortcut.



42
43
44
45
# File 'lib/rubyn_code/learning/shortcut.rb', line 42

def initialize
  @applied_shortcuts = []
  @tokens_saved_estimate = 0
end

Instance Attribute Details

#applied_shortcutsObject (readonly)

Returns the value of attribute applied_shortcuts.



40
41
42
# File 'lib/rubyn_code/learning/shortcut.rb', line 40

def applied_shortcuts
  @applied_shortcuts
end

#tokens_saved_estimateObject (readonly)

Returns the value of attribute tokens_saved_estimate.



40
41
42
# File 'lib/rubyn_code/learning/shortcut.rb', line 40

def tokens_saved_estimate
  @tokens_saved_estimate
end

Instance Method Details

#apply(instinct_patterns) ⇒ Hash

Apply shortcuts based on instinct patterns.

Parameters:

  • instinct_patterns (Array<String>)

    patterns from the instincts table

Returns:

  • (Hash)

    aggregated settings from applied shortcuts



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubyn_code/learning/shortcut.rb', line 51

def apply(instinct_patterns)
  settings = {}

  instinct_patterns.each do |pattern|
    rule_key = match_rule(pattern)
    next unless rule_key

    rule = SHORTCUT_RULES[rule_key]
    settings.merge!(rule[:apply])
    @applied_shortcuts << { rule: rule_key, skipped: rule[:skip] }
    @tokens_saved_estimate += rule[:skip].size * 500
  end

  settings
end

#skip?(step_name) ⇒ Boolean

Check if a discovery step should be skipped.

Parameters:

  • step_name (String)

    the discovery step name

Returns:

  • (Boolean)


71
72
73
# File 'lib/rubyn_code/learning/shortcut.rb', line 71

def skip?(step_name)
  @applied_shortcuts.any? { |s| s[:skipped].include?(step_name.to_s) }
end

#statsObject

Returns stats about shortcuts applied this session.



76
77
78
79
80
81
82
# File 'lib/rubyn_code/learning/shortcut.rb', line 76

def stats
  {
    shortcuts_applied: @applied_shortcuts.size,
    steps_skipped: @applied_shortcuts.sum { |s| s[:skipped].size },
    tokens_saved_estimate: @tokens_saved_estimate
  }
end