Class: CodeStats

Inherits:
Object
  • Object
show all
Defined in:
lib/code_stats.rb,
lib/code_stats/tasks.rb,
lib/code_stats/printer.rb

Defined Under Namespace

Classes: Printer, Report, Stats, Tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CodeStats

Returns a new instance of CodeStats.


57
58
59
60
61
# File 'lib/code_stats.rb', line 57

def initialize(options)
  @directories = options[:directories] ? Array(options[:directories]) : raise('need to specify directory')
  @reporting_depth = options[:reporting_depth] || 0
  @file_extensions = Array(options[:file_extensions] || 'rb')
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories


55
56
57
# File 'lib/code_stats.rb', line 55

def directories
  @directories
end

#file_extensionsObject (readonly)

Returns the value of attribute file_extensions


55
56
57
# File 'lib/code_stats.rb', line 55

def file_extensions
  @file_extensions
end

#reporting_depthObject (readonly)

Returns the value of attribute reporting_depth


55
56
57
# File 'lib/code_stats.rb', line 55

def reporting_depth
  @reporting_depth
end

Instance Method Details

#runObject


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/code_stats.rb', line 63

def run
  reports = Hash.new{|h,k| h[k] = []}
  directories.each do |dir|
    Dir.glob(File.join(dir, '**', "*.{#{file_extensions * ','}}")) do |entry|
      relative_entry = entry[entry.index(dir) + dir.size, entry.size]
      relative_entry_parts = File.dirname(relative_entry).split(File::SEPARATOR).select{|f| not f.nil? || f.empty?}
      stats = Stats.new(entry)
      (0..reporting_depth).each do |depth|
        if relative_entry_parts.size >= depth
          reports[dir][depth] ||= {}
          reports[dir][depth][relative_entry_parts[0, depth]] ||= Report.new(relative_entry_parts[0, depth])
          reports[dir][depth][relative_entry_parts[0, depth]] << stats
        end
      end
    end
  end
  reports
end