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

#definition_file_pattern, #extract_definitions_from_file

Instance Method Details

#default_definition_file_patternObject



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

def default_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
# 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 all methods (scanner handles both single string and array)
  methods.length == 1 ? methods.first : methods
end

#nameObject



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

def name
  "delegations"
end

#skip_comments?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/keela/strategies/delegations.rb', line 61

def skip_comments?
  true
end

#usage_regex(name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/keela/strategies/delegations.rb', line 51

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