runt 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +107 -71
- data/LICENSE.txt +43 -43
- data/README +100 -94
- data/Rakefile +119 -120
- data/TODO +11 -20
- data/doc/tutorial_schedule.rdoc +84 -51
- data/doc/tutorial_te.rdoc +190 -190
- data/lib/runt.rb +219 -110
- data/lib/runt/daterange.rb +74 -74
- data/lib/runt/dprecision.rb +141 -137
- data/lib/runt/pdate.rb +153 -126
- data/lib/runt/schedule.rb +88 -89
- data/lib/runt/temporalexpression.rb +695 -524
- data/setup.rb +1331 -1331
- data/site/blue-robot3.css +131 -131
- data/site/index.html +94 -93
- data/site/runt-logo.psd +0 -0
- data/test/daterangetest.rb +87 -87
- data/test/dprecisiontest.rb +55 -45
- data/test/icalendartest.rb +524 -0
- data/test/pdatetest.rb +117 -104
- data/test/runttest.rb +101 -0
- data/test/scheduletest.rb +148 -88
- data/test/temporalexpressiontest.rb +612 -402
- metadata +56 -43
- data/test/alltests.rb +0 -10
data/lib/runt/dprecision.rb
CHANGED
@@ -1,137 +1,141 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'runt'
|
4
|
-
require 'date'
|
5
|
-
|
6
|
-
module Runt
|
7
|
-
|
8
|
-
# :title:DPrecision
|
9
|
-
# == DPrecision
|
10
|
-
# Module providing automatic precisioning of Date, DateTime, and PDate classes.
|
11
|
-
#
|
12
|
-
# Inspired by a <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# Author:: Matthew Lipper
|
16
|
-
module DPrecision
|
17
|
-
|
18
|
-
def DPrecision.to_p(date,prec=DEFAULT)
|
19
|
-
|
20
|
-
case prec
|
21
|
-
when MIN then PDate.min(*DPrecision.explode(date,prec))
|
22
|
-
when DAY then PDate.day(*DPrecision.explode(date,prec))
|
23
|
-
when HOUR then PDate.hour(*DPrecision.explode(date,prec))
|
24
|
-
when MONTH then PDate.month(*DPrecision.explode(date,prec))
|
25
|
-
when YEAR then PDate.year(*DPrecision.explode(date,prec))
|
26
|
-
when SEC then PDate.sec(*DPrecision.explode(date,prec))
|
27
|
-
when MILLI then raise "Not implemented."
|
28
|
-
else PDate.default(*DPrecision.explode(date,prec))
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def DPrecision.explode(date,prec)
|
33
|
-
result = [date.year,date.month,date.day]
|
34
|
-
if(date.respond_to?("hour"))
|
35
|
-
result << date.hour << date.min << date.sec
|
36
|
-
else
|
37
|
-
result << 0 << 0 << 0
|
38
|
-
end
|
39
|
-
result
|
40
|
-
end
|
41
|
-
|
42
|
-
#Simple value class for keeping track of precisioned dates
|
43
|
-
class Precision
|
44
|
-
include Comparable
|
45
|
-
|
46
|
-
attr_reader :precision
|
47
|
-
private_class_method :new
|
48
|
-
|
49
|
-
#Some constants w/arbitrary integer values used internally for comparisions
|
50
|
-
YEAR_PREC = 0
|
51
|
-
MONTH_PREC = 1
|
52
|
-
DAY_PREC = 2
|
53
|
-
HOUR_PREC = 3
|
54
|
-
MIN_PREC = 4
|
55
|
-
SEC_PREC = 5
|
56
|
-
MILLI_PREC = 6
|
57
|
-
|
58
|
-
#String values for display
|
59
|
-
LABEL = { YEAR_PREC => "YEAR",
|
60
|
-
MONTH_PREC => "MONTH",
|
61
|
-
DAY_PREC => "DAY",
|
62
|
-
HOUR_PREC => "HOUR",
|
63
|
-
MIN_PREC => "
|
64
|
-
SEC_PREC => "
|
65
|
-
MILLI_PREC => "
|
66
|
-
|
67
|
-
#Minimun values that precisioned fields get set to
|
68
|
-
FIELD_MIN = { YEAR_PREC => 1,
|
69
|
-
MONTH_PREC => 1,
|
70
|
-
DAY_PREC => 1,
|
71
|
-
HOUR_PREC => 0,
|
72
|
-
MIN_PREC => 0,
|
73
|
-
SEC_PREC => 0,
|
74
|
-
MILLI_PREC => 0}
|
75
|
-
|
76
|
-
def Precision.year
|
77
|
-
new(YEAR_PREC)
|
78
|
-
end
|
79
|
-
|
80
|
-
def Precision.month
|
81
|
-
new(MONTH_PREC)
|
82
|
-
end
|
83
|
-
|
84
|
-
def Precision.day
|
85
|
-
new(DAY_PREC)
|
86
|
-
end
|
87
|
-
|
88
|
-
def Precision.hour
|
89
|
-
new(HOUR_PREC)
|
90
|
-
end
|
91
|
-
|
92
|
-
def Precision.min
|
93
|
-
new(MIN_PREC)
|
94
|
-
end
|
95
|
-
|
96
|
-
def Precision.sec
|
97
|
-
new(SEC_PREC)
|
98
|
-
end
|
99
|
-
|
100
|
-
def Precision.millisec
|
101
|
-
new(MILLI_PREC)
|
102
|
-
end
|
103
|
-
|
104
|
-
def min_value()
|
105
|
-
FIELD_MIN[@precision]
|
106
|
-
end
|
107
|
-
|
108
|
-
def initialize(prec)
|
109
|
-
@precision = prec
|
110
|
-
end
|
111
|
-
|
112
|
-
def <=>(other)
|
113
|
-
self.precision <=> other.precision
|
114
|
-
end
|
115
|
-
|
116
|
-
def ===(other)
|
117
|
-
self.precision == other.precision
|
118
|
-
end
|
119
|
-
|
120
|
-
def to_s
|
121
|
-
"DPrecision::#{
|
122
|
-
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'runt'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
module Runt
|
7
|
+
|
8
|
+
# :title:DPrecision
|
9
|
+
# == DPrecision
|
10
|
+
# Module providing automatic precisioning of Date, DateTime, and PDate classes.
|
11
|
+
#
|
12
|
+
# Inspired by a <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# Author:: Matthew Lipper
|
16
|
+
module DPrecision
|
17
|
+
|
18
|
+
def DPrecision.to_p(date,prec=DEFAULT)
|
19
|
+
|
20
|
+
case prec
|
21
|
+
when MIN then PDate.min(*DPrecision.explode(date,prec))
|
22
|
+
when DAY then PDate.day(*DPrecision.explode(date,prec))
|
23
|
+
when HOUR then PDate.hour(*DPrecision.explode(date,prec))
|
24
|
+
when MONTH then PDate.month(*DPrecision.explode(date,prec))
|
25
|
+
when YEAR then PDate.year(*DPrecision.explode(date,prec))
|
26
|
+
when SEC then PDate.sec(*DPrecision.explode(date,prec))
|
27
|
+
when MILLI then date #raise "Not implemented."
|
28
|
+
else PDate.default(*DPrecision.explode(date,prec))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def DPrecision.explode(date,prec)
|
33
|
+
result = [date.year,date.month,date.day]
|
34
|
+
if(date.respond_to?("hour"))
|
35
|
+
result << date.hour << date.min << date.sec
|
36
|
+
else
|
37
|
+
result << 0 << 0 << 0
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
|
42
|
+
#Simple value class for keeping track of precisioned dates
|
43
|
+
class Precision
|
44
|
+
include Comparable
|
45
|
+
|
46
|
+
attr_reader :precision
|
47
|
+
private_class_method :new
|
48
|
+
|
49
|
+
#Some constants w/arbitrary integer values used internally for comparisions
|
50
|
+
YEAR_PREC = 0
|
51
|
+
MONTH_PREC = 1
|
52
|
+
DAY_PREC = 2
|
53
|
+
HOUR_PREC = 3
|
54
|
+
MIN_PREC = 4
|
55
|
+
SEC_PREC = 5
|
56
|
+
MILLI_PREC = 6
|
57
|
+
|
58
|
+
#String values for display
|
59
|
+
LABEL = { YEAR_PREC => "YEAR",
|
60
|
+
MONTH_PREC => "MONTH",
|
61
|
+
DAY_PREC => "DAY",
|
62
|
+
HOUR_PREC => "HOUR",
|
63
|
+
MIN_PREC => "MINUTE",
|
64
|
+
SEC_PREC => "SECOND",
|
65
|
+
MILLI_PREC => "MILLISECOND"}
|
66
|
+
|
67
|
+
#Minimun values that precisioned fields get set to
|
68
|
+
FIELD_MIN = { YEAR_PREC => 1,
|
69
|
+
MONTH_PREC => 1,
|
70
|
+
DAY_PREC => 1,
|
71
|
+
HOUR_PREC => 0,
|
72
|
+
MIN_PREC => 0,
|
73
|
+
SEC_PREC => 0,
|
74
|
+
MILLI_PREC => 0}
|
75
|
+
|
76
|
+
def Precision.year
|
77
|
+
new(YEAR_PREC)
|
78
|
+
end
|
79
|
+
|
80
|
+
def Precision.month
|
81
|
+
new(MONTH_PREC)
|
82
|
+
end
|
83
|
+
|
84
|
+
def Precision.day
|
85
|
+
new(DAY_PREC)
|
86
|
+
end
|
87
|
+
|
88
|
+
def Precision.hour
|
89
|
+
new(HOUR_PREC)
|
90
|
+
end
|
91
|
+
|
92
|
+
def Precision.min
|
93
|
+
new(MIN_PREC)
|
94
|
+
end
|
95
|
+
|
96
|
+
def Precision.sec
|
97
|
+
new(SEC_PREC)
|
98
|
+
end
|
99
|
+
|
100
|
+
def Precision.millisec
|
101
|
+
new(MILLI_PREC)
|
102
|
+
end
|
103
|
+
|
104
|
+
def min_value()
|
105
|
+
FIELD_MIN[@precision]
|
106
|
+
end
|
107
|
+
|
108
|
+
def initialize(prec)
|
109
|
+
@precision = prec
|
110
|
+
end
|
111
|
+
|
112
|
+
def <=>(other)
|
113
|
+
self.precision <=> other.precision
|
114
|
+
end
|
115
|
+
|
116
|
+
def ===(other)
|
117
|
+
self.precision == other.precision
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_s
|
121
|
+
"DPrecision::#{self.label}"
|
122
|
+
end
|
123
|
+
|
124
|
+
def label
|
125
|
+
LABEL[@precision]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#Pseudo Singletons:
|
130
|
+
YEAR = Precision.year
|
131
|
+
MONTH = Precision.month
|
132
|
+
DAY = Precision.day
|
133
|
+
HOUR = Precision.hour
|
134
|
+
MIN = Precision.min
|
135
|
+
SEC = Precision.sec
|
136
|
+
MILLI = Precision.millisec
|
137
|
+
DEFAULT=MIN
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
data/lib/runt/pdate.rb
CHANGED
@@ -1,126 +1,153 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'date'
|
4
|
-
require 'runt'
|
5
|
-
|
6
|
-
|
7
|
-
module Runt
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
_civil
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'runt'
|
5
|
+
|
6
|
+
|
7
|
+
module Runt
|
8
|
+
|
9
|
+
|
10
|
+
# :title:PDate
|
11
|
+
# == PDate
|
12
|
+
# Date and DateTime with explicit precision.
|
13
|
+
#
|
14
|
+
# Based the <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# Author:: Matthew Lipper
|
18
|
+
class PDate < DateTime
|
19
|
+
include DPrecision
|
20
|
+
|
21
|
+
attr_accessor :date_precision
|
22
|
+
|
23
|
+
class << self
|
24
|
+
alias_method :old_civil, :civil
|
25
|
+
|
26
|
+
def civil(*args)
|
27
|
+
precision=nil
|
28
|
+
if(args[0].instance_of?(DPrecision::Precision))
|
29
|
+
precision = args.shift
|
30
|
+
else
|
31
|
+
return PDate::sec(*args)
|
32
|
+
end
|
33
|
+
_civil = old_civil(*args)
|
34
|
+
_civil.date_precision = precision
|
35
|
+
_civil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self; alias_method :new, :civil end
|
40
|
+
|
41
|
+
def include?(expr)
|
42
|
+
eql?(expr)
|
43
|
+
end
|
44
|
+
|
45
|
+
def + (n)
|
46
|
+
raise TypeError, 'expected numeric' unless n.kind_of?(Numeric)
|
47
|
+
case @date_precision
|
48
|
+
when YEAR then
|
49
|
+
return DPrecision::to_p(PDate::civil(year+n,month,day),@date_precision)
|
50
|
+
when MONTH then
|
51
|
+
current_date = self.class.to_date(self)
|
52
|
+
return DPrecision::to_p((current_date>>n),@date_precision)
|
53
|
+
when DAY then
|
54
|
+
return new_self_plus(n)
|
55
|
+
when HOUR then
|
56
|
+
return new_self_plus(n){ |n| n = (n*(1.to_r/24) ) }
|
57
|
+
when MIN then
|
58
|
+
return new_self_plus(n){ |n| n = (n*(1.to_r/1440) ) }
|
59
|
+
when SEC then
|
60
|
+
return new_self_plus(n){ |n| n = (n*(1.to_r/86400) ) }
|
61
|
+
when MILLI then
|
62
|
+
return self
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def - (x)
|
67
|
+
case x
|
68
|
+
when Numeric then
|
69
|
+
return self+(-x)
|
70
|
+
#FIXME!!
|
71
|
+
when Date; return @ajd - x.ajd
|
72
|
+
end
|
73
|
+
raise TypeError, 'expected numeric or date'
|
74
|
+
end
|
75
|
+
|
76
|
+
def <=> (other)
|
77
|
+
result = nil
|
78
|
+
if(other.respond_to?("date_precision") && other.date_precision>@date_precision)
|
79
|
+
result = super(DPrecision::to_p(other,@date_precision))
|
80
|
+
else
|
81
|
+
result = super(other)
|
82
|
+
end
|
83
|
+
puts "#{self.to_s}<=>#{other.to_s} => #{result}" if $DEBUG
|
84
|
+
result
|
85
|
+
end
|
86
|
+
|
87
|
+
def new_self_plus(n)
|
88
|
+
if(block_given?)
|
89
|
+
n=yield(n)
|
90
|
+
end
|
91
|
+
return DPrecision::to_p(self.class.new0(@ajd + n, @of, @sg),@date_precision)
|
92
|
+
end
|
93
|
+
|
94
|
+
def PDate.to_date(pdate)
|
95
|
+
if( pdate.date_precision > DPrecision::DAY) then
|
96
|
+
DateTime.new(pdate.year,pdate.month,pdate.day,pdate.hour,pdate.min,pdate.sec)
|
97
|
+
end
|
98
|
+
return Date.new(pdate.year,pdate.month,pdate.day)
|
99
|
+
end
|
100
|
+
|
101
|
+
def PDate.year(yr,*ignored)
|
102
|
+
PDate.civil(YEAR, yr, MONTH.min_value, DAY.min_value )
|
103
|
+
end
|
104
|
+
|
105
|
+
def PDate.month( yr,mon,*ignored )
|
106
|
+
PDate.civil(MONTH, yr, mon, DAY.min_value )
|
107
|
+
end
|
108
|
+
|
109
|
+
def PDate.day( yr,mon,day,*ignored )
|
110
|
+
PDate.civil(DAY, yr, mon, day )
|
111
|
+
end
|
112
|
+
|
113
|
+
def PDate.hour( yr,mon,day,hr=HOUR.min_value,*ignored )
|
114
|
+
PDate.civil(HOUR, yr, mon, day,hr,MIN.min_value, SEC.min_value)
|
115
|
+
end
|
116
|
+
|
117
|
+
def PDate.min( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,*ignored )
|
118
|
+
PDate.civil(MIN, yr, mon, day,hr,min, SEC.min_value)
|
119
|
+
end
|
120
|
+
|
121
|
+
def PDate.sec( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,sec=SEC.min_value,*ignored )
|
122
|
+
PDate.civil(SEC, yr, mon, day,hr,min, sec)
|
123
|
+
end
|
124
|
+
|
125
|
+
def PDate.millisecond( yr,mon,day,hr,min,sec,ms,*ignored )
|
126
|
+
PDate.civil(SEC, yr, mon, day,hr,min, sec, ms, *ignored)
|
127
|
+
#raise "Not implemented yet."
|
128
|
+
end
|
129
|
+
|
130
|
+
def PDate.default(*args)
|
131
|
+
PDate.civil(DEFAULT, *args)
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Custom dump which preserves DatePrecision
|
136
|
+
#
|
137
|
+
# Author:: Jodi Showers
|
138
|
+
#
|
139
|
+
def marshal_dump
|
140
|
+
[date_precision, ajd, sg, of]
|
141
|
+
end
|
142
|
+
|
143
|
+
#
|
144
|
+
# Custom load which preserves DatePrecision
|
145
|
+
#
|
146
|
+
# Author:: Jodi Showers
|
147
|
+
#
|
148
|
+
def marshal_load(dumped_obj)
|
149
|
+
@date_precision, @ajd, @sg, @of=dumped_obj
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|