Class: PPStats::Modules

Inherits:
Object
  • Object
show all
Defined in:
lib/pp-stats/modules.rb

Defined Under Namespace

Classes: ModuleType, ModuleTypes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Modules

Returns a new instance of Modules.



8
9
10
11
12
13
14
15
16
# File 'lib/pp-stats/modules.rb', line 8

def initialize(options)
  @options = options
  @types = ModuleTypes.instance.types
  @procssed_file = false
  @previous_line = nil
  @module_count = 0
  @classes = 0
  @defines = 0
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



6
7
8
# File 'lib/pp-stats/modules.rb', line 6

def classes
  @classes
end

#definesObject (readonly)

Returns the value of attribute defines.



6
7
8
# File 'lib/pp-stats/modules.rb', line 6

def defines
  @defines
end

#module_countObject (readonly)

Returns the value of attribute module_count.



6
7
8
# File 'lib/pp-stats/modules.rb', line 6

def module_count
  @module_count
end

#typesObject (readonly)

Returns the value of attribute types.



6
7
8
# File 'lib/pp-stats/modules.rb', line 6

def types
  @types
end

Instance Method Details

#filter_file(file, type) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pp-stats/modules.rb', line 60

def filter_file(file, type)
  ext = File.extname(file)
  if type.file_exts == '*'
    if ext =~ /\.\d/
      return false
    elsif type.exclude_exts.include? ext
      return false
    else
      return true
    end
  end
  type.file_exts.include? ext
end

#process_file(path, index, totfiles) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pp-stats/modules.rb', line 74

def process_file(path, index, totfiles)
  @procssed_file = true
  file = Pathname.new(path).basename
  line = "\r    #{file} (#{index}/#{totfiles})"
  STDOUT.write "\r" + " "*@previous_line.length if !@previous_line.nil?
  @previous_line = line
  STDOUT.write line
  STDOUT.flush
  lines = File.new(path).readlines
  commented_lines = lines.select { |line| line =~ /^#/ }
  c = lines.select { |line| line =~ /^class/ }
  d = lines.select { |line| line =~ /^define/ }
  sleep(0.05)
  @classes += 1 if ! c.empty?
  @defines += 1 if ! d.empty?
  return lines.count, commented_lines.count
end

#process_module(moddir) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pp-stats/modules.rb', line 35

def process_module(moddir)
  puts PPStats::Format.green "module #{moddir}"
  @types.each do |k,v|
    puts PPStats::Format.blue "  #{k.to_s}"
    process_type(moddir, v)
    puts if @procssed_file
  end
  @module_count += 1
end

#process_type(moddir, type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pp-stats/modules.rb', line 45

def process_type(moddir, type)
  @procssed_file = false
  return if !File.directory? moddir

  type.dirs.each do |dir|
    files = PPStats::Utils.files_recursive_nodirs(File.join(moddir, dir))
    files.each_with_index do |file, index|
      if filter_file(file, type)
        count, commented_count = process_file(file, index+1, files.length)
        type.counter.adjust(file, count, commented_count)
      end
    end
  end
end

#resultsObject



30
31
32
33
# File 'lib/pp-stats/modules.rb', line 30

def results
  summary = PPStats::Summary.new(@types, @module_count, @classes, @defines)
  summary.run
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pp-stats/modules.rb', line 18

def run
  if @options[:single_module]
    process_module(@options[:modulepath])
  else
    PPStats::Utils.files_dotless(@options[:modulepath]).each do |file|
      file = File.join(@options[:modulepath], file)
      process_module file if File.directory? file
    end
  end

end