[FIX] fix SourceFileList.analyze to accept an argument; my mistake in not specifying...
[ohcount] / ruby / ohcount.rb
1 # ohcount.rb written by Mitchell Foral. mitchell<att>caladbolg.net.
2 # See COPYING for license information.
3 # Ohcount module tweaked for use by Ohloh.
4
5 $: << File.expand_path(File.dirname(__FILE__))
6 require 'ohcount.so'
7
8 module Ohcount
9   class SourceFile
10     def file_location=(value) set_diskpath(value) end
11     def file_location() diskpath() end
12     def filenames=(value) set_filenames(value) end
13     def contents() get_contents() end
14     def polyglot() get_language() end
15
16     def language_breakdowns
17       list = get_parsed_language_list()
18       return array_from_list(list, :pl)
19     end
20
21     def language_breakdown(language)
22       return language_breakdowns().find { |lb| lb.name == language.to_s }
23     end
24
25     def licenses
26       list = get_license_list()
27       array = array_from_list(list, :lic)
28       return array.map! { |l| l.name }
29     end
30
31     def languages
32       return language_breakdowns().collect { |lb| lb.name }
33     end
34
35     def loc_list
36       list = get_loc_list()
37       return array_from_list(list, :loc)
38     end
39
40     def diff(to)
41       list = _diff(to)
42       ret = array_from_list(list, :delta)
43       class << ret
44         def loc_deltas() self end
45       end
46       return ret
47     end
48
49     def each
50       filenames.each { |f| yield f }
51     end
52
53     private
54
55     def array_from_list(list, method)
56       array = Array.new
57       iter = list.head
58       while (iter)
59         array << iter.send(method)
60         iter = iter.next
61       end
62       return array
63     end
64   end
65
66   class SourceFileList
67     def each_source_file
68       iter = self.head
69       while (iter)
70         yield iter.sf if iter.sf.polyglot
71         iter = iter.next
72       end
73     end
74
75                 # this should yield each filename, not an sf object
76                 def each
77       iter = self.head
78       while (iter)
79         yield iter.sf
80         iter = iter.next
81       end
82                 end
83
84     def size
85       count = 0
86       iter = self.head
87       while (iter)
88         count += 1
89         iter = iter.next
90       end
91       return count
92     end
93   end
94
95   class Detector
96     def self.binary_filename?(filename)
97       return Ohcount.ohcount_is_binary_filename(filename) == 1
98     end
99   end
100 end