Fixes recursion bug in disambiguate_in().
[ohcount] / ruby / gestalt / rules / keyword_rule.rb
1 module Ohcount
2         module Gestalt
3                 # Will trigger if the given keywords are found in the specified language.
4                 # Example;
5                 #
6                 #
7                 # c_keywords '__MSDOS__', 'MSDOS', :min => 2 
8                 #       # this will trigger if the      words "__MSDOS__" or "MSDOS" is found at
9                 #       # least twice in 'C' source code
10                 #
11                 class KeywordRule < FileRule
12                         attr_reader :keywords, :language
13
14                         def initialize(language, *args)
15                                 options = args.pop if args.last.is_a?(Hash)
16                                 keywords = args
17                                 @language = language
18                                 @keywords = keywords
19                                 super(options)
20                         end
21
22                         def process_source_file(source_file)
23                                 if language
24                                         return unless source_file.language_breakdown(language)
25                                         @count += source_file.language_breakdown(language).code.scan(regexp).size
26                                 else
27                                         # All languages
28                                         source_file.language_breakdowns.each do |lb|
29                                                 @count += lb.code.to_s.scan(regexp).size
30                                         end
31                                 end
32                         end
33
34                         def regexp
35                                 @regexp ||= begin
36                                         Regexp.new("(" + keywords.join("|") + ")")
37                                 end
38                         end
39                 end
40         end
41 end