class CountVonCount::Stat

Constants

PATTERNS
VALS

Public Class Methods

get_pattern(type) click to toggle source
# File lib/count_von_count/stat.rb, line 27
def self.get_pattern(type)
  return PATTERNS[type]
end
new(all = 0, loc = 0, methods = 0, modules = 0, classes = 0, comments = 0) click to toggle source
# 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
sum(stats) click to toggle source
# File lib/count_von_count/stat.rb, line 31
def self.sum(stats)
  stats.reduce(Stat.new) do |prev, val|
    prev + val
  end
end

Public Instance Methods

+(other) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/count_von_count/stat.rb, line 111
def empty?
  loc == 0
end
file_type(file_path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_h() click to toggle source
# 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