Changeset 149 for ruby/trunk/lib
- Timestamp:
- Jun 30, 2009, 11:59:58 PM (15 years ago)
- Location:
- ruby/trunk/lib/models
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/models/assumed_library_types.rb
r143 r149 165 165 month_unknown? or day_unknown? 166 166 end 167 168 protected 169 def leapyear?(year) 170 case 171 when year % 400 == 0: true 172 when year % 100 == 0: false 173 else year % 4 == 0 174 end 175 end 167 176 end 168 177 … … 187 196 end 188 197 end 198 189 199 def self.valid_iso8601_date?(string) 190 200 begin … … 353 363 end # end of ISO8601_TIME 354 364 355 class ISO8601_DATE_TIME < ISO8601_DATE 356 include ISO8601_TIME_MODULE, ISO8601_DATE_MODULE 357 def initialize(string) 358 /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string 359 if $1.empty? 360 raise ArgumentError, 'format invalid' 361 else 362 self.year = $1.to_i 363 end 364 if $2.nil? 365 self.month = nil 366 else 367 self.month = $2.to_i 368 end 369 if $3.nil? 370 self.day = nil 371 else 372 self.day = $3.to_i 373 end 374 if $5.nil? 375 self.minute = nil 376 else 377 self.minute = $5.to_i 378 end 379 if $6.nil? 380 self.second = nil 381 else 382 self.second = $6.to_i 383 end 384 if $4.nil? 385 self.hour = nil 386 else 387 self.hour = $4.to_i 388 end 389 if $7.nil? or $7.empty? 390 self.fractional_second = nil 391 else 392 self.fractional_second = ("0."+$7).to_f 393 end 394 if $8.nil? 395 self.timezone = nil 396 else 397 self.timezone = $9+$10 398 end 399 end 400 365 module ISO8601_DATE_TIME_MODULE 366 include ISO8601_DATE_MODULE, ISO8601_TIME_MODULE 401 367 def as_string 402 368 if (!@year.nil? and !@month.nil? and !@day.nil?) … … 424 390 return s 425 391 end 426 427 392 end 428 393 394 class ISO8601_DATE_TIME < ISO8601_DATE 395 include ISO8601_DATE_TIME_MODULE 396 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? 399 raise ArgumentError, 'format invalid' 400 else 401 self.year = $1.to_i 402 end 403 if $2.nil? 404 self.month = nil 405 else 406 self.month = $2.to_i 407 end 408 if $3.nil? 409 self.day = nil 410 else 411 self.day = $3.to_i 412 end 413 if $5.nil? 414 self.minute = nil 415 else 416 self.minute = $5.to_i 417 end 418 if $6.nil? 419 self.second = nil 420 else 421 self.second = $6.to_i 422 end 423 if $4.nil? 424 self.hour = nil 425 else 426 self.hour = $4.to_i 427 end 428 if $7.nil? or $7.empty? 429 self.fractional_second = nil 430 else 431 self.fractional_second = ("0."+$7).to_f 432 end 433 if $8.nil? 434 self.timezone = nil 435 else 436 self.timezone = $9+$10 437 end 438 end 439 end 440 429 441 class ISO8601_TIMEZONE 430 442 attr_accessor :sign, :hour, :minute … … 444 456 end # end of ISO8601_TIMEZONE 445 457 446 class ISO8601_DURATION < TIME_DEFINITIONS458 module ISO8601_DURATION_MODULE 447 459 attr_reader :years, :months, :weeks, :days 448 460 attr_reader :hours, :minutes, :seconds, :fractional_second 449 450 def initialize(str)451 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str452 self.years = $2.to_i453 self.months = $4.to_i454 self.weeks = $6.to_i455 self.days = $8.to_i456 self.hours = $11.to_i457 self.minutes = $13.to_i458 self.seconds = $15.to_i459 self.fractional_second = $16.to_f460 end461 461 462 462 def years=(years) … … 545 545 return str 546 546 end 547 end 548 class ISO8601_DURATION < TIME_DEFINITIONS 549 include ISO8601_DURATION_MODULE 550 def initialize(str) 551 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str 552 self.years = $2.to_i 553 self.months = $4.to_i 554 self.weeks = $6.to_i 555 self.days = $8.to_i 556 self.hours = $11.to_i 557 self.minutes = $13.to_i 558 self.seconds = $15.to_i 559 self.fractional_second = $16.to_f 560 end 547 561 end # end of ISO8601_DURATION 548 562 end # end of Assumed_Types -
ruby/trunk/lib/models/rm/data_types/quantity.rb
r148 r149 150 150 normal_range=nil, normal_status = nil, 151 151 other_reference_ranges=nil) 152 super 152 super(magnitude, magnitude_status, normal_range, 153 153 normal_status, other_reference_ranges) 154 154 self.accuracy = accuracy … … 156 156 157 157 def add(a_diff) 158 raise NotImplement Error, 'add must be implemented'158 raise NotImplementedError, 'add must be implemented' 159 159 end 160 160 161 161 def diff(other) 162 raise NotImplement Error, 'diff must be implemented'162 raise NotImplementedError, 'diff must be implemented' 163 163 end 164 164 165 165 def subtract(a_diff) 166 raise NotImplement Error, 'subtract must be implemented'166 raise NotImplementedError, 'subtract must be implemented' 167 167 end 168 168 end -
ruby/trunk/lib/models/rm/data_types/quantity/date_time.rb
r142 r149 2 2 # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109696321450_28117_5362Report.html 3 3 # Ticket refs #49 4 require 'assumed_library_types' 5 4 6 module OpenEHR 5 7 module RM … … 10 12 attr_reader :value 11 13 12 def initialize(value) 14 def initialize(value, magnitude_status=nil, accuracy=nil, 15 normal_range=nil, normal_status=nil, 16 other_reference_ranges=nil) 13 17 self.value = value 18 self.magnitude_status = magnitude_status 19 self.accuracy = accuracy 20 self.normal_range = normal_range 21 self.normal_status = normal_status 22 self.other_reference_ranges = other_reference_ranges 14 23 end 15 24 16 25 def value=(value) 17 if value. empty? or value.nil?26 if value.nil? or value.empty? 18 27 raise ArgumentError, 'invalid value' 19 28 end 20 29 @value = value 21 end22 23 def diff(value)24 raise NotImplementedError, 'diff must be implemented'25 30 end 26 31 end … … 28 33 class DV_Date < DV_Temporal 29 34 include OpenEHR::Assumed_Library_Types::ISO8601_DATE_MODULE 30 def initialize 31 35 36 DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31] 37 38 def initialize(value, magnitude_status=nil, accuracy=nil, 39 normal_range=nil, normal_status=nil, 40 other_reference_range=nil) 41 super(value, magnitude_status, accuracy, normal_range, 42 normal_status, other_reference_range) 32 43 end 33 44 45 def value=(value) 46 super(value) 47 iso8601_date = Assumed_Library_Types::ISO8601_DATE.new(value) 48 @year = iso8601_date.year 49 @month = iso8601_date.month 50 @day = iso8601_date.day 51 end 52 53 undef magnitude= 54 55 def magnitude 56 return Date.new(@year, @month, @day)-Date.new(1601,1,1) 57 end 58 59 def diff(other) 60 if self.magnitude > other.magnitude 61 past, future = other, self 62 else 63 past, future = self, other 64 end 65 year, month, day = 0, 0, 0 66 if (future.day >= past.day) 67 day = future.day - past.day 68 else 69 month -= 1 70 previous_month = future.month - 1 71 if previous_month <= 0 72 previous_month = 12 73 end 74 day = DAYS_IN_MONTH[previous_month] + future.day - past.day 75 if leapyear?(future.year) && (previous_month == 2) 76 day += 1 77 end 78 end 79 week = day / 7 80 if (future.month >= past.month) 81 month += future.month - past.month 82 else 83 year -= 1 84 month += future.month + 12 - past.month 85 end 86 year += future.year - past.year 87 return DV_Duration.new( 88 'P' + year.to_s + 'Y' + month.to_s + 'M' + 89 week.to_s + 'W' + day.to_s + 'D') 90 end 34 91 end 35 92 … … 43 100 end 44 101 end 102 103 class DV_Duration < DV_Amount 104 include Assumed_Library_Types::ISO8601_DURATION_MODULE 105 attr_reader :value 106 107 def initialize(value, magnitude_status=nil, accuracy=nil, 108 accuracy_percent=nil, normal_range=nil, 109 normal_status = nil, other_reference_ranges=nil) 110 self.value = value 111 end 112 113 def value=(value) 114 raise ArgumentError, 'value must be not nil' if value.nil? 115 @value = value 116 iso8601_duration = Assumed_Library_Types::ISO8601_DURATION.new(value) 117 self.years = iso8601_duration.years 118 self.months = iso8601_duration.months 119 self.weeks = iso8601_duration.weeks 120 self.days = iso8601_duration.days 121 self.hours = iso8601_duration.hours 122 self.minutes = iso8601_duration.minutes 123 self.seconds = iso8601_duration.seconds 124 self.fractional_second = iso8601_duration.fractional_second 125 end 126 127 def magnitude 128 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) + 129 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR* 130 SECONDS_IN_MINUTE) + @second + @fractional_second 131 end 132 133 undef magnitude= 134 135 end 45 136 end # of Date_Time 46 137 end # of Quantity -
ruby/trunk/lib/models/tests/rm/test_data_types.rb
r148 r149 2 2 require 'set' 3 3 require 'rm' 4 require 'tests/rm/quantity/ts_date_time' 4 5 5 6 class RM_Data_Types_Basic_Test < Test::Unit::TestCase … … 360 361 end 361 362 362 class QuantityDateTimeTest < Test::Unit::TestCase363 def setup364 assert_nothing_raised(Exception){365 @dv_temporal = OpenEHR::RM::Data_Types::Quantity::Date_Time::DV_Temporal.new('2008')}366 end367 def test_init368 assert_instance_of OpenEHR::RM::Data_Types::Quantity::Date_Time::DV_Temporal, @dv_temporal369 end370 371 def test_dv_temporal372 assert_equal '2008', @dv_temporal.value373 assert_raise(NotImplementedError){@dv_temporal.diff('2009')}374 end375 end376 377 363 class EncapsulatedTest < Test::Unit::TestCase 378 364 def setup -
ruby/trunk/lib/models/tests/test_assumed_library_types.rb
r143 r149 1 1 require 'test/unit' 2 2 require 'assumed_library_types.rb' 3 4 include OpenEHR::Assumed_Library_Types 3 5 4 6 class Assumed_Library_Test < Test::Unit::TestCase
Note:
See TracChangeset
for help on using the changeset viewer.