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, RSpec, Schedule, Spec, Union, WIMonth, 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.7.7"

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

.day_name(number) ⇒ Object



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

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

.format_date(date) ⇒ Object



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

def format_date(date)
  date.ctime
end

.format_time(date) ⇒ Object



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

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

.month_name(number) ⇒ Object



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

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

.ordinalize(number) ⇒ Object

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



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

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

#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