Class: Idid::Task

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, logdate = nil) ⇒ Task

Public: Initialize a new Task

contents - The String containing the task that has been done

Returns an instance of Task



13
14
15
16
# File 'lib/idid/task.rb', line 13

def initialize(contents, logdate = nil)
  @contents = contents
  @logdate = logdate || Date.today
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



6
7
8
# File 'lib/idid/task.rb', line 6

def contents
  @contents
end

#logObject

Returns the value of attribute log.



6
7
8
# File 'lib/idid/task.rb', line 6

def log
  @log
end

#logdateObject

Returns the value of attribute logdate.



6
7
8
# File 'lib/idid/task.rb', line 6

def logdate
  @logdate
end

Class Method Details

.list(date) ⇒ Object

Public: Lists all activities for a given date.

date - The String date you wish to show the entries of, in the format

YYYY-MM-DD

Returns the String showing the logged activities



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

def self.list(date)
  formatted_date = Date.parse(date).strftime '%e %B %Y'
  list = read_log[date]

  if list.nil?
    "Could not find any activity for#{formatted_date}"
  else
    formatted_log = list.map{|l| '* ' + l }.join "\n"
    "Log for#{formatted_date}:\n#{formatted_log}"
  end
end

Instance Method Details

#saveObject

Public: Save a task to the log file. If there were tasks for this day already, it’s appended to that days log. Otherwise, a new day is added to the log.

Returns boolean



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/idid/task.rb', line 23

def save
  logdate = @logdate.strftime '%Y-%m-%d'

  if log.has_key? logdate
    log[logdate].push @contents
  else
    log[logdate] = [@contents]
  end

  Task.write_log log
end

#to_sObject

Public: Transform Task into a String

Returns the String with the contents of the task



38
39
40
# File 'lib/idid/task.rb', line 38

def to_s
  @contents
end