Class: Timert::Day

Inherits:
Object
  • Object
show all
Defined in:
lib/timert/day.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Day

Returns a new instance of Day.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/timert/day.rb', line 7

def initialize(args = {})
  @intervals = args[:intervals] || []
  @tasks = args[:tasks] || []
  @date = args[:date]

  raise ArgumentError.new("intervals should be an array") if !@intervals.is_a?(Array)
  raise ArgumentError.new("tasks should be an array") if !@tasks.is_a?(Array)
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/timert/day.rb', line 4

def date
  @date
end

#intervalsObject (readonly)

Returns the value of attribute intervals.



4
5
6
# File 'lib/timert/day.rb', line 4

def intervals
  @intervals
end

#onObject

Returns the value of attribute on.



5
6
7
# File 'lib/timert/day.rb', line 5

def on
  @on
end

#tasksObject (readonly)

Returns the value of attribute tasks.



4
5
6
# File 'lib/timert/day.rb', line 4

def tasks
  @tasks
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
# File 'lib/timert/day.rb', line 65

def ==(other)
  other.to_hash == to_hash
end

#add_start(time) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/timert/day.rb', line 16

def add_start(time)
  if !is_interval_started?
    if time <= last_start
      raise ArgumentError.new("Invalid start time")
    elsif time < last_stop
      raise ArgumentError.new("Invalid start time. It's before the last stop time.")
    elsif !is_date_correct?(time)
      raise ArgumentError.new("Invalid date")
    end
    @intervals.push({"start" => time})
    time
  end
end

#add_stop(time) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/timert/day.rb', line 30

def add_stop(time)
  if is_interval_started?
    if time < last_start
      raise ArgumentError.new("Invalid stop time")
    elsif !is_date_correct?(time)
      raise ArgumentError.new("Invalid date")
    end
    @intervals.last["stop"] = time
    time
  end
end

#add_task(task) ⇒ Object



48
49
50
# File 'lib/timert/day.rb', line 48

def add_task(task)
  @tasks.push(task)
end

#is_interval_started?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/timert/day.rb', line 59

def is_interval_started?
  @intervals.length > 0 &&
    @intervals.last["start"] &&
    !@intervals.last["stop"]
end

#last_startObject



69
70
71
# File 'lib/timert/day.rb', line 69

def last_start
  last_interval['start'].to_i
end

#last_stopObject



73
74
75
# File 'lib/timert/day.rb', line 73

def last_stop
  last_interval['stop'].to_i
end

#to_hashObject



52
53
54
55
56
57
# File 'lib/timert/day.rb', line 52

def to_hash
  {
    "tasks" => @tasks.uniq,
    "intervals" => @intervals
  }
end

#total_elapsed_timeObject



42
43
44
45
46
# File 'lib/timert/day.rb', line 42

def total_elapsed_time
  total = 0
  @intervals.each { |i| total += interval_duration(i) }
  total
end