Class: TExp::Base
- Inherits:
-
Object
- Object
- TExp::Base
- Includes:
- Enumerable
- Defined in:
- lib/texp/base.rb,
lib/texp/operators.rb
Overview
Abstract Base class for all Texp Temporal Expressions.
Direct Known Subclasses
DayInterval, DayOfMonth, DayOfWeek, EveryDay, Month, MultiTermBase, SingleTermBase, Week, Year
Class Attribute Summary collapse
-
.encoding_token ⇒ Object
readonly
The token to be used for encoding this temporal expression.
Class Method Summary collapse
-
.parse_callback(stack) ⇒ Object
The default parsing callback for single argument time expressions.
-
.register_parse_callback(token, callback = self) ⇒ Object
Register a parse callack for the encoding token for this class.
Instance Method Summary collapse
-
#*(texp) ⇒ Object
Combine two temporal expressions so that the result will match the intersection of the dates matched by the individual temporal expressions.
-
#+(texp) ⇒ Object
Combine two temporal expressions so that the result will match the union of the dates matched by the individual temporal expressions.
-
#-(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.
-
#-@ ⇒ Object
Return a new temporal expression that negates the sense of the current expression.
-
#each {|_self| ... } ⇒ Object
Iterate over all temporal expressions and subexpressions (in post order).
-
#first_day_of_window(date) ⇒ Object
Return the first day of the window for the temporal expression.
- #include?(*args) ⇒ Boolean
-
#last_day_of_window(date) ⇒ Object
Return the last day of the window for the temporal expression.
-
#reanchor(date) ⇒ Object
Create a new temporal expression with a new anchor date.
-
#to_s ⇒ Object
Convert the temporal expression into an encoded string (that can be parsed by TExp.parse).
-
#window(*args) ⇒ Object
:call-seq: window(days) window(predays, postdays) window(n, units) window(pre, pre_units, post, post_units).
Class Attribute Details
.encoding_token ⇒ Object (readonly)
The token to be used for encoding this temporal expression.
208 209 210 |
# File 'lib/texp/base.rb', line 208 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.
219 220 221 |
# File 'lib/texp/base.rb', line 219 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.
212 213 214 215 |
# File 'lib/texp/base.rb', line 212 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).
47 48 49 |
# File 'lib/texp/base.rb', line 47 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.
34 35 36 |
# File 'lib/texp/base.rb', line 34 def first_day_of_window(date) includes?(date) ? date : nil end |
#include?(*args) ⇒ Boolean
22 23 24 |
# File 'lib/texp/base.rb', line 22 def include?(*args) raise TExpIncludeError, "Use includes? rather than include?" 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.
41 42 43 |
# File 'lib/texp/base.rb', line 41 def last_day_of_window(date) includes?(date) ? date : nil end |
#reanchor(date) ⇒ Object
Create a new temporal expression with a new anchor date.
27 28 29 |
# File 'lib/texp/base.rb', line 27 def reanchor(date) self end |
#to_s ⇒ Object
Convert the temporal expression into an encoded string (that can be parsed by TExp.parse).
16 17 18 19 20 |
# File 'lib/texp/base.rb', line 16 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.
87 88 89 90 91 |
# File 'lib/texp/base.rb', line 87 def window(*args) prewindow, postwindow = TExp.normalize_units(args) postwindow ||= prewindow TExp::Window.new(self, prewindow, postwindow) end |