Module: Temporal::ClassMethods

Defined in:
lib/acts_as_flux_capacitor/temporal.rb

Instance Method Summary collapse

Instance Method Details

#length_of_time_since(obj, reference_obj = Time.now) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acts_as_flux_capacitor/temporal.rb', line 41

def length_of_time_since(obj,reference_obj=Time.now)
  begins_at = extract_time(obj,:begins_at)
  ends_at = extract_time(obj,:ends_at)
  
  reference_point = extract_time(reference_obj,:begins_at)

  time_since_ended = reference_point - ends_at
  has_started_but_not_ended = time_since_ended <= 0 && reference_point.after?(begins_at)
  has_ended = time_since_ended >= 0
  if has_ended
    time_since_ended
  elsif has_started_but_not_ended
    0
  else # has not started
    nil
  end
end

#length_of_time_until(obj, reference_obj = Time.now) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_flux_capacitor/temporal.rb', line 23

def length_of_time_until(obj,reference_obj=Time.now)
  begins_at = extract_time(obj,:begins_at)
  ends_at = extract_time(obj,:ends_at)
  reference_point = extract_time(reference_obj,:ends_at)
  
  time_until_begins = begins_at - reference_point
  # has not started
  if time_until_begins >= 0
    time_until_begins
  # has started, but not ended
  elsif time_until_begins < 0 && reference_point.before?(ends_at)
    0
  # has ended
  else
    nil
  end
end

#period_between(obj, another_obj) ⇒ Object



16
17
18
19
20
21
# File 'lib/acts_as_flux_capacitor/temporal.rb', line 16

def period_between(obj,another_obj)
  first_obj,second_obj = ordered_tuple(obj,another_obj)
  beginning = extract_time(first_obj,:ends_at)
  ending    = extract_time(second_obj,:begins_at)
  beginning.before_or_at?(ending) ? ending - beginning : nil
end

#range_between(obj, another_obj) ⇒ Object



9
10
11
12
13
14
# File 'lib/acts_as_flux_capacitor/temporal.rb', line 9

def range_between(obj,another_obj)
  first_obj,second_obj = ordered_tuple(obj,another_obj)
  beginning = extract_time(first_obj,:ends_at)
  ending    = extract_time(second_obj,:begins_at)
  beginning.before?(ending) ? (beginning..ending).to_time : nil
end