Class: Buncho::WeightLogger

Inherits:
BaseLogger show all
Defined in:
lib/buncho/weight_logger.rb

Constant Summary collapse

DATA_DIR_PATH =
File.expand_path("../../../data/weight/", __FILE__)

Instance Attribute Summary

Attributes inherited from BaseLogger

#path

Instance Method Summary collapse

Methods inherited from BaseLogger

#read_lines

Constructor Details

#initialize(name, file_path = DATA_DIR_PATH) ⇒ WeightLogger

Returns a new instance of WeightLogger.



7
8
9
10
11
# File 'lib/buncho/weight_logger.rb', line 7

def initialize(name, file_path = DATA_DIR_PATH)
  file_path = "#{file_path}/#{name}.csv"
  @name = name
  super(file_path)
end

Instance Method Details

#add(weight, date = nil) ⇒ Object



13
14
15
16
17
# File 'lib/buncho/weight_logger.rb', line 13

def add(weight, date = nil)
  date = Date.today if date.nil?
  line = [date, weight].join(',')
  @io.write("#{line}\n")
end

#formatted_rows(n) ⇒ Object



27
28
29
30
# File 'lib/buncho/weight_logger.rb', line 27

def formatted_rows(n)
  @io.rewind
  read_lines.reverse.first(n)
end

#show(n = 10) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/buncho/weight_logger.rb', line 19

def show(n = 10)
  puts "#{@name}'s weights:"
  formatted_rows(n).each do |line|
    line = line.split(",")
    puts "#{line[0]}: #{line[1]}g"
  end
end