[NEW] Gestalts for silverlight + popular JS libs
[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                                 return unless source_file.language_breakdown(language)
24
25         code = source_file.language_breakdown(language).code
26                                 @count += code.scan(regexp).size
27                         end
28
29                         def regexp
30                                 @regexp ||= begin
31                                         Regexp.new("(" + keywords.join("|") + ")")
32                                 end
33                         end
34                 end
35         end
36 end