Class: TExp::Window

Inherits:
SingleTermBase show all
Defined in:
lib/texp/window.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SingleTermBase

#each, #reanchor

Methods inherited from Base

#*, #+, #-, #-@, #each, #include?, #reanchor, register_parse_callback, #to_s, #window

Constructor Details

#initialize(texp, prewindow_days, postwindow_days) ⇒ Window

Returns a new instance of Window.



5
6
7
8
9
# File 'lib/texp/window.rb', line 5

def initialize(texp, prewindow_days, postwindow_days)
  super(texp)
  @prewindow_days = prewindow_days
  @postwindow_days = postwindow_days      
end

Class Method Details

.parse_callback(stack) ⇒ Object

Parsing callback for window temporal expressions.



61
62
63
64
65
66
# File 'lib/texp/window.rb', line 61

def parse_callback(stack)
  postwindow = stack.pop
  prewindow = stack.pop
  te = stack.pop
  stack.push TExp::Window.new(te, prewindow, postwindow)
end

Instance Method Details

#encode(codes) ⇒ Object

Encode the temporal expression into codes.



48
49
50
51
# File 'lib/texp/window.rb', line 48

def encode(codes)
  @term.encode(codes)
  codes << @prewindow_days << "," << @postwindow_days << "s"
end

#find_pivot(date) ⇒ Object

Find the matching date for the window.



31
32
33
34
35
36
37
38
# File 'lib/texp/window.rb', line 31

def find_pivot(date)
  d = date - @postwindow_days
  while d <= date + @prewindow_days
    return d if @term.includes?(d)
    d += 1
  end
  return nil
end

#first_day_of_window(date) ⇒ Object

Return the first day of the window for a given date. Return nil if the date is not in a window.



18
19
20
21
# File 'lib/texp/window.rb', line 18

def first_day_of_window(date)
  pivot = find_pivot(date)
  pivot ? pivot - @prewindow_days : nil
end

#includes?(date) ⇒ Boolean

Is date included in the temporal expression.

Returns:

  • (Boolean)


12
13
14
# File 'lib/texp/window.rb', line 12

def includes?(date)
  find_pivot(date) != nil
end

#inspectObject

Human readable version of the temporal expression.



41
42
43
44
45
# File 'lib/texp/window.rb', line 41

def inspect
  @term.inspect + ", " +
    "or up to #{days(@prewindow_days)} prior, " +
    "or up to #{days(@postwindow_days)} after"
end

#last_day_of_window(date) ⇒ Object

Return the first day of the window for a given date. Return nil if the date is not in a window.



25
26
27
28
# File 'lib/texp/window.rb', line 25

def last_day_of_window(date)
  pivot = find_pivot(date)
  pivot ? pivot + @postwindow_days : nil
end