Class: Hiiro::TodoItem

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(values = OPTS) ⇒ TodoItem

Returns a new instance of TodoItem.



29
30
31
32
33
# File 'lib/hiiro/todo.rb', line 29

def initialize(values = OPTS)
  super
  self.status = STATUSES.include?(status.to_s) ? status : 'not_started'
  self.created_at ||= Time.now.to_s
end

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 = tags_list
  return if current.any? { |t| t.downcase == tag.downcase }
  current << tag
  self.tags = current.join(', ')
end

#before_createObject



35
36
37
38
# File 'lib/hiiro/todo.rb', line 35

def before_create
  self.updated_at ||= created_at
  super
end

#full_task_nameObject



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

Returns:

  • (Boolean)


45
46
47
# File 'lib/hiiro/todo.rb', line 45

def has_tag?(tag)
  tags_list.any? { |t| t.downcase == tag.downcase }
end

#has_task_info?Boolean

Returns:

  • (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

Returns:

  • (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) ||
    tags_list.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 = tags_list.reject { |t| t.downcase == tag.downcase }
  self.tags = current.empty? ? nil : current.join(', ')
end

#tags_listObject



40
41
42
43
# File 'lib/hiiro/todo.rb', line 40

def tags_list
  return [] if tags.nil? || tags.empty?
  tags.split(',').map(&:strip)
end

#to_hObject



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'] = tags if tags && !tags.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