Class: Wip::Todo

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/todo.rb

Constant Summary collapse

ATTRIBUTES =
%i(id body completed_at created_at)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, body: nil, completed_at: nil, created_at: nil) ⇒ Todo

Returns a new instance of Todo.



63
64
65
66
67
68
# File 'lib/wip/todo.rb', line 63

def initialize(id: nil, body: nil, completed_at: nil, created_at: nil)
  @id = id
  @body = body
  @completed_at = completed_at
  @created_at = created_at
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/wip/todo.rb', line 11

def client
  @client
end

#deletedObject

Returns the value of attribute deleted.



10
11
12
# File 'lib/wip/todo.rb', line 10

def deleted
  @deleted
end

Class Method Details

.collection_query(**args) ⇒ Object



13
14
15
16
# File 'lib/wip/todo.rb', line 13

def self.collection_query(**args)
  conditions = Wip::GraphqlHelper.query_conditions(**args)
  %{ todos#{conditions} { #{default_selection} } }
end

.complete(id) ⇒ Object



44
45
46
47
48
# File 'lib/wip/todo.rb', line 44

def self.complete(id)
  todo = find id
  todo.complete
  todo
end

.create(body:, completed_at: nil) ⇒ Object



33
34
35
# File 'lib/wip/todo.rb', line 33

def self.create(body:, completed_at: nil)
  new(body: body, completed_at: completed_at).tap &:save
end

.default_selectionObject



18
19
20
# File 'lib/wip/todo.rb', line 18

def self.default_selection
  ATTRIBUTES.join("\n ")
end

.delete(id) ⇒ Object



56
57
58
59
60
# File 'lib/wip/todo.rb', line 56

def self.delete(id)
  todo = find id
  todo.delete
  todo
end

.find(id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/wip/todo.rb', line 22

def self.find(id)
  client = Wip::Client.new
  find_query = %{
    {
      todo(id: \"#{id}\") {#{default_selection}}
    }
  }
  client.request find_query
  parse client.data("todo")
end

.parse(data) ⇒ Object



37
38
39
40
41
42
# File 'lib/wip/todo.rb', line 37

def self.parse(data)
  todo_id = data["id"].to_i
  completed_at = DateTime.parse(data["completed_at"]) unless data["completed_at"].nil?
  created_at = DateTime.parse(data["created_at"]) unless data["created_at"].nil?
  new id: todo_id, body: data["body"], completed_at: completed_at, created_at: created_at
end

.uncomplete(id) ⇒ Object



50
51
52
53
54
# File 'lib/wip/todo.rb', line 50

def self.uncomplete(id)
  todo = find id
  todo.uncomplete
  todo
end

Instance Method Details

#completeObject



87
88
89
90
91
92
# File 'lib/wip/todo.rb', line 87

def complete
  client.request complete_query
  client.data("completeTodo").tap do |params|
    @completed_at = DateTime.parse params["completed_at"]
  end
end

#deleteObject



112
113
114
115
# File 'lib/wip/todo.rb', line 112

def delete
  client.request delete_query
  @deleted = !client.data("deleteTodo").nil?
end

#descriptionObject



75
76
77
# File 'lib/wip/todo.rb', line 75

def description
  [icon, body, "- #{id}"].join " "
end

#done?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/wip/todo.rb', line 79

def done?
  !completed_at.nil?
end

#iconObject



83
84
85
# File 'lib/wip/todo.rb', line 83

def icon
  deleted ? "🗑" : (done? ? "✅" : "🚧")
end

#saveObject



105
106
107
108
109
110
# File 'lib/wip/todo.rb', line 105

def save
  client.request create_query
  client.data("createTodo").tap do |params|
    @id = params["id"]&.to_i
  end
end

#toggleObject



101
102
103
# File 'lib/wip/todo.rb', line 101

def toggle
  done? ? uncomplete : complete
end

#uncompleteObject



94
95
96
97
98
99
# File 'lib/wip/todo.rb', line 94

def uncomplete
  client.request uncomplete_query
  client.data("uncompleteTodo").tap do |params|
    @completed_at = nil
  end
end