Class: CountVonCount::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/count_von_count/stat.rb

Constant Summary collapse

PATTERNS =
{
  rb: {
    line_comment: /^\s*#/,
    begin_block_comment: /^=begin/,
    end_block_comment: /^=end/,
    class:  /^\s*class\s+[_A-Z]/,
    module: /^\s*module\s+[_A-Z]/,
    method: /^\s*def\s+[_a-z]/,
  },
  js: {
    line_comment: %r{^\s*//},
    begin_block_comment: %r{^\s*/\*},
    end_block_comment: %r{\*/},
    method: /function(\s+[_a-zA-Z][\da-zA-Z]*)?\s*\(/,
  },
  coffee: {
    line_comment: /^\s*#/,
    begin_block_comment: /^\s*###/,
    end_block_comment: /^\s*###/,
    class: /^\s*class\s+[_A-Z]/,
    method: /[-=]>/,
  }
}
VALS =
%i(all loc methods modules classes comments)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all = 0, loc = 0, methods = 0, modules = 0, classes = 0, comments = 0) ⇒ Stat

Returns a new instance of Stat.



39
40
41
42
43
44
45
46
# File 'lib/count_von_count/stat.rb', line 39

def initialize(all = 0, loc = 0, methods = 0, modules = 0, classes = 0, comments = 0)
  @all = all
  @loc = loc
  @methods = methods
  @modules = modules
  @classes = classes
  @comments = comments
end

Class Method Details

.get_pattern(type) ⇒ Object



27
28
29
# File 'lib/count_von_count/stat.rb', line 27

def self.get_pattern(type)
  return PATTERNS[type]
end

.sum(stats) ⇒ Object



31
32
33
34
35
# File 'lib/count_von_count/stat.rb', line 31

def self.sum(stats)
  stats.reduce(Stat.new) do |prev, val|
    prev + val
  end
end

Instance Method Details

#+(other) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/count_von_count/stat.rb', line 48

def + (other)
  r = self.class.new
  VALS.each do |v|
    r.send("#{v.to_s}=", (self.send(v) + other.send(v)))
  end
  r
end

#==(other) ⇒ Object



56
57
58
59
60
61
# File 'lib/count_von_count/stat.rb', line 56

def == (other)
  VALS.each do |v|
    return false if self.send(v) != other.send(v)
  end
  true
end

#empty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/count_von_count/stat.rb', line 111

def empty?
  loc == 0
end

#file_type(file_path) ⇒ Object



107
108
109
# File 'lib/count_von_count/stat.rb', line 107

def file_type(file_path)
  File.extname(file_path).sub(/\A\./, '').downcase.to_sym
end

#process(file) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/count_von_count/stat.rb', line 63

def process(file)
  File.open(file) do |io|
    ft = file_type(file)
    patterns = PATTERNS[ft] || {}
    process_io(io, patterns)
  end
end

#process_io(io, patterns) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/count_von_count/stat.rb', line 71

def process_io(io, patterns)
  comment_started = false

  while line = io.gets
    @all += 1

    if comment_started
      @comments += 1
      if patterns[:end_block_comment] && line =~ patterns[:end_block_comment]
        comment_started = false
      end
      next
    else
      if patterns[:begin_block_comment] && line =~ patterns[:begin_block_comment]
        @comments += 1
        comment_started = true
        next
      end
    end

    @classes   += 1 if patterns[:class] && line =~ patterns[:class]
    @methods   += 1 if patterns[:method] && line =~ patterns[:method]
    @modules   += 1 if patterns[:module] && line =~ patterns[:module]
    if (patterns[:line_comment] && line =~ patterns[:line_comment])
      @comments += 1
    elsif line !~ /^\s*$/
      @loc += 1
    end
  end
end

#to_hObject



103
104
105
# File 'lib/count_von_count/stat.rb', line 103

def to_h
  {all: all, loc: loc, methods: methods, modules: modules, classes: classes, comments: comments}
end