Changeset 297 for ruby/trunk/lib/open_ehr/assumed_library_types.rb
- Timestamp:
- Sep 25, 2009, 12:15:59 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/open_ehr/assumed_library_types.rb
r167 r297 3 3 require 'time' 4 4 5 module OpenE hr5 module OpenEHR 6 6 module AssumedLibraryTypes 7 7 class Any < Object … … 12 12 attr_reader :lower, :upper 13 13 14 def initialize(lower, upper, 15 lower_included = nil, upper_included = nil) 16 check_lower_upper(lower, upper) 17 self.lower_included = lower_included 18 self.upper_included = upper_included 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] 19 18 end 20 19 … … 47 46 48 47 def upper_included=(upper_included) 49 if ( upper == nil) && (upper_included != nil)48 if (@upper.nil?) && (upper_included != nil) 50 49 raise ArgumentError, "upper is not set" 51 50 end … … 58 57 59 58 def has?(value) 60 if ((@lower < value) && (value < @upper) || 61 (@lower_included == true) && (@lower == value) || 62 (@upper_included == true) && (@upper == value)) 59 if ((@lower.nil?||@lower < value||((@lower_included == true) && (@lower == value)))&& 60 (@upper.nil?||value < @upper||((@upper_included == true) && (@upper == value)))) 63 61 true 64 62 else … … 70 68 71 69 def check_lower_upper(lower, upper) 72 if (lower == nil) && (upper == nil)70 if lower.nil? && upper.nil? 73 71 raise ArgumentError, "Either lower or upper must be assigned" 74 72 end … … 83 81 end # end of Interval 84 82 85 class T IME_DEFINITIONS< Any83 class TimeDefinitions < Any 86 84 DAYS_IN_LEAP_YEAR = 366 87 85 DAYS_IN_WEEK = 7 … … 97 95 98 96 def self.valid_year?(year) 99 year >= 097 return !year.nil? && year >= 0 100 98 end 101 99 102 100 def self.valid_day?(y, m, d) 103 Date.valid_date?(y,m,d) and valid_year? y 101 unless y.nil? || m.nil? || d.nil? 102 return Date.valid_date?(y,m,d) 103 end 104 if (y.nil?) || (m.nil? && !d.nil?) 105 return false 106 end 107 return self.valid_year?(y) && self.valid_month?(m) 104 108 end 105 109 106 110 def self.valid_hour?(h,m = nil, s = nil) 111 if h.nil? 112 return false 113 end 107 114 if !m.nil? and !valid_minute?(m) 108 115 return false … … 113 120 (h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0) 114 121 end 122 115 123 def self.valid_minute?(mi) 116 124 mi >= 0 and mi < MINUTES_IN_HOUR 117 125 end 126 118 127 def self.valid_second?(s) 119 128 s >= 0 and s < SECONDS_IN_MINUTE 120 129 end 130 121 131 def self.valid_month?(mo) 122 132 mo >= 1 and mo <= MONTH_IN_YEAR 123 133 end 124 end # end of T IME_DEFINITIONS125 126 module ISO8601 _DATE_MODULE134 end # end of TimeDefinitions 135 136 module ISO8601DateModule 127 137 attr_reader :year, :month, :day 138 128 139 def year=(year) 129 raise ArgumentError, "Year is not valid" unless ISO8601_DATE.valid_year?(year) 140 unless ISO8601Date.valid_year?(year) 141 raise ArgumentError, "Year is not valid" 142 end 130 143 @year = year 131 144 end 145 132 146 def month=(month) 133 raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601 _DATE.valid_month?(month)147 raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601Date.valid_month?(month) 134 148 @month = month 135 149 end 136 150 137 151 def day=(day) 138 raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601_DATE.valid_day?(@year, @month, day) 152 153 raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601Date.valid_day?(@year, @month, day) 139 154 @day = day 140 155 end … … 176 191 end 177 192 178 class ISO8601 _DATE < TIME_DEFINITIONS179 include ISO8601 _DATE_MODULE193 class ISO8601Date < TimeDefinitions 194 include ISO8601DateModule 180 195 def initialize(string) 181 196 /(\d{4})(?:-(\d{2})(?:-(\d{2})?)?)?/ =~ string … … 207 222 end # end of ISO8601_DATE 208 223 209 module ISO8601 _TIME_MODULE224 module ISO8601TimeModule 210 225 attr_reader :hour, :minute, :second, :fractional_second, :timezone 211 226 212 227 def hour=(hour) 213 raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second) 228 unless ISO8601Time.valid_hour?(hour, @minute, @second) 229 raise ArgumentError, "hour is not valid" 230 end 214 231 @hour = hour 215 232 end … … 220 237 221 238 def minute=(minute) 222 raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601 _TIME.valid_minute?(minute)239 raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601Time.valid_minute?(minute) 223 240 @minute = minute 224 241 end … … 230 247 def second=(second) 231 248 raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil? 232 raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601 _TIME.valid_second?(second)249 raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601Time.valid_second?(second) 233 250 @second = second 234 251 end … … 237 254 raise ArgumentError, "minute not defined" if minute_unknown? and !fractional_second.nil? 238 255 raise ArgumentError, "second not defined" if second_unknown? and !fractional_second.nil? 239 raise ArgumentError, "fractional second should be lower than 1.0" if !fractional_second.nil? and fractional_second >= 1.0 256 if !fractional_second.nil? && 257 (fractional_second < 0.0 || fractional_second >= 1.0) 258 raise ArgumentError, 'fractional second should be between 0.0 - 1.0' 259 end 240 260 @fractional_second = fractional_second 241 261 end 242 262 243 263 def has_fractional_second? 244 if @fractional_second.nil? 245 return false 246 else 247 return true 248 end 264 return !@fractional_second.nil? 249 265 end 250 266 … … 291 307 end 292 308 293 class ISO8601 _TIME < TIME_DEFINITIONS294 include ISO8601 _TIME_MODULE309 class ISO8601Time < TimeDefinitions 310 include ISO8601TimeModule 295 311 def initialize(string) 296 312 /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ string … … 321 337 end 322 338 end 339 323 340 def self.valid_iso8601_time?(s) 324 if / (\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s341 if /^(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?$/ =~ s 325 342 # ISO 8601 regular expression by H. Yuki 326 343 # 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 … … 352 369 return false 353 370 end 354 else355 return false356 371 end 357 372 end … … 363 378 end # end of ISO8601_TIME 364 379 365 module ISO8601 _DATE_TIME_MODULE366 include ISO8601 _DATE_MODULE, ISO8601_TIME_MODULE380 module ISO8601DateTimeModule 381 include ISO8601DateModule, ISO8601TimeModule 367 382 def as_string 368 383 if (!@year.nil? and !@month.nil? and !@day.nil?) … … 392 407 end 393 408 394 class ISO8601 _DATE_TIME < ISO8601_DATE395 include ISO8601 _DATE_TIME_MODULE409 class ISO8601DateTime < ISO8601Date 410 include ISO8601DateTimeModule 396 411 def initialize(string) 397 /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string 398 if $1.empty? 412 unless /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string 399 413 raise ArgumentError, 'format invalid' 400 414 else … … 439 453 end 440 454 441 class ISO8601 _TIMEZONE455 class ISO8601Timezone 442 456 attr_accessor :sign, :hour, :minute 443 457 458 def initialize(string) 459 unless /(Z|(([+-])(\d{2}):?(\d{2})))/ =~ string 460 raise ArgumentError, 'invaild format' 461 end 462 if $1 == 'Z' 463 @sign, @hour, @minute = +1, 0, 0 464 else 465 @sign, @hour, @minute = ($3+'1').to_i, $4.to_i , $5.to_i 466 end 467 end 468 444 469 def is_gmt? 445 @sign == "+1"and @hour == 0 and @minute == 0470 @sign == +1 and @hour == 0 and @minute == 0 446 471 end 447 472 448 473 def as_string 449 if @sign == "+1"474 if @sign == +1 450 475 s = "+" 451 elsif @sign == "-1"476 elsif @sign == -1 452 477 s = "-" 453 478 end 454 sprintf(" Z%s%02d%02d", s, @hour, @minute)455 end 456 end # end of ISO8601 _TIMEZONE457 458 module ISO8601 _DURATION_MODULE479 sprintf("%s%02d%02d", s, @hour, @minute) 480 end 481 end # end of ISO8601Timezone 482 483 module ISO8601DurationModule 459 484 attr_reader :years, :months, :weeks, :days 460 485 attr_reader :hours, :minutes, :seconds, :fractional_second … … 546 571 end 547 572 end 548 class ISO8601_DURATION < TIME_DEFINITIONS 549 include ISO8601_DURATION_MODULE 573 574 class ISO8601Duration < TimeDefinitions 575 include ISO8601DurationModule 550 576 def initialize(str) 551 577 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str … … 559 585 self.fractional_second = $16.to_f 560 586 end 561 end # end of ISO8601 _DURATION587 end # end of ISO8601Duration 562 588 end # end of Assumed_Types 563 589 end # end of OpenEHR
Note:
See TracChangeset
for help on using the changeset viewer.