Module: Runt

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

Overview

Author

Matthew Lipper

Defined Under Namespace

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

Constant Summary collapse

Sunday =

Yes it’s true, I’m a big idiot!

Date::DAYNAMES.index("Sunday")
Monday =
Date::DAYNAMES.index("Monday")
Tuesday =
Date::DAYNAMES.index("Tuesday")
Wednesday =
Date::DAYNAMES.index("Wednesday")
Thursday =
Date::DAYNAMES.index("Thursday")
Friday =
Date::DAYNAMES.index("Friday")
Saturday =
Date::DAYNAMES.index("Saturday")
Sun =
Date::ABBR_DAYNAMES.index("Sun")
Mon =
Date::ABBR_DAYNAMES.index("Mon")
Tue =
Date::ABBR_DAYNAMES.index("Tue")
Wed =
Date::ABBR_DAYNAMES.index("Wed")
Thu =
Date::ABBR_DAYNAMES.index("Thu")
Fri =
Date::ABBR_DAYNAMES.index("Fri")
Sat =
Date::ABBR_DAYNAMES.index("Sat")
January =
Date::MONTHNAMES.index("January")
February =
Date::MONTHNAMES.index("February")
March =
Date::MONTHNAMES.index("March")
April =
Date::MONTHNAMES.index("April")
May =
Date::MONTHNAMES.index("May")
June =
Date::MONTHNAMES.index("June")
July =
Date::MONTHNAMES.index("July")
August =
Date::MONTHNAMES.index("August")
September =
Date::MONTHNAMES.index("September")
October =
Date::MONTHNAMES.index("October")
November =
Date::MONTHNAMES.index("November")
December =
Date::MONTHNAMES.index("December")
First =
1
Second =
2
Third =
3
Fourth =
4
Fifth =
5
Sixth =
6
Seventh =
7
Eighth =
8
Eigth =

Will be removed in v0.9.0

8
Ninth =
9
Tenth =
10
Last =
LastProc[First]
Last_of =
LastProc[First]
Second_to_last =
LastProc[Second]
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)'
VERSION =
"0.9.0"

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/runt/sugar.rb', line 154

def method_missing(name, *args, &block) 
	#puts "method_missing(#{name},#{args},#{block}) => #{result}"
  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
 super
  end
end

Class Method Details

.const(string) ⇒ Object



149
150
151
# File 'lib/runt/sugar.rb', line 149

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

.day_name(number) ⇒ Object



62
63
64
# File 'lib/runt.rb', line 62

def day_name(number)
  Date::DAYNAMES[number]
end

.format_date(date) ⇒ Object



74
75
76
# File 'lib/runt.rb', line 74

def format_date(date)
  date.ctime
end

.format_time(date) ⇒ Object



70
71
72
# File 'lib/runt.rb', line 70

def format_time(date)
  date.strftime('%I:%M%p')
end

.month_name(number) ⇒ Object



66
67
68
# File 'lib/runt.rb', line 66

def month_name(number)
  Date::MONTHNAMES[number]
end

.ordinalize(number) ⇒ Object

Cut and pasted from activesupport-1.2.5/lib/inflector.rb



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/runt.rb', line 81

def ordinalize(number)
  if (number.to_i==-1)
	'last'
  elsif (number.to_i==-2)
	'second to last'  
  elsif (11..13).include?(number.to_i % 100)
	"#{number}th"
  else
	case number.to_i % 10
	  when 1 then "#{number}st"
	  when 2 then "#{number}nd"
	  when 3 then "#{number}rd"
	  else    "#{number}th"
	end
  end
end

Instance Method Details

#after(date, inclusive = false) ⇒ Object

Shortcut for AfterTE(date, …).new



190
191
192
# File 'lib/runt/sugar.rb', line 190

def after(date, inclusive=false)
  AfterTE.new(date, inclusive)
end

#before(date, inclusive = false) ⇒ Object

Shortcut for BeforeTE(date, …).new



195
196
197
# File 'lib/runt/sugar.rb', line 195

def before(date, inclusive=false)
  BeforeTE.new(date, inclusive)
end

#parse_time(hour, minute, ampm) ⇒ Object



199
200
201
202
# File 'lib/runt/sugar.rb', line 199

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