Class: Medo::Task

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/medo/task.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, options = {}) ⇒ Task



24
25
26
27
28
29
# File 'lib/medo/task.rb', line 24

def initialize(description, options = {})
  self.description = description
  @created_at   = clock.now
  @completed_at = nil
  self.notes    = options["notes"] || options[:notes]
end

Class Attribute Details

.clockObject

Returns the value of attribute clock.



10
11
12
# File 'lib/medo/task.rb', line 10

def clock
  @clock
end

Instance Attribute Details

#completed_atObject (readonly)

Returns the value of attribute completed_at.



7
8
9
# File 'lib/medo/task.rb', line 7

def completed_at
  @completed_at
end

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/medo/task.rb', line 7

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/medo/task.rb', line 7

def description
  @description
end

#notesObject

Returns the value of attribute notes.



7
8
9
# File 'lib/medo/task.rb', line 7

def notes
  @notes
end

Class Method Details

.from_attributes(attributes) ⇒ Object



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

def from_attributes(attributes)
  task = Task.new(attributes.delete("description"), attributes)
  task.instance_eval do
    @created_at = attributes["created_at"] or raise ArgumentError, "Missing created_at"
    @done = attributes["done"]
    @completed_at = attributes["completed_at"] or raise ArgumentError, "Missing completed_at" if @done
  end
  task
end

Instance Method Details

#<=>(other) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/medo/task.rb', line 31

def <=>(other)
  unless other.kind_of?(Task)
    raise ArgumentError, "comparison of #{self.class.name} with #{other.class.name} failed"
  end

  case [self.completed_at.nil?, other.completed_at.nil?]
  when [true, true]   then (self.created_at <=> other.created_at) * -1
  when [false, false] then (self.completed_at <=> other.completed_at) * -1
  when [true, false]  then -1
  when [false, true]  then 1
  end
end

#==(other) ⇒ Object



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

def ==(other)
  return true if other.equal? self
  return false unless other.kind_of?(self.class)
  self.description    == other.description &&
    self.created_at   == other.created_at &&
    self.done?        == other.done? &&
    self.completed_at == other.completed_at &&
    self.notes        == other.notes
end

#doneObject



72
73
74
75
# File 'lib/medo/task.rb', line 72

def done
  @completed_at = clock.now
  @done         = true
end

#done?Boolean



82
83
84
# File 'lib/medo/task.rb', line 82

def done?
  !!@done
end

#initialize_copy(source) ⇒ Object



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

def initialize_copy(source)
  super
  @description  = @description.dup
  @created_at   = @created_at.dup
  @completed_at = @completed_at.dup if @completed_at
  @notes        = @notes.dup
end

#resetObject



77
78
79
80
# File 'lib/medo/task.rb', line 77

def reset
  @completed_at = nil
  @done         = false
end