Class: Recorder::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/recorder/command.rb,
lib/recorder/command/options.rb

Defined Under Namespace

Modules: Options

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



9
10
11
# File 'lib/recorder/command.rb', line 9

def initialize(argv)
  @argv = argv
end

Class Method Details

.run(argv) ⇒ Object



5
6
7
# File 'lib/recorder/command.rb', line 5

def self.run(argv)
  new(argv).execute
end

Instance Method Details

#create_data(weight, bodyfat) ⇒ Object



37
38
39
40
# 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) ⇒ Object



47
48
49
50
# File 'lib/recorder/command.rb', line 47

def delete_data(id)
  data = Data.find(id)
  data.destroy
end

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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_dataObject



59
60
61
# File 'lib/recorder/command.rb', line 59

def find_data()
  all_data = Data.order('created_at DESC')
end

#list_data(weight, bodyfat) ⇒ Object



42
43
44
45
# 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) ⇒ Object



52
53
54
55
56
57
# File 'lib/recorder/command.rb', line 52

def update_data(id, attributes)
  data = Data.find(id)
  data.update_attributes! attributes

  data.reload
end