Class: Block

Inherits:
Object
  • Object
show all
Includes:
Attributes, Comparable, Totals
Defined in:
lib/block.rb

Constant Summary

Constants included from Totals

Totals::FORMATTERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Totals

format, #total_str

Methods included from Attributes

#initialize

Instance Attribute Details

#dayObject

Returns the value of attribute day


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

def day
  @day
end

#finishObject

Returns the value of attribute finish


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

def finish
  @finish
end

#startObject

Returns the value of attribute start


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

def start
  @start
end

Class Method Details

.from(str, day) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/block.rb', line 9

def self.from(str, day)
  block = Block.new
  block.day = day

  start_str, finish_str = str.split '-'
  start_ary = start_str.split ':'
  finish_ary = finish_str.split ':'

  if start_ary.empty? && finish_ary.empty?
    fail ArgumentError, "#{str} is not a valid block"
  end

  block.start  = Time.new(day.year, day.month, day.day, *start_ary)
  block.finish = Time.new(day.year, day.month, day.day, *finish_ary)
  if block.finish < block.start
    block.finish = block.finish.next_day
    day.unhealthy!
    block.over_midnight!
  end
  block
end

Instance Method Details

#<=>(other) ⇒ Object


41
42
43
# File 'lib/block.rb', line 41

def <=>(other)
  start <=> other.start
end

#finish_sObject


59
60
61
# File 'lib/block.rb', line 59

def finish_s
  format finish
end

#include?(t) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

46
47
48
# File 'lib/block.rb', line 46

def include?(t)
  (start <= t.start) && (finish >= t.finish)
end

#ongoing?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/block.rb', line 63

def ongoing?
  start == finish
end

#start_sObject


55
56
57
# File 'lib/block.rb', line 55

def start_s
  format start
end

#strict_include?(t) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

51
52
53
# File 'lib/block.rb', line 51

def strict_include?(t)
  (start < t.start) && (finish > t.finish)
end

#to_s(options = {}) ⇒ Object


31
32
33
34
35
# File 'lib/block.rb', line 31

def to_s(options = {})
  str = "#{format start}-"
  return str << '     ' if options[:fancy] && ongoing?
  str << format(finish)
end

#totalObject


37
38
39
# File 'lib/block.rb', line 37

def total
  (finish - start).to_i
end