Class: Keela::Strategies::Delegations

Inherits:
Keela::Strategy show all
Defined in:
lib/keela/strategies/delegations.rb

Instance Method Summary collapse

Methods inherited from Keela::Strategy

#extract_definitions_from_file

Instance Method Details

#definition_file_patternObject



10
11
12
13
# File 'lib/keela/strategies/delegations.rb', line 10

def definition_file_pattern
  # Match app/models/ directories (including concerns), but exclude spec/test
  %r{(?:^|/)(?:ee/)?app/models/}
end

#extract_definition(line) ⇒ Object



15
16
17
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
# File 'lib/keela/strategies/delegations.rb', line 15

def extract_definition(line)
  # Match delegate declarations like:
  #   delegate :name, to: :user
  #   delegate :name, :email, to: :user
  #   delegate :name, to: :user, prefix: true
  #   delegate :name, to: :user, prefix: :owner
  #   delegate :name, to: :user, allow_nil: true
  return nil unless line =~ /^\s*delegate\s+/

  # Extract the target for prefix detection
  target = line[/to:\s*:[@]?(\w+)/, 1]

  # Check for prefix option
  prefix = if line =~ /prefix:\s*:(\w+)/
             Regexp.last_match(1)
           elsif line =~ /prefix:\s*true/
             target
           end

  # Extract all method symbols from the delegate call
  # Match :symbol patterns before 'to:'
  # Include ? and ! for predicate and bang methods
  delegate_part = line.split(/,\s*to:/)[0]
  methods = delegate_part.scan(/:(\w+[?!]?)/).flatten

  return nil if methods.empty?

  # Apply prefix if present
  if prefix
    methods = methods.map { |m| "#{prefix}_#{m}" }
  end

  # Return single string for single method (scanner expects this)
  # For multiple methods, return first one only
  # The scanner will create one definition entry per extract_definition call
  # To handle multiple delegations per line, we'd need to change the scanner
  # For now, return just the first method
  methods.first
end

#nameObject



6
7
8
# File 'lib/keela/strategies/delegations.rb', line 6

def name
  "delegations"
end

#skip_comments?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/keela/strategies/delegations.rb', line 65

def skip_comments?
  true
end

#usage_regex(name) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/keela/strategies/delegations.rb', line 55

def usage_regex(name)
  # Match usage of the delegated method, but not the delegate declaration
  # Uses negative lookbehind to avoid matching:
  #   - Symbol notation (:name)
  #   - Part of delegate declaration
  # Uses word boundary to avoid partial matches
  # Note: Regexp.quote handles ? and ! in method names
  /(?<!:)(?<!delegate\s)(?<![a-z_])#{Regexp.quote(name)}(?!\w)/i
end