Module: RailsStats::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/rails_stats/util.rb

Instance Method Summary collapse

Instance Method Details

#calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee|feature)$/) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_stats/util.rb', line 78

def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee|feature)$/)
  stats = CodeStatisticsCalculator.new

  Dir.foreach(directory) do |file_name|
    path = "#{directory}/#{file_name}"

    if File.directory?(path) && (/^\./ !~ file_name)
      stats.add(calculate_directory_statistics(path, pattern))
    end

    next unless file_name =~ pattern

    stats.add_by_file_path(path)
  end

  stats
end

#calculate_file_statistics(file, type = :code, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_stats/util.rb', line 39

def calculate_file_statistics(file, type = :code, &block)
  stats = CodeStatisticsCalculator.new

  files = [file].flatten

  files.each do |path|
    stats.add_by_file_path(path)
  end

  stats
end

#calculate_projects(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_stats/util.rb', line 5

def calculate_projects(*args)
  projects = {}

  children = [args.pop].flatten
  parent   = args.flatten
  children.each do |dirname|
    child_folder_pattern = File.join(*(parent.dup + [dirname]))
    Dir[child_folder_pattern].each do |marker_path|
      next if marker_path =~ /\/vendor\//

      projects[File.absolute_path(File.dirname(marker_path))] = true
    end
  end

  out = projects.keys

  # TODO: make sure none are children of other ones in there
  out
end

#calculate_shared_projects(shared, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_stats/util.rb', line 25

def calculate_shared_projects(shared, *args)
  projects = {}
  calculate_projects(*args).each do |path|
    if path =~ /(^.*\/#{shared}\/).*/
      projects[$1] = true
    end
  end

  out = projects.keys

  # TODO: make sure none are children of other ones in there
  out
end

#calculate_statistics(directories, type = :code, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rails_stats/util.rb', line 51

def calculate_statistics(directories, type = :code, &block)
  out = {}

  directories = [directories].flatten
  is_test = (type.to_s == "test")

  directories.each do |dir_path|
    next unless File.directory?(dir_path)

    key = nil
    if block_given?
      key = yield(dir_path)
    end

    key = path_to_name(dir_path, is_test) if !key || key.length == 0

    out[key] ||= CodeStatisticsCalculator.new(is_test)
    out[key].add(calculate_directory_statistics(dir_path))
  end

  out.keys.each do |key|
    out.delete(key) if out[key].lines == 0
  end

  out
end

#path_to_name(path, is_test) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rails_stats/util.rb', line 96

def path_to_name(path, is_test)
  folder = File.basename(path)
  folder = Inflector.humanize(folder)
  folder = Inflector.titleize(folder)

  if is_test
    folder = Inflector.singularize(folder)
    folder = "#{folder} Tests"
  else
    folder = Inflector.pluralize(folder)
  end

  folder
end