Class: Loco::Stats

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/loco/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#align

Constructor Details

#initialize(path) ⇒ Stats

Returns a new instance of Stats.



9
10
11
12
# File 'lib/loco/stats.rb', line 9

def initialize(path)
  @path = path
  @lines = count_lines(path)
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



7
8
9
# File 'lib/loco/stats.rb', line 7

def lines
  @lines
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/loco/stats.rb', line 7

def path
  @path
end

Instance Method Details

#blank?(str) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/loco/stats.rb', line 14

def blank? str
  str.nil? || str.strip == ""
end

#count_lines(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/loco/stats.rb', line 18

def count_lines path
  File.read(path).split(/\n/).inject(0) {|sum, line| sum + (blank?(line) ? 0 : 1) }
rescue Exception => e
  puts "ERROR: reading #{path}: #{e.message}"
  puts ""
  puts "consider adding this line to your .loco:"
  puts ""
  puts "Loco::Counter::EXCLUDE << /^#{path.gsub /\//, "\\/"}/"
  puts ""
  0
end

#matches(options) ⇒ Object



34
35
36
# File 'lib/loco/stats.rb', line 34

def matches options
  options[:size].nil? || (options[:size] == lines)
end

#to_sObject



30
31
32
# File 'lib/loco/stats.rb', line 30

def to_s
  "#{align lines} #{path}"
end