Module: Runt

Included in:
ExpressionBuilder
Defined in:
lib/runt/temporalexpression.rb,
lib/runt/pdate.rb,
lib/runt/sugar.rb,
lib/runt/schedule.rb,
lib/runt/daterange.rb,
lib/runt/dprecision.rb

Overview

Author

Matthew Lipper

Defined Under Namespace

Modules: DPrecision, TExpr, TExprUtils Classes: AfterTE, BeforeTE, Collection, DIMonth, DIWeek, DateRange, DayIntervalTE, Diff, Event, EveryTE, Intersect, PDate, REDay, REMonth, REWeek, REYear, RSpec, Schedule, Spec, Union, WIMonth, YearTE

Constant Summary collapse

MONTHS =
'(january|february|march|april|may|june|july|august|september|october|november|december)'
DAYS =
'(sunday|monday|tuesday|wednesday|thursday|friday|saturday)'
WEEK_OF_MONTH_ORDINALS =
'(first|second|third|fourth|last|second_to_last)'
ORDINAL_SUFFIX =
'(?:st|nd|rd|th)'
ORDINAL_ABBR =
'(st|nd|rd|th)'

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



127
128
129
130
131
# File 'lib/runt/sugar.rb', line 127

def method_missing(name, *args, &block) 
  result = self.build(name, *args, &block)
  return result unless result.nil?
  super
end

Class Method Details

.const(string) ⇒ Object



122
123
124
# File 'lib/runt/sugar.rb', line 122

def const(string)
  self.const_get(string.capitalize)
end

Instance Method Details

#build(name, *args, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/runt/sugar.rb', line 133

def build(name, *args, &block)
  case name.to_s
  when /^daily_(\d{1,2})_(\d{2})([ap]m)_to_(\d{1,2})_(\d{2})([ap]m)$/
    # REDay
    st_hr, st_min, st_m, end_hr, end_min, end_m = $1, $2, $3, $4, $5, $6
    args = parse_time(st_hr, st_min, st_m)
    args.concat(parse_time(end_hr, end_min, end_m))
    return REDay.new(*args)
  when Regexp.new('^weekly_' + DAYS + '_to_' + DAYS + '$')
    # REWeek
    st_day, end_day = $1, $2
    return REWeek.new(Runt.const(st_day), Runt.const(end_day))
  when Regexp.new('^monthly_(\d{1,2})' + ORDINAL_SUFFIX + '_to_(\d{1,2})' \
    + ORDINAL_SUFFIX + '$')
    # REMonth
    st_day, end_day = $1, $2
    return REMonth.new(st_day, end_day)
  when Regexp.new('^yearly_' + MONTHS + '_(\d{1,2})_to_' + MONTHS + '_(\d{1,2})$')
    # REYear
    st_mon, st_day, end_mon, end_day = $1, $2, $3, $4
    return REYear.new(Runt.const(st_mon), st_day, Runt.const(end_mon), end_day)
  when Regexp.new('^' + DAYS + '$')
    # DIWeek
    return DIWeek.new(Runt.const(name.to_s))
  when Regexp.new(WEEK_OF_MONTH_ORDINALS + '_' + DAYS)
    # DIMonth
    ordinal, day = $1, $2
    return DIMonth.new(Runt.const(ordinal), Runt.const(day))
  else
    # You're hosed
    nil
  end
end

#parse_time(hour, minute, ampm) ⇒ Object



167
168
169
170
# File 'lib/runt/sugar.rb', line 167

def parse_time(hour, minute, ampm)
  hour = hour.to_i + 12 if ampm =~ /pm/
  [hour.to_i, minute.to_i]
end