Class: Taskcmd::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/taskcmd/task.rb

Overview

An individual Todo item

Constant Summary collapse

PRIORITY_LOW =
:low
PRIORITY_MEDIUM =
:medium
PRIORITY_HIGH =
:high
PRIORITY_CHOICES =
[PRIORITY_LOW, PRIORITY_MEDIUM, PRIORITY_HIGH]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Task

Returns a new instance of Task.

Raises:



14
15
16
17
18
19
20
# File 'lib/taskcmd/task.rb', line 14

def initialize(id)
  raise Taskcmd::Error, 'invalid id' unless id.is_a?(Integer)

  @id = id
  @priority = PRIORITY_MEDIUM
  @created_at = Time.now
end

Instance Attribute Details

#completed_atObject (readonly)

Returns the value of attribute completed_at.



11
12
13
# File 'lib/taskcmd/task.rb', line 11

def completed_at
  @completed_at
end

#created_atObject (readonly)

Returns the value of attribute created_at.



11
12
13
# File 'lib/taskcmd/task.rb', line 11

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/taskcmd/task.rb', line 12

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/taskcmd/task.rb', line 11

def id
  @id
end

#priorityObject

Returns the value of attribute priority.



11
12
13
# File 'lib/taskcmd/task.rb', line 11

def priority
  @priority
end

Class Method Details

.from_msgpack_ext(data) ⇒ Object



62
63
64
65
66
67
# File 'lib/taskcmd/task.rb', line 62

def self.from_msgpack_ext(data)
  unpacked = MessagePack.unpack(data)
  new(unpacked[:id]).tap do |obj|
    unpacked.each { |k, v| obj.instance_variable_set("@#{k}", v) }
  end
end

Instance Method Details

#complete!Object



32
33
34
# File 'lib/taskcmd/task.rb', line 32

def complete!
  @completed_at = Time.now
end

#done?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/taskcmd/task.rb', line 40

def done?
  !@completed_at.nil?
end

#to_msgpack_extObject



52
53
54
55
56
57
58
59
60
# File 'lib/taskcmd/task.rb', line 52

def to_msgpack_ext
  {
    id: id,
    priority: priority,
    description: description,
    created_at: created_at,
    completed_at: completed_at,
  }.to_msgpack
end

#to_sObject



44
45
46
47
48
49
50
# File 'lib/taskcmd/task.rb', line 44

def to_s
  "id: #{id}\n" \
  "priority: #{priority}\n" \
  "description: #{description}\n" \
  "created_at: #{created_at}\n" \
  "completed_at: #{completed_at || "-"}" \
end

#undo!Object



36
37
38
# File 'lib/taskcmd/task.rb', line 36

def undo!
  @completed_at = nil
end