class Recorder::Command
Public Class Methods
new(argv)
click to toggle source
# File lib/recorder/command.rb, line 9 def initialize(argv) @argv = argv end
run(argv)
click to toggle source
# File lib/recorder/command.rb, line 5 def self.run(argv) new(argv).execute end
Public Instance Methods
create_data(weight, bodyfat)
click to toggle source
# File lib/recorder/command.rb, line 37 def create_data(weight, bodyfat) date = Time.now Data.create!(weight: weight, bodyfat: bodyfat, date: date).reload end
delete_data(id)
click to toggle source
# File lib/recorder/command.rb, line 47 def delete_data(id) data = Data.find(id) data.destroy end
execute()
click to toggle source
# File lib/recorder/command.rb, line 13 def execute options = Options.parse!(@argv) sub_command = options.delete(:command) DB.prepare begin data = case sub_command when 'create' create_data(options[:weight],options[:bodyfat]) when 'list' find_data() when 'delete' delete_data(options[:id]) when 'update' update_data(options[:id], options) end display_data data rescue => e abort "Error: #{e.message}" end end
find_data()
click to toggle source
# File lib/recorder/command.rb, line 59 def find_data() all_data = Data.order('created_at DESC') end
list_data(weight, bodyfat)
click to toggle source
# File lib/recorder/command.rb, line 42 def list_data(weight, bodyfat) date = Time.now Data.create!(weight: weight, bodyfat: bodyfat, date: date).reload end
update_data(id, attributes)
click to toggle source
# File lib/recorder/command.rb, line 52 def update_data(id, attributes) data = Data.find(id) data.update_attributes! attributes data.reload end
Private Instance Methods
display_data(data)
click to toggle source
# File lib/recorder/command.rb, line 64 def display_data(data) header = display_format('ID','Weight','BodyFat','Date') puts header puts '-' * header.size Array(data).each do |d| puts display_format(d.id,d.weight,d.bodyfat,d.date) end end
display_format(id, weight, bodyfat, date)
click to toggle source
# File lib/recorder/command.rb, line 74 def display_format(id, weight, bodyfat, date) [id.to_s.rjust(4), weight.to_s.ljust(7),bodyfat.to_s.ljust(7), date.to_s.center(20)].join(' | ') end