Class: ReVIEW::Book::Volume

Inherits:
Object show all
Defined in:
lib/review/book/volume.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes = 0, chars = 0, lines = 0) ⇒ Volume



35
36
37
38
39
40
# File 'lib/review/book/volume.rb', line 35

def initialize(bytes = 0, chars = 0, lines = 0)
  @bytes = bytes
  @chars = chars
  @lines = lines
  @page_per_kbyte = nil
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



42
43
44
# File 'lib/review/book/volume.rb', line 42

def bytes
  @bytes
end

#charsObject (readonly)

Returns the value of attribute chars.



43
44
45
# File 'lib/review/book/volume.rb', line 43

def chars
  @chars
end

#linesObject

Returns the value of attribute lines.



44
45
46
# File 'lib/review/book/volume.rb', line 44

def lines
  @lines
end

#page_per_kbyteObject

Returns the value of attribute page_per_kbyte.



45
46
47
# File 'lib/review/book/volume.rb', line 45

def page_per_kbyte
  @page_per_kbyte
end

Class Method Details

.count_file(path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/review/book/volume.rb', line 15

def Volume.count_file(path)
  b = c = l = 0
  File.foreach(path) do |line|
    next if %r<\A\#@> =~ line
    text = line.gsub(/\s+/, '')
    b += text.bytesize
    c += text.charsize
    l += 1
  end
  new(b, c, l)
end

.dummyObject



31
32
33
# File 'lib/review/book/volume.rb', line 31

def Volume.dummy
  new(-1, -1, -1)
end

.sum(vols) ⇒ Object



27
28
29
# File 'lib/review/book/volume.rb', line 27

def Volume.sum(vols)
  vols.inject(new()) {|sum, i| sum + i }
end

Instance Method Details

#+(other) ⇒ Object



59
60
61
62
63
# File 'lib/review/book/volume.rb', line 59

def +(other)
  Volume.new(@bytes + other.bytes,
    @chars + other.chars,
    @lines + other.lines)
end

#kbytesObject



47
48
49
# File 'lib/review/book/volume.rb', line 47

def kbytes
  (@bytes.to_f / 1024).ceil
end

#pageObject



51
52
53
# File 'lib/review/book/volume.rb', line 51

def page
  (kbytes.to_f/@page_per_kbyte).ceil
end

#to_sObject



55
56
57
# File 'lib/review/book/volume.rb', line 55

def to_s
  "#{kbytes()}KB #{@chars}C #{@lines}L #{page()}P"
end