Sha256: dd213a32e2d05c9e476f7cc98e7f5c179dd2aff5146a9b0df6f82f6ef1a5152e
Contents?: true
Size: 1.7 KB
Versions: 11
Compression:
Stored size: 1.7 KB
Contents
# coding: utf-8 module Recorder class Command def self.run(argv) new(argv).execute end def initialize(argv) @argv = argv end 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 def create_data(weight, bodyfat) date = Time.now Data.create!(weight: weight, bodyfat: bodyfat, date: date).reload end def list_data(weight, bodyfat) date = Time.now Data.create!(weight: weight, bodyfat: bodyfat, date: date).reload end def delete_data(id) data = Data.find(id) data.destroy end def update_data(id, attributes) data = Data.find(id) data.update_attributes! attributes data.reload end def find_data() all_data = Data.order('created_at DESC') end private 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 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 end end
Version data entries
11 entries across 11 versions & 1 rubygems