Class: TExp::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/texp/base.rb,
lib/texp/operators.rb

Overview

Abstract Base class for all Texp Temporal Expressions.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.encoding_tokenObject (readonly)

The token to be used for encoding this temporal expression.



198
199
200
# File 'lib/texp/base.rb', line 198

def encoding_token
  @encoding_token
end

Class Method Details

.parse_callback(stack) ⇒ Object

The default parsing callback for single argument time expressions. Override if you need anything more complicated.



209
210
211
# File 'lib/texp/base.rb', line 209

def parse_callback(stack)
  stack.push new(stack.pop)
end

.register_parse_callback(token, callback = self) ⇒ Object

Register a parse callack for the encoding token for this class.



202
203
204
205
# File 'lib/texp/base.rb', line 202

def register_parse_callback(token, callback=self)
  @encoding_token = token if callback == self
  TExp.register_parse_callback(token, callback)
end

Instance Method Details

#*(texp) ⇒ Object

Combine two temporal expressions so that the result will match the intersection of the dates matched by the individual temporal expressions.

Examples:

month("Feb") * day(14) # Match the 14th of February (in any year)


24
25
26
# File 'lib/texp/operators.rb', line 24

def *(texp)
  TExp::And.new(self, texp)
end

#+(texp) ⇒ Object

Combine two temporal expressions so that the result will match the union of the dates matched by the individual temporal expressions.

Examples:

dow(:monday) + dow(:tuesday) # Match any date falling on Monday or Tuesday


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

def +(texp)
  TExp::Or.new(self, texp)
end

#-(texp) ⇒ Object

Combine two temporal expressions so that the result will match the any date matched by left expression except for dates matched by the right expression.

Examples:

month("Feb") - dow(:mon)    # Match any day in February except Mondays


36
37
38
# File 'lib/texp/operators.rb', line 36

def -(texp)
  TExp::And.new(self, TExp::Not.new(texp))
end

#-@Object

Return a new temporal expression that negates the sense of the current expression. In other words, match everything the current expressions does not match and don’t match anything that it does.

Examples:

-dow(:mon)    # Match everything but Mondays


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

def -@()
  TExp::Not.new(self)
end

#each {|_self| ... } ⇒ Object

Iterate over all temporal expressions and subexpressions (in post order).

Yields:

  • (_self)

Yield Parameters:

  • _self (TExp::Base)

    the object that the method was called on



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

def each()                  # :yield: temporal_expression
  yield self
end

#first_day_of_window(date) ⇒ Object

Return the first day of the window for the temporal expression. If the temporal expression is not a windowed expression, then the first day of the window is the given date.



24
25
26
# File 'lib/texp/base.rb', line 24

def first_day_of_window(date)
  includes?(date) ? date : nil
end

#last_day_of_window(date) ⇒ Object

Return the last day of the window for the temporal expression. If the temporal expression is not a windowed expression, then the last day of the window is the given date.



31
32
33
# File 'lib/texp/base.rb', line 31

def last_day_of_window(date)
  includes?(date) ? date : nil
end

#reanchor(date) ⇒ Object

Create a new temporal expression with a new anchor date.



17
18
19
# File 'lib/texp/base.rb', line 17

def reanchor(date)
  self
end

#to_sObject

Convert the temporal expression into an encoded string (that can be parsed by TExp.parse).



10
11
12
13
14
# File 'lib/texp/base.rb', line 10

def to_s
  codes = []
  encode(codes)
  codes.join("")
end

#window(*args) ⇒ Object

:call-seq:

window(days)
window(predays, postdays)
window(n, units)
window(pre, pre_units, post, post_units)

Create a new temporal expression that matches a window around any date matched by the current expression.

If a single numeric value is given, then a symetrical window of the given number of days is created around each date matched by the current expression. If a symbol representing units is given in addition to the numeric, then the appropriate scale factor is applied to the numeric value.

If two numberic values are given (with or without unit symbols), then the window will be asymmetric. The firsts numeric value will be the pre-window, and the second numeric value will be the post window.

The following unit symbols are recognized:

  • :day, :days (scale by 1)

  • :week, :weeks (scale by 7)

  • :month, :months (scale by 30)

  • :year, :years (scale by 365)

Examples:

texp.window(3)         # window of 3 days on either side
texp.window(3, :days)  # window of 3 days on either side
texp.window(1, :week)  # window of 1 week on either side
texp.window(3, :days, 2, :weeks)
                       # window of 3 days before any match,
                       # and 2 weeks after any match.


77
78
79
80
81
# File 'lib/texp/base.rb', line 77

def window(*args)
  prewindow, postwindow = TExp.normalize_units(args)
  postwindow ||= prewindow
  TExp::Window.new(self, prewindow, postwindow)
end