source: ruby/branches/0.5/lib/open_ehr/assumed_library_types.rb@ 256

Last change on this file since 256 was 256, checked in by KOBAYASHI, Shinji, 15 years ago

assumed_library_types move test/unit to rspec

File size: 14.7 KB
RevLine 
[83]1# This module is related to the ticket #36
[70]2require 'date'
[81]3require 'time'
[70]4
[216]5module OpenEHR
[167]6 module AssumedLibraryTypes
[69]7 class Any < Object
8
9 end # of Any
[143]10
[69]11 class Interval < Any
[143]12 attr_reader :lower, :upper
13
[244]14 def initialize(args = {})
15 check_lower_upper(args[:lower], args[:upper])
16 self.lower_included = args[:lower_included]
17 self.upper_included = args[:upper_included]
[4]18 end
[21]19
[143]20 def lower=(lower)
[4]21 check_lower_upper(lower, @upper)
22 end
[21]23
[143]24 def upper=(upper)
[4]25 check_lower_upper(@lower, upper)
26 end
[21]27
[143]28 def lower_included?
29 return @lower_included
30 end
31
32 def lower_included=(lower_included)
[4]33 if (lower == nil) && (lower_included != nil)
34 raise ArgumentError, "lower is not set"
35 end
36 @lower_included = lower_included
37 end
[143]38
39 def lower_unbounded?
40 return @lower.nil?
41 end
[21]42
[143]43 def upper_included?
44 return @upper_included
45 end
46
47 def upper_included=(upper_included)
[255]48 if (@upper.nil?) && (upper_included != nil)
[4]49 raise ArgumentError, "upper is not set"
50 end
[143]51 @upper_included = upper_included
[4]52 end
[21]53
[143]54 def upper_unbounded?
55 return @upper.nil?
56 end
57
[4]58 def has?(value)
[255]59 if ((@lower.nil?||@lower < value||((@lower_included == true) && (@lower == value)))&&
60 (@upper.nil?||value < @upper||((@upper_included == true) && (@upper == value))))
[4]61 true
62 else
63 false
64 end
65 end
[21]66
[4]67 private
[21]68
[4]69 def check_lower_upper(lower, upper)
[244]70 if lower.nil? && upper.nil?
[4]71 raise ArgumentError, "Either lower or upper must be assigned"
72 end
[143]73 unless (lower.nil? || upper.nil?)
74 if lower > upper
75 raise ArgumentError, "Upper must be larger than lower."
76 end
[4]77 end
78 @lower = lower
79 @upper = upper
[143]80 end
[69]81 end # end of Interval
[143]82
[256]83 class TimeDefinitions < Any
[69]84 DAYS_IN_LEAP_YEAR = 366
85 DAYS_IN_WEEK = 7
86 DAYS_IN_YEAR = 365
87 HOURS_IN_DAY = 24
88 MAX_DAYS_IN_MONTH = 31
89 MAX_DAYS_IN_YEAR = 366
90 MINUTES_IN_HOUR = 60
91 MONTH_IN_YEAR = 12
92 NOMINAL_DAYS_IN_MONTH = 30.42
93 NOMINAL_DAYS_IN_YEAR = 365.24
94 SECONDS_IN_MINUTE = 60
[70]95
96 def self.valid_year?(year)
97 year >= 0
98 end
99
100 def self.valid_day?(y, m, d)
101 Date.valid_date?(y,m,d) and valid_year? y
102 end
103
[83]104 def self.valid_hour?(h,m = nil, s = nil)
105 if !m.nil? and !valid_minute?(m)
106 return false
107 end
108 if !s.nil? and (!m.nil? and !valid_second?(s))
109 return false
110 end
111 (h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0)
[70]112 end
[81]113 def self.valid_minute?(mi)
114 mi >= 0 and mi < MINUTES_IN_HOUR
[70]115 end
116 def self.valid_second?(s)
117 s >= 0 and s < SECONDS_IN_MINUTE
118 end
[81]119 def self.valid_month?(mo)
120 mo >= 1 and mo <= MONTH_IN_YEAR
[70]121 end
[256]122 end # end of TimeDefinitions
[69]123
[256]124 module ISO8601DateModule
[70]125 attr_reader :year, :month, :day
[256]126
[70]127 def year=(year)
[256]128 unless !year.nil? && ISO8601Date.valid_year?(year)
129 raise ArgumentError, "Year is not valid"
130 end
[70]131 @year = year
132 end
[256]133
[70]134 def month=(month)
[256]135 raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601Date.valid_month?(month)
[70]136 @month = month
137 end
[125]138
[70]139 def day=(day)
[256]140 raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601Date.valid_day?(@year, @month, day)
[70]141 @day = day
142 end
[125]143
[70]144 def as_string
145 if (!@year.nil? and !@month.nil? and !@day.nil?)
146 Date.new(@year, @month, @day).to_s
147 elsif (!@year.nil? and !@month.nil? and @day.nil?)
148 Date.new(@year, @month).to_s[0,7]
149 elsif (!@year.nil? and @month.nil? and @day.nil?)
150 Date.new(@year).to_s[0,4]
151 end
152 end
[125]153
[70]154 def month_unknown?
155 @month.nil?
156 end
[125]157
[70]158 def day_unknown?
159 @day.nil?
160 end
[125]161
[70]162 def is_extended?
163 true
164 end
[125]165
[70]166 def is_partial?
167 month_unknown? or day_unknown?
168 end
[149]169
170 protected
171 def leapyear?(year)
172 case
173 when year % 400 == 0: true
174 when year % 100 == 0: false
175 else year % 4 == 0
176 end
177 end
[120]178 end
179
[256]180 class ISO8601Date < TimeDefinitions
181 include ISO8601DateModule
[120]182 def initialize(string)
183 /(\d{4})(?:-(\d{2})(?:-(\d{2})?)?)?/ =~ string
184 if $1.nil?
185 raise ArgumentError, 'data invalid'
186 else
187 self.year = $1.to_i
188 end
189 if $2.nil?
190 self.month = nil
191 else
192 self.month = $2.to_i
193 end
194 if $3.nil?
195 self.day = nil
196 else
197 self.day = $3.to_i
198 end
199 end
[149]200
[81]201 def self.valid_iso8601_date?(string)
[70]202 begin
[81]203 Date.parse(string)
[70]204 rescue
205 return false
206 end
207 true
208 end
[79]209 end # end of ISO8601_DATE
210
[256]211 module ISO8601TimeModule
[83]212 attr_reader :hour, :minute, :second, :fractional_second, :timezone
[120]213
[83]214 def hour=(hour)
215 raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second)
216 @hour = hour
217 end
[124]218
[83]219 def minute_unknown?
220 @minute.nil?
221 end
[124]222
[83]223 def minute=(minute)
224 raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601_TIME.valid_minute?(minute)
225 @minute = minute
226 end
[124]227
[83]228 def second_unknown?
229 @second.nil?
230 end
[124]231
[83]232 def second=(second)
[120]233 raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil?
[83]234 raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601_TIME.valid_second?(second)
235 @second = second
236 end
[124]237
[83]238 def fractional_second=(fractional_second)
[120]239 raise ArgumentError, "minute not defined" if minute_unknown? and !fractional_second.nil?
240 raise ArgumentError, "second not defined" if second_unknown? and !fractional_second.nil?
241 raise ArgumentError, "fractional second should be lower than 1.0" if !fractional_second.nil? and fractional_second >= 1.0
[83]242 @fractional_second = fractional_second
243 end
[124]244
[83]245 def has_fractional_second?
246 if @fractional_second.nil?
247 return false
248 else
249 return true
250 end
251 end
[124]252
[120]253 def timezone=(timezone)
254 unless timezone.nil? or timezone == 'Z'
255 if /[+-](\d{2}):?(\d{2})/ =~ timezone
256 @timezone = timezone
257 else
258 raise ArgumentError, "timezone invalid"
259 end
260 else
261 @timezone = nil
262 end
263 end
[124]264
[83]265 def is_decimal_sign_comma?
[126]266 false
[83]267 end
[124]268
[83]269 def is_extended?
270 true
271 end
[124]272
[83]273 def is_partial?
274 second_unknown? or minute_unknown?
275 end
[125]276
[81]277 def as_string
[83]278 s = sprintf("%02d", @hour)
279 if !@minute.nil?
280 s += ":" + sprintf("%02d",@minute)
281 if !@second.nil?
282 s += ":" + sprintf("%02d", @second)
283 if !@fractional_second.nil?
[125]284 s += "." + @fractional_second.to_s[2..-1]
[83]285 if !@timezone.nil?
286 s += @timezone
[81]287 end
288 end
289 end
290 end
[125]291 return s
[81]292 end
[120]293 end
294
[256]295 class ISO8601Time < TimeDefinitions
296 include ISO8601TimeModule
[120]297 def initialize(string)
298 /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ string
299 if $2.nil?
300 self.minute = nil
301 else
302 self.minute = $2.to_i
303 end
304 if $4.nil?
305 self.second = nil
306 else
307 self.second = $4.to_i
308 end
309 if $1.nil?
310 raise ArgumentError, 'data invalid'
311 else
312 self.hour = $1.to_i
313 end
314 if $7.nil?
315 self.fractional_second = nil
316 else
317 self.fractional_second = ("0." + $7).to_f
318 end
319 if $8.nil?
320 self.timezone = nil
321 else
322 self.timezone = $8
323 end
324 end
[81]325 def self.valid_iso8601_time?(s)
326 if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
327# ISO 8601 regular expression by H. Yuki
328# http://digit.que.ne.jp/work/wiki.cgi?Perl%E3%83%A1%E3%83%A2%2FW3C%E5%BD%A2%E5%BC%8F%E3%81%AE%E6%97%A5%E6%99%82%E3%81%AE%E8%A7%A3%E6%9E%90
329# (\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?
330 hh = $1; mm = $2; ss = $4; msec = $7; tz = $8
[83]331 if hh.to_i == HOURS_IN_DAY and (mm.nil? or mm.to_i == 0) and (ss.nil? or ss.to_i == 0) and (msec.nil? or msec.to_i==0)
[81]332 return true
333 end
334 if hh.nil? or (hh.to_i < 0 or hh.to_i >= HOURS_IN_DAY)
335 return false
336 end
337 if !mm.nil?
338 if !self.valid_minute?(mm.to_i)
339 return false
340 end
341 end
342 if !ss.nil?
343 if !self.valid_second?(ss.to_i)
344 return false
345 end
346 end
347 if !tz.nil? and tz != "Z"
348 if /[+-](\d{2}):?(\d{2})/ =~ tz
349 h = $1; m = $2
350 if h.to_i < 0 or h.to_i >= HOURS_IN_DAY
351 return false
352 end
353 if m.to_i < 0 or m.to_i >= MINUTES_IN_HOUR
354 return false
355 end
356 else
357 return false
358 end
359 end
360 return true
361 else
362 return false
363 end
364 end
[79]365 end # end of ISO8601_TIME
366
[256]367 module ISO8601DateTimeModule
368 include ISO8601DateModule, ISO8601TimeModule
[149]369 def as_string
370 if (!@year.nil? and !@month.nil? and !@day.nil?)
371 s = Date.new(@year, @month, @day).to_s
372 elsif (!@year.nil? and !@month.nil? and @day.nil?)
373 return Date.new(@year, @month).to_s[0,7]
374 elsif (!@year.nil? and @month.nil? and @day.nil?)
375 return Date.new(@year).to_s[0,4]
376 end
377 unless hour.nil?
378 s += sprintf("T%02d", @hour)
379 unless @minute.nil?
380 s += ":" + sprintf("%02d",@minute)
381 unless @second.nil?
382 s += ":" + sprintf("%02d", @second)
383 unless @fractional_second.nil?
384 s += "." + @fractional_second.to_s[2..-1]
385 unless @timezone.nil?
386 s += @timezone
387 end
388 end
389 end
390 end
391 end
392 return s
393 end
394 end
395
[256]396 class ISO8601DateTime < ISO8601Date
397 include ISO8601DateTimeModule
[120]398 def initialize(string)
[121]399 /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string
[120]400 if $1.empty?
401 raise ArgumentError, 'format invalid'
402 else
403 self.year = $1.to_i
404 end
405 if $2.nil?
406 self.month = nil
407 else
408 self.month = $2.to_i
409 end
410 if $3.nil?
411 self.day = nil
412 else
413 self.day = $3.to_i
414 end
415 if $5.nil?
416 self.minute = nil
417 else
418 self.minute = $5.to_i
419 end
420 if $6.nil?
421 self.second = nil
422 else
423 self.second = $6.to_i
424 end
425 if $4.nil?
426 self.hour = nil
427 else
428 self.hour = $4.to_i
429 end
[121]430 if $7.nil? or $7.empty?
[120]431 self.fractional_second = nil
432 else
433 self.fractional_second = ("0."+$7).to_f
434 end
435 if $8.nil?
436 self.timezone = nil
437 else
[121]438 self.timezone = $9+$10
[120]439 end
440 end
441 end
[149]442
[79]443 class ISO8601_TIMEZONE
444 attr_accessor :sign, :hour, :minute
[124]445
[79]446 def is_gmt?
[80]447 @sign == "+1" and @hour == 0 and @minute == 0
[79]448 end
[124]449
[79]450 def as_string
[80]451 if @sign == "+1"
452 s = "+"
453 elsif @sign == "-1"
454 s = "-"
455 end
456 sprintf("Z%s%02d%02d", s, @hour, @minute)
[79]457 end
458 end # end of ISO8601_TIMEZONE
[123]459
[149]460 module ISO8601_DURATION_MODULE
[126]461 attr_reader :years, :months, :weeks, :days
462 attr_reader :hours, :minutes, :seconds, :fractional_second
463
464 def years=(years)
[128]465 unless years.nil? || years >= 0
[126]466 raise ArgumentError, 'years must be above zero'
467 end
468 @years = years
469 end
[124]470
[126]471 def months=(months)
[128]472 unless months.nil? || months >= 0
[126]473 raise ArgumentError, 'months must be above zero'
474 end
475 @months = months
[124]476 end
[126]477
478 def weeks=(weeks)
[128]479 unless weeks.nil? || weeks >= 0
[126]480 raise ArgumentError, 'weeks must be above zero'
481 end
482 @weeks = weeks
483 end
484
485 def days=(days)
[128]486 unless days.nil? || days >= 0
[126]487 raise ArgumentError, 'days must be above zero'
488 end
489 @days = days
490 end
491
492 def hours=(hours)
[128]493 unless hours.nil? || hours >= 0
[126]494 raise ArgumentError, 'hours must be above zero'
495 end
496 @hours = hours
497 end
498
499 def minutes=(minutes)
[128]500 unless minutes.nil? || minutes >= 0
[126]501 raise ArgumentError, 'minutes must be above zero'
502 end
503 @minutes = minutes
504 end
505
506 def seconds=(seconds)
[128]507 unless seconds.nil? || seconds >= 0
[126]508 raise ArgumentError, 'seconds must be above zero'
509 end
510 @seconds = seconds
511 end
512
513 def fractional_second=(fractional_second)
[128]514 unless fractional_second.nil? || (fractional_second >= 0 && fractional_second < 1.0)
[126]515 raise ArgumentError, 'fractional_second must be between 0.0 and 1.0'
516 end
517 @fractional_second = fractional_second
518 end
519
520 def as_string
521 str = 'P'
522 unless @years.nil?
523 str += @years.to_s + 'Y'
524 end
525 unless @months.nil?
526 str += @months.to_s + 'M'
527 end
528 unless @weeks.nil?
529 str += @weeks.to_s + 'W'
530 end
531 unless @days.nil?
532 str += @days.to_s + 'D'
533 end
534 unless @hours.nil?
535 str += 'T' + @hours.to_s + 'H'
536 unless @minutes.nil?
537 str += @minutes.to_s + 'M'
538 unless @seconds.nil?
539 str += @seconds.to_s
540 unless @fractional_second.nil?
541 str += @fractional_second.to_s[1 .. -1]
542 end
543 str += 'S'
544 end
545 end
546 end
547 return str
548 end
[149]549 end
[256]550 class ISO8601_DURATION < TimeDefinitions
[149]551 include ISO8601_DURATION_MODULE
552 def initialize(str)
553 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str
554 self.years = $2.to_i
555 self.months = $4.to_i
556 self.weeks = $6.to_i
557 self.days = $8.to_i
558 self.hours = $11.to_i
559 self.minutes = $13.to_i
560 self.seconds = $15.to_i
561 self.fractional_second = $16.to_f
562 end
[124]563 end # end of ISO8601_DURATION
[69]564 end # end of Assumed_Types
565end # end of OpenEHR
Note: See TracBrowser for help on using the repository browser.