Class: Keela::Strategies::Constants
- Inherits:
-
Keela::Strategy
- Object
- Keela::Strategy
- Keela::Strategies::Constants
- Defined in:
- lib/keela/strategies/constants.rb
Instance Method Summary collapse
- #definition_file_pattern ⇒ Object
- #extract_definition(line) ⇒ Object
- #name ⇒ Object
- #skip_comments? ⇒ Boolean
- #usage_regex(name) ⇒ Object
Methods inherited from Keela::Strategy
#extract_definitions_from_file
Instance Method Details
#definition_file_pattern ⇒ Object
10 11 12 13 |
# File 'lib/keela/strategies/constants.rb', line 10 def definition_file_pattern # Match app/ and lib/ directories, but exclude spec/ and test/ %r{(?:^|/)(?:ee/)?(?:app|lib)/} 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 |
# File 'lib/keela/strategies/constants.rb', line 15 def extract_definition(line) # Match constant definitions like: # MAX_SIZE = 100 # ALLOWED_TYPES = %w[foo bar].freeze # OPTIONS = { foo: 1 }.freeze # # Must start with uppercase letter followed by uppercase letters, # digits, or underscores, then = (with optional whitespace) # # Avoid matching: # - Comparisons: MAX_SIZE == 100 # - Namespaced access: Foo::BAR # - Class/module definitions # First check it's not a comparison return nil if line =~ /[!=]=/ # Match the constant definition pattern return nil unless line =~ /^\s*([A-Z][A-Z0-9_]*)\s*=/ Regexp.last_match(1) end |
#name ⇒ Object
6 7 8 |
# File 'lib/keela/strategies/constants.rb', line 6 def name "constants" end |
#skip_comments? ⇒ Boolean
48 49 50 |
# File 'lib/keela/strategies/constants.rb', line 48 def skip_comments? true end |
#usage_regex(name) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/keela/strategies/constants.rb', line 38 def usage_regex(name) # Match usage of the constant, but not its definition # Uses negative lookbehind to avoid matching when preceded by # uppercase letters/digits/underscores (partial match) # Uses negative lookahead to avoid: # - partial matches (followed by uppercase letters/digits/underscores) # - definitions (followed by optional whitespace then =, but not ==) /(?<![A-Z0-9_])#{Regexp.quote(name)}(?![A-Z0-9_])(?!\s*=(?!=))/ end |