Class: Task

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduled-format/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, time_frame: nil, start_time: nil, tags: Array.new) ⇒ Task

Create a new scheduled task.

Parameters:

  • body (String)

    the task description.

  • start_time (Hour)

    when the task starts.

  • time_frame (String)

    name, initials or an abbreviation of a time frame to which the task will be scheduled.

  • tags (Array<Symbol>)

    list of tags.

Raises:

  • (ArgumentError)

13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scheduled-format/task.rb', line 13

def initialize(body:, time_frame: nil, start_time: nil, tags: Array.new)
  @body, @time_frame, @start_time, @tags = body, time_frame, start_time, tags

  raise ArgumentError, "Body cannot be empty." if body.empty?

  if start_time && !start_time.is_a?(Hour)
    raise ArgumentError, "Hour instance was expected, got #{start_time.class}"
  end

  if !tags.empty? && tags.any? { |tag| !tag.is_a?(Symbol) }
    raise ArgumentError, "Tags are supposed to be an array of symbols."
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


4
5
6
# File 'lib/scheduled-format/task.rb', line 4

def body
  @body
end

#start_timeObject (readonly)

Returns the value of attribute start_time


4
5
6
# File 'lib/scheduled-format/task.rb', line 4

def start_time
  @start_time
end

#tagsObject (readonly)

Returns the value of attribute tags


4
5
6
# File 'lib/scheduled-format/task.rb', line 4

def tags
  @tags
end

#time_frameObject (readonly)

Returns the value of attribute time_frame


4
5
6
# File 'lib/scheduled-format/task.rb', line 4

def time_frame
  @time_frame
end

Instance Method Details

#to_sObject

Format task in the scheduled task list format.

Since:

  • 0.2


29
30
31
32
33
34
35
36
# File 'lib/scheduled-format/task.rb', line 29

def to_s
  [
    '-',
    ("[#{@time_frame}]" if @time_frame),
    ("[#{@start_time}]" if @start_time),
    @body.to_s, *@tags.map { |tag| "##{tag}" }
  ].compact.join(' ')
end