Module: Doing::ItemDates

Included in:
Item
Defined in:
lib/doing/item/dates.rb

Instance Method Summary collapse

Instance Method Details

#calculate_end_date(opt) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/doing/item/dates.rb', line 35

def calculate_end_date(opt)
  if opt[:took]
    if @date + opt[:took] > Time.now
      @date = Time.now - opt[:took]
      Time.now
    else
      @date + opt[:took]
    end
  elsif opt[:back]
    if opt[:back].is_a? Integer
      @date + opt[:back]
    else
      @date + (opt[:back] - @date)
    end
  else
    Time.now
  end
end

#durationObject

If the entry doesn't have a @done date, return the elapsed time



8
9
10
11
12
13
14
# File 'lib/doing/item/dates.rb', line 8

def duration
  return nil unless should_time? && should_finish?

  return nil if @title =~ /(?<=^| )@done\b/

  return Time.now - @date
end

#end_dateTime

Get the value of the item's @done tag

Returns:

  • (Time)

    @done value



31
32
33
# File 'lib/doing/item/dates.rb', line 31

def end_date
  @end_date ||= Time.parse(Regexp.last_match(1)) if @title =~ /@done\((\d{4}-\d\d-\d\d \d\d:\d\d.*?)\)/
end

#expand_date_tags(additional_tags = nil) ⇒ Object

Updates the title of the Item by expanding natural language dates within configured date tags (tags whose value is expected to be a date)

Parameters:

  • additional_tags (defaults to: nil)

    An array of additional tag names to consider dates



94
95
96
# File 'lib/doing/item/dates.rb', line 94

def expand_date_tags(additional_tags = nil)
  @title.expand_date_tags(additional_tags)
end

#intervalObject

Get the difference between the item's start date and the value of its @done tag (if present)

Returns:

  • Interval in seconds



22
23
24
# File 'lib/doing/item/dates.rb', line 22

def interval
  @interval ||= calc_interval
end

#overlapping_time?(item_b) ⇒ Boolean

Test if the interval between start date and @done value overlaps with another item's

Parameters:

  • item_b (Item)

    The item to compare

Returns:

  • (Boolean)

    overlaps?



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/doing/item/dates.rb', line 73

def overlapping_time?(item_b)
  return true if same_time?(item_b)

  start_a = date
  a_interval = interval
  end_a = a_interval ? start_a + a_interval.to_i : start_a
  start_b = item_b.date
  b_interval = item_b.interval
  end_b = b_interval ? start_b + b_interval.to_i : start_b
  (start_a >= start_b && start_a <= end_b) || (end_a >= start_b && end_a <= end_b) || (start_a < start_b && end_a > end_b)
end

#same_time?(item_b) ⇒ Boolean

Test if two items occur at the same time (same start date and equal duration)

Parameters:

  • item_b (Item)

    The item to compare

Returns:

  • (Boolean)

    is equal?



61
62
63
# File 'lib/doing/item/dates.rb', line 61

def same_time?(item_b)
  date == item_b.date ? interval == item_b.interval : false
end