Class: Hiiro::TodoItem
- Inherits:
-
Object
- Object
- Hiiro::TodoItem
- Defined in:
- lib/hiiro/todo.rb
Constant Summary collapse
- STATUSES =
%w[not_started started done skip].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #add_tag(tag) ⇒ Object
- #before_create ⇒ Object
- #full_task_name ⇒ Object
- #has_tag?(tag) ⇒ Boolean
- #has_task_info? ⇒ Boolean
-
#initialize(values = OPTS) ⇒ TodoItem
constructor
A new instance of TodoItem.
- #match?(query) ⇒ Boolean
- #remove_tag(tag) ⇒ Object
- #tags_list ⇒ Object
- #to_h ⇒ Object
- #update_status(new_status) ⇒ Object
Constructor Details
Class Method Details
.create_table!(db) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hiiro/todo.rb', line 13 def self.create_table!(db) db.create_table?(:todos) do Integer :id, primary_key: true String :text, null: false String :status, default: 'not_started' String :tags String :task_name String :subtask_name String :tree String :branch String :session String :created_at String :updated_at end end |
.from_h(h) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hiiro/todo.rb', line 94 def self.from_h(h) new( id: h['id'], text: h['text'], status: h['status'], tags: h['tags'], task_name: h['task_name'], subtask_name: h['subtask_name'], tree: h['tree'], branch: h['branch'], session: h['session'], created_at: h['created_at'], updated_at: h['updated_at'] ) end |
Instance Method Details
#add_tag(tag) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/hiiro/todo.rb', line 49 def add_tag(tag) current = return if current.any? { |t| t.downcase == tag.downcase } current << tag self. = current.join(', ') end |
#before_create ⇒ Object
35 36 37 38 |
# File 'lib/hiiro/todo.rb', line 35 def before_create self.updated_at ||= created_at super end |
#full_task_name ⇒ Object
65 66 67 68 |
# File 'lib/hiiro/todo.rb', line 65 def full_task_name return nil unless has_task_info? subtask_name ? "#{task_name}/#{subtask_name}" : task_name end |
#has_tag?(tag) ⇒ Boolean
45 46 47 |
# File 'lib/hiiro/todo.rb', line 45 def has_tag?(tag) .any? { |t| t.downcase == tag.downcase } end |
#has_task_info? ⇒ Boolean
61 62 63 |
# File 'lib/hiiro/todo.rb', line 61 def has_task_info? !task_name.nil? || !subtask_name.nil? end |
#match?(query) ⇒ Boolean
110 111 112 113 114 115 116 117 118 |
# File 'lib/hiiro/todo.rb', line 110 def match?(query) query = query.downcase text.downcase.include?(query) || .any? { |t| t.downcase.include?(query) } || (task_name && task_name.downcase.include?(query)) || (subtask_name && subtask_name.downcase.include?(query)) || (tree && tree.downcase.include?(query)) || (branch && branch.downcase.include?(query)) end |
#remove_tag(tag) ⇒ Object
56 57 58 59 |
# File 'lib/hiiro/todo.rb', line 56 def remove_tag(tag) current = .reject { |t| t.downcase == tag.downcase } self. = current.empty? ? nil : current.join(', ') end |
#tags_list ⇒ Object
40 41 42 43 |
# File 'lib/hiiro/todo.rb', line 40 def return [] if .nil? || .empty? .split(',').map(&:strip) end |
#to_h ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hiiro/todo.rb', line 77 def to_h h = { 'id' => id, 'text' => text, 'status' => status, 'created_at' => created_at, 'updated_at' => updated_at } h['tags'] = if && !.empty? h['task_name'] = task_name if task_name h['subtask_name'] = subtask_name if subtask_name h['tree'] = tree if tree h['branch'] = branch if branch h['session'] = session if session h end |
#update_status(new_status) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/hiiro/todo.rb', line 70 def update_status(new_status) return false unless STATUSES.include?(new_status) self.status = new_status self.updated_at = Time.now.to_s true end |