Class: JsDuck::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/stats.rb

Overview

Calculates all kinds of statistics for classes

Instance Method Summary collapse

Instance Method Details

#create(classes) ⇒ Object

Maps array of classes into array of stats per class



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jsduck/stats.rb', line 7

def create(classes)
  @classes = classes

  classes.map do |cls|
    local_members = cls.all_local_members
    total_members = cls.all_members
    class_wc = wc(cls[:doc])
    members_wc = members_wc(cls)

    {
      :name => cls[:name],

      :local_cfgs => member_count(local_members, :cfg),
      :local_properties => member_count(local_members, :property),
      :local_methods => member_count(local_members, :method),
      :local_events => member_count(local_members, :event),
      :local_members => local_members.length,

      :total_cfgs => member_count(total_members, :cfg),
      :total_properties => member_count(total_members, :property),
      :total_methods => member_count(total_members, :method),
      :total_events => member_count(total_members, :event),
      :total_members => total_members.length,

      :fanIn => fan_in(cls),
      :fanOut => fan_out(cls),

      :class_wc => class_wc,
      :members_wc => members_wc,
      :wc_per_member => local_members.length > 0 ? (members_wc / local_members.length) : 0,
    }
  end
end

#dependencies(cls) ⇒ Object

list of class names the class depends on



56
57
58
59
60
61
62
63
# File 'lib/jsduck/stats.rb', line 56

def dependencies(cls)
  [
    cls[:extends],
    cls[:mixins],
    cls[:requires],
    cls[:uses],
  ].compact.flatten.sort.uniq
end

#fan_in(cls) ⇒ Object

How many classes depend on this class



46
47
48
# File 'lib/jsduck/stats.rb', line 46

def fan_in(cls)
  fan_in_table[cls[:name]] || 0
end

#fan_in_tableObject

Returns map of class names to its fan-in number.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jsduck/stats.rb', line 66

def fan_in_table
  return @fi_table if @fi_table

  @fi_table = {}
  @classes.each do |cls|
    dependencies(cls).each do |d|
      @fi_table[d] = (@fi_table[d] || 0) + 1
    end
  end
  @fi_table
end

#fan_out(cls) ⇒ Object

On how many classes this class depends on



51
52
53
# File 'lib/jsduck/stats.rb', line 51

def fan_out(cls)
  dependencies(cls).length
end

#member_count(members, type) ⇒ Object



41
42
43
# File 'lib/jsduck/stats.rb', line 41

def member_count(members, type)
  members.find_all {|m| m[:tagname] == type }.length
end

#members_wc(cls) ⇒ Object

Counts nr of words in documentation of all members of class



79
80
81
82
83
84
85
86
87
88
# File 'lib/jsduck/stats.rb', line 79

def members_wc(cls)
  cnt = 0
  cls.all_local_members.each do |m|
    cnt += wc(m[:doc])
    (m[:params] || []).each {|p| cnt += property_wc(p) }
    (m[:properties] || []).each {|p| cnt += property_wc(p) }
    cnt += wc(m[:return][:doc]) if m[:return]
  end
  cnt
end

#property_wc(property) ⇒ Object



90
91
92
93
94
# File 'lib/jsduck/stats.rb', line 90

def property_wc(property)
  cnt = wc(property[:doc] || "")
  (property[:properties] || []).each {|p| cnt += property_wc(p) }
  cnt
end

#wc(str) ⇒ Object

Strips HTML and counts words in text



97
98
99
# File 'lib/jsduck/stats.rb', line 97

def wc(str)
  str.gsub(/<\/?[^>]*>/, "").scan(/\w+/).length
end