[NEW] Gestalts for silverlight + popular JS libs
[ohcount] / ruby / gestalt / rules / c_header_rule.rb
1 module Ohcount
2         module Gestalt
3                 # Triggers if a c or cpp header is present
4                 class CHeaderRule < FileRule
5                         attr_reader :headers
6
7                         def initialize(*args)
8                                 options = args.pop if args.last.is_a?(Hash)
9                                 @headers = args
10                                 super(options)
11                         end
12
13       def process_source_file(source_file)
14                                 return unless ['c','cpp'].include?(source_file.polyglot)
15
16         ['c','cpp'].each do |lang|
17           next unless source_file.language_breakdown(lang)
18           md = regexp.match(source_file.language_breakdown(lang).code)
19           @count += (md && md.size).to_i
20         end
21       end
22
23                         def regexp
24                                 @regexp ||= begin
25                                         header_term = "(" + headers.join("|") + ")"
26                                         Regexp.new("include\s+['<\"]#{ header_term }[\">']", Regexp::IGNORECASE)
27                                 end
28                         end
29                 end
30         end
31 end