Module: TExp::DSL

Included in:
TExp, EvalEnvironment
Defined in:
lib/texp/dsl.rb

Overview

DSL methods are available as methods on TExp (e.g. TExp.day()). Alternatively, you can include the TExp::Builder module into whatever namespace to get direct access to these methods.

Instance Method Summary collapse

Instance Method Details

#day(*days_of_month) ⇒ Object

Return a temporal expression that matches any date that falls on a day of the month given in the argument list. Examples:

day(1)       # Match any date that falls on the 1st of any month
day(1, 15)   # Match any date that falls on the 1st or 15th of any month


15
16
17
# File 'lib/texp/dsl.rb', line 15

def day(*days_of_month)
  TExp::DayOfMonth.new(days_of_month)
end

#dow(*dow) ⇒ Object

Return a temporal expression matching the given days of the week.

Examples:

dow(2)                 # Match any date on a Tuesday
dow("Tuesday")         # Match any date on a Tuesday
dow(:mon, :wed, :fr)   # Match any date on a Monday, Wednesday or Friday


155
156
157
# File 'lib/texp/dsl.rb', line 155

def dow(*dow)
  TExp::DayOfWeek.new(normalize_dows(dow))
end

#evaluate_expression_in_environment(&block) ⇒ Object

Evaluate a temporal expression in the TExp environment. Redirect missing method calls to the containing environment.



166
167
168
169
# File 'lib/texp/dsl.rb', line 166

def evaluate_expression_in_environment(&block) # :nodoc:
  env = EvalEnvironment.new(block)
  env.instance_eval(&block)
end

#every(n, unit, start_date = Date.today) ⇒ Object



159
160
161
162
# File 'lib/texp/dsl.rb', line 159

def every(n, unit, start_date=Date.today)
  value = apply_units(unit, n)
  TExp::DayInterval.new(start_date, value)
end

#month(*month) ⇒ Object

Return a temporal expression that matches any date in the list of given months.

Examples:

month(2)              # Match any date in February
month(2, 8)           # Match any date in February or August
month("February")     # Match any date in February
month("Sep", "Apr", "Jun", "Nov")
                      # Match any date in any month with 30 days.


52
53
54
# File 'lib/texp/dsl.rb', line 52

def month(*month)
  TExp::Month.new(normalize_months(month))
end

#normalize_units(args) ⇒ Object

:nodoc:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/texp/dsl.rb', line 171

def normalize_units(args)   # :nodoc:
  result = []
  while ! args.empty?
    arg = args.shift
    case arg
    when Numeric
      result.push(arg)
    when Symbol
      result.push(apply_units(arg, result.pop))
    else
      fail ArgumentError, "Unabled to recognize #{arg.inspect}"
    end
  end
  result
end

#on(*args) ⇒ Object

:call-seq:

on(day, month)
on(day, month_string)
on(day, month, year)
on(day, month_string, year)
on(date)
on(date_string)
on(time)
on(object_with_to_date)
on(object_with_to_s)

Return a temporal expression that matches a particular date of the year. The temporal expression will be pinned to a particular year if a year is given (either explicity as a parameter or implicitly via a Date object). If no year is given, then the temporal expression will match that date in any year.

If only a single argument is given, then the argument may be a string (which is parsed), a Date, a Time, or an object that responds to to_date. If the single argument is none of the above, then it is converted to a string (via to_s) and given to Date.parse().

Invalid arguments will cause on to throw an ArgumentError exception.

Examples:

The following examples all match Feb 14 of any year.

on(14, 2)
on(14, "February")
on(14, "Feb")
on(14, :feb)

The following examples all match Feb 14 of the year 2008.

on(14, 2, 2008)
on(14, "February", 2008)
on(14, "Feb", 2008)
on(14, :feb, 2008)
on("Feb 14, 2008")
on(Date.new(2008, 2, 14))


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/texp/dsl.rb', line 112

def on(*args)
  if args.size == 1
    arg = args.first
    case arg
    when String
      date = Date.parse(arg)
    when Date
      date = arg
    else
      if arg.respond_to?(:to_date)
        date = arg.to_date
      else
        date = try_parsing(arg.to_s)
      end
    end
    on(date.day, date.month, date.year)
  elsif args.size == 2
    day, month = dm_args(args)
    TExp::And.new(
      TExp::DayOfMonth.new(day),
      TExp::Month.new(month))
  elsif args.size == 3
    day, month, year = dmy_args(args)
    TExp::And.new(
      TExp::DayOfMonth.new(day),
      TExp::Month.new(normalize_month(month)),
      TExp::Year.new(year))
  else
    fail DateArgumentError
  end
rescue DateArgumentError
  fail ArgumentError, "Invalid arguents for on(): #{args.inspect}"
end

#week(*weeks) ⇒ Object

Return a temporal expression that matches any date in the specified week of the month. Days 1 through 7 are considered the first week of the month; days 8 through 14 are the second week; and so on.

The week is specified by a numeric argument. Negative arguments are calculated from the end of the month (e.g. -1 would request the last 7 days of the month). The symbols :first, :second, :third, :fourth, :fifth, and :last are also recognized.

Examples:

week(1)       # Match any date in the first 7 days of any month.
week(1, 2)    # Match any date in the first or second 7 days of any month.
week(:first)  # Match any date in the first 7 days of any month.
week(:last)   # Match any date in the last 7 days of any month.


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

def week(*weeks)
  TExp::Week.new(normalize_weeks(weeks))
end

#year(*years) ⇒ Object

Return a temporal expression that matches the given list of years.

Examples:

year(2008)              # Match any date in 2008
year(2000, 2004, 2008)  # Match any date in any of the three years


63
64
65
# File 'lib/texp/dsl.rb', line 63

def year(*years)
  TExp::Year.new(years)
end