Class: Timely::TemporalPatterns::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/timely/temporal_patterns/interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first_datetime, last_datetime = nil) ⇒ Interval

Returns a new instance of Interval.



16
17
18
19
# File 'lib/timely/temporal_patterns/interval.rb', line 16

def initialize(first_datetime, last_datetime = nil)
  self.first_datetime = first_datetime
  self.last_datetime = last_datetime || first_datetime
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

:nodoc:



62
63
64
# File 'lib/timely/temporal_patterns/interval.rb', line 62

def method_missing(method, *args, &block) #:nodoc:
  range.send(method, *args, &block)
end

Instance Attribute Details

#first_datetimeObject

Returns the value of attribute first_datetime.



4
5
6
# File 'lib/timely/temporal_patterns/interval.rb', line 4

def first_datetime
  @first_datetime
end

#last_datetimeObject

Returns the value of attribute last_datetime.



4
5
6
# File 'lib/timely/temporal_patterns/interval.rb', line 4

def last_datetime
  @last_datetime
end

Class Method Details

.surrounding(intervals) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/timely/temporal_patterns/interval.rb', line 6

def self.surrounding(intervals)
  first_datetime = nil
  last_datetime = nil
  intervals.each do |i|
    first_datetime = i.first_datetime if first_datetime.nil? || i.first_datetime < first_datetime
    last_datetime = i.last_datetime if last_datetime.nil? || i.last_datetime > last_datetime
  end
  new(first_datetime, last_datetime)
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
# File 'lib/timely/temporal_patterns/interval.rb', line 37

def ==(other)
  self.range == other.range
end

#date_time_to_s(datetime) ⇒ Object



56
57
58
# File 'lib/timely/temporal_patterns/interval.rb', line 56

def date_time_to_s(datetime)
  datetime.strftime("%I:%M %p")
end

#datetimesObject



33
34
35
# File 'lib/timely/temporal_patterns/interval.rb', line 33

def datetimes
  range.to_a
end

#rangeObject



29
30
31
# File 'lib/timely/temporal_patterns/interval.rb', line 29

def range
  (first_datetime..last_datetime)
end

#to_sObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/timely/temporal_patterns/interval.rb', line 41

def to_s
  if first_datetime == last_datetime
    "on #{first_datetime}#{first_datetime == first_datetime.beginning_of_day ? "" : " at #{first_datetime.strftime("%I:%M %p")}"}"
  elsif first_datetime == first_datetime.beginning_of_month && last_datetime == last_datetime.end_of_month
    if first_datetime.month == last_datetime.month
      "during #{first_datetime.strftime('%b %Y')}"
    else
      "from #{first_datetime.strftime('%b %Y')} to #{last_datetime.strftime('%b %Y')}"
    end
  else
    "from #{first_datetime}#{first_datetime == first_datetime.beginning_of_day ? "" : " at #{first_datetime.strftime("%I:%M %p")}"} "+
    "to #{last_datetime}#{last_datetime == last_datetime.beginning_of_day ? "" : " at #{last_datetime.strftime("%I:%M %p")}"}"
  end
end