Class: MyTodo::Todo

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/my_todo.rb

Overview

Todo tasks using thor gem

Instance Method Summary collapse

Instance Method Details

#createObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/my_todo.rb', line 54

def create
  begin
    item = Item.create!(options.except(:tags))
    options[:tags].split(' ').each{|tag| item.tags.create(name: tag) }
    say 'ToDo CREATED!'
    output item
  rescue ActiveRecord::RecordInvalid => e
    say e.message
  end
end

#delete(id) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/my_todo.rb', line 81

def delete(id)
  begin
    item = Item.find_by_id(id)
    output item
    item.destroy!
    say 'ToDo DESTROYED!'
  rescue Exception => e
    say e.message
  end
end

#list(status = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/my_todo.rb', line 35

def list(status=nil)
  items = case status
  when 'all'
    Item.all
  when 'done'
    Item.where(done: true)
  else
    Item.where(done: false)
  end

  say "ToDos FOUND: #{items.count}"
  items.each {|item| output(item)}
end

#noteObject



125
126
127
128
129
130
131
132
# File 'lib/my_todo.rb', line 125

def note
  begin
    item.notes.create(body: options[:body])
    output item.reload
  rescue Exception => e
    say e.message
  end
end

#rm_noteObject



137
138
139
140
141
142
143
144
# File 'lib/my_todo.rb', line 137

def rm_note
  begin
    item.notes.where(id: options[:noteid]).first.destroy!
    output item.reload
  rescue Exception => e
    say e.message
  end
end

#rm_tagObject



113
114
115
116
117
118
119
120
# File 'lib/my_todo.rb', line 113

def rm_tag
  begin
    item.tags.where(name: options[:tag]).first.destroy!
    output item.reload
  rescue Exception => e
    say e.message
  end
end

#search(text) ⇒ Object



93
94
95
96
97
# File 'lib/my_todo.rb', line 93

def search(text)
  items = Item.ransack(body_or_tags_name_or_notes_body_cont: text).result
  say "ToDos FOUND: #{items.count}"
  items.each {|item| output item}
end

#tagObject



102
103
104
105
106
107
108
# File 'lib/my_todo.rb', line 102

def tag
  begin
    item.tags.create!(name: options[:tag])
  rescue Exception => e
    say e.message
  end
end

#updateObject



70
71
72
73
74
75
76
77
78
# File 'lib/my_todo.rb', line 70

def update
  begin
    item.update!(options)
    say 'ToDo UPDATED!'
    output item
  rescue ActiveRecord::RecordInvalid => e
    say e.message
  end
end