Changeset 298 for ruby/trunk/lib/open_ehr/rm/data_types/quantity.rb
- Timestamp:
- Sep 25, 2009, 12:27:29 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/open_ehr/rm/data_types/quantity.rb
r297 r298 2 2 # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109599337877_94556_1510Report.html 3 3 # Ticket refs #50 4 require 'assumed_library_types'4 #require 'assumed_library_types' 5 5 module OpenEHR 6 6 module RM 7 7 module DataTypes 8 8 module Quantity 9 10 autoload :Date_Time, "rm/data_types/quantity/date_time.rb"11 12 9 class DvOrdered < OpenEHR::RM::DataTypes::Basic::DataValue 13 10 include Comparable 14 11 attr_accessor :normal_range, :other_refference_ranges, :normal_status 15 12 16 def initialize( normal_range=nil, normal_status = nil,17 other_reference_ranges=nil)18 self.normal_range = normal_range19 self.normal_status = normal_status20 self.other_reference_ranges = other_reference_ranges13 def initialize(args = {}) 14 super(args) 15 self.normal_range = args[:normal_range] 16 self.normal_status = args[:normal_status] 17 self.other_reference_ranges = args[:other_reference_ranges] 21 18 end 22 19 … … 32 29 33 30 def is_simple? 34 normal_status.nil? and other_refference_ranges.nil?31 return @other_reference_ranges.nil? 35 32 end 36 33 … … 40 37 41 38 def other_reference_ranges=(other_reference_ranges) 42 unless other_reference_ranges.nil? or !other_reference_ranges.is_empty?39 if !other_reference_ranges.nil? && other_reference_ranges.empty? 43 40 raise ArgumentError, "Other reference ranges validity error" 44 41 end … … 46 43 end 47 44 48 def is_strictly_comparable_to?(other) 49 raise NotImplementedError, 'this method should be implemented' 45 def is_strictly_comparable_to?(others) 46 if others.instance_of? self.class 47 return true 48 else 49 return false 50 end 50 51 end 51 52 end … … 58 59 attr_reader :magnitude, :magnitude_status 59 60 60 def initialize(magnitude, magnitude_status=nil, 61 normal_range=nil, normal_status = nil, 62 other_reference_ranges=nil) 63 super(normal_range, normal_status, other_reference_ranges) 64 self.magnitude = magnitude 65 self.magnitude_status = magnitude_status 61 def initialize(args = {}) 62 super(args) 63 self.magnitude = args[:magnitude] 64 self.magnitude_status = args[:magnitude_status] 66 65 end 67 66 68 67 def <=>(others) 69 @ value <=> others.value68 @magnitude <=> others.magnitude 70 69 end 71 70 … … 102 101 attr_reader :value, :symbol, :limits 103 102 104 def initialize(value, symbol, limits=nil, normal_range=nil, 105 normal_status = nil, other_reference_ranges=nil) 106 self.value = value 107 self.symbol = symbol 108 self.limits = limits 109 super(normal_range, normal_status, other_reference_ranges) 103 def initialize(args = {}) 104 super(args) 105 self.symbol = args[:symbol] 106 self.limits = args[:limits] 110 107 end 111 108 … … 120 117 end 121 118 122 def is_strictly_comparable_to?(others)123 unless others.instance_of? OpenEhr::RM::DataTypes::Quantity::DvOrdinal124 return false125 end126 unless others.symbol.defining_code.terminology_id.value ==127 @symbol.defining_code.terminology_id.value128 return false129 end130 return true131 end132 133 119 def <=>(other) 134 120 @value <=> other.value … … 142 128 end 143 129 end 130 def is_strictly_comparable_to?(others) 131 unless super(others) 132 return false 133 end 134 unless others.symbol.defining_code.terminology_id.value == 135 @symbol.defining_code.terminology_id.value 136 return false 137 else 138 return true 139 end 140 end 144 141 end 145 142 … … 147 144 attr_accessor :accuracy 148 145 149 def initialize(magnitude, magnitude_status=nil, accuracy=nil, 150 normal_range=nil, normal_status = nil, 151 other_reference_ranges=nil) 152 super(magnitude, magnitude_status, normal_range, 153 normal_status, other_reference_ranges) 154 self.accuracy = accuracy 146 def initialize(args = {}) 147 super(args) 148 self.accuracy = args[:accuracy] 155 149 end 156 150 157 151 def add(a_diff) 158 raise NotImplementedError, 'add must be implemented' 152 type_check(a_diff) 153 return result_builder(DvAbsoluteQuantity, 154 @magnitude+a_diff.magnitude) 159 155 end 160 156 161 157 def diff(other) 162 raise NotImplementedError, 'diff must be implemented' 158 type_check(other) 159 return result_builder(DvAmount, 160 (@magnitude-other.magnitude).abs) 163 161 end 164 162 165 163 def subtract(a_diff) 166 raise NotImplementedError, 'subtract must be implemented' 164 type_check(a_diff) 165 return result_builder(DvAbsoluteQuantity, 166 @magnitude-a_diff.magnitude) 167 end 168 private 169 def type_check(other) 170 unless self.is_strictly_comparable_to? other 171 raise ArgumentError, 'type mismatch' 172 end 173 end 174 175 def result_builder(klass, magnitude) 176 return klass.new(:magnitude => magnitude, 177 :magnitude_status => @magnitude_status, 178 :accuracy => @accuracy, 179 :accuracy_percent => @accuracy_percent, 180 :normal_range => @normal_range, 181 :normal_status => @normal_status, 182 :other_reference_ranges => @other_reference_ranges) 167 183 end 168 184 end … … 170 186 class DvAmount < DvQuantified 171 187 attr_reader :accuracy, :accuracy_percent 172 def initialize(magnitude, magnitude_status=nil, accuracy=nil, 173 accuracy_percent=nil, normal_range=nil, 174 normal_status = nil, other_reference_ranges=nil) 175 super(magnitude, magnitude_status, normal_range, 176 normal_status, other_reference_ranges) 177 unless accuracy.nil? 178 set_accuracy(accuracy, accuracy_percent) 188 189 def initialize(args = {}) 190 super(args) 191 unless args[:accuracy].nil? 192 set_accuracy(args[:accuracy], args[:accuracy_percent]) 179 193 else 180 194 @accuracy, @accuracy_percent = nil, nil … … 183 197 184 198 def +(other) 185 unless self.is_strictly_comparable_to? (other)199 unless self.is_strictly_comparable_to? other 186 200 raise ArgumentError, 'type mismatch' 187 201 end 188 re turn DvAmount.new(@magnitude+other.magnitude, @magnitude_status,189 @accuracy, @accuracy_percent, @normal_range,190 @normal_status, @other_reference_ranges)191 end 192 193 def -(other) 202 result = self.dup 203 result.magnitude = @magnitude + other.magnitude 204 return result 205 end 206 207 def -(other) 194 208 other.magnitude = - other.magnitude 195 209 self+(other) … … 212 226 class DvQuantity < DvAmount 213 227 attr_reader :units, :precision 214 def initialize(magnitude, units, magnitude_status=nil, precision=nil, 215 accuracy=nil, accuracy_percent=nil, normal_range=nil, 216 normal_status = nil, other_reference_ranges=nil) 217 super(magnitude, magnitude_status, accuracy, accuracy_percent, 218 normal_range, normal_status, other_reference_ranges) 219 self.units = units 220 self.precision = precision 228 229 def initialize(args = {}) 230 super(args) 231 self.units = args[:units] 232 self.precision = args[:precision] 221 233 end 222 234 … … 234 246 235 247 def is_strictly_comparable_to?(others) 236 return false if others.nil? 237 if others.instance_of?(DvQuantity) && others.units == @units 248 unless super(others) 249 return false 250 end 251 if others.units == @units 238 252 return true 239 253 else … … 249 263 end 250 264 end 251 # accuracy???252 def +(other)253 dv_amount = super(other)254 return DvQuantity.new(dv_amount.magnitude, @units,255 @magnitude_status, @precision,256 @accuracy, @accuracy_percent, @normal_range,257 @normal_status, @other_reference_ranges)258 end259 265 end 260 266 261 267 class DvCount < DvAmount 262 def is_strictly_comparable_to?(others) 263 return false if others.nil? 264 if others.instance_of?(DvCount) 265 return true 266 else 267 return false 268 end 269 end 268 270 269 end 271 270 … … 273 272 attr_reader :meaning, :range 274 273 275 def initialize( meaning, range)276 self.meaning = meaning277 self.range = range274 def initialize(args = {}) 275 self.meaning = args[:meaning] 276 self.range = args[:range] 278 277 end 279 278 … … 314 313 attr_reader :numerator, :denominator, :type, :precision 315 314 316 def initialize(numerator, denominator, type, precision=nil, 317 magnitude_status=nil, accuracy=nil, 318 accuracy_percent=nil, normal_range=nil, 319 normal_status = nil, other_reference_ranges=nil) 320 self.type = type 321 self.numerator = numerator 322 self.denominator = denominator 323 self.precision = precision 324 self.magnitude_status = magnitude_status 325 unless accuracy.nil? 326 set_accuracy(accuracy, accuracy_percent) 315 def initialize(args = {}) 316 self.type = args[:type] 317 self.numerator = args[:numerator] 318 self.denominator = args[:denominator] 319 self.precision = args[:precision] 320 self.magnitude_status =args[:magnitude_status] 321 unless args[:accuracy].nil? 322 set_accuracy(args[:accuracy], args[:accuracy_percent]) 327 323 else 328 324 @accuracy, @accuracy_percent = nil, nil 329 325 end 330 self.normal_range = normal_range331 self.normal_status = normal_status332 self.other_reference_ranges = other_reference_ranges326 self.normal_range = args[:normal_range] 327 self.normal_status = args[:normal_status] 328 self.other_reference_ranges = args[:other_reference_ranges] 333 329 end 334 330 … … 343 339 344 340 def denominator=(denominator) 345 if denominator.nil? or denominator == PK_RATIO 346 raise ArgumentError, 'denominator invalid' 347 end 348 if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) && 349 !denominator.integer? 350 raise ArgumentError, 'denominator invalid for type' 351 end 352 if @type == PK_UNITARY && denominator != 1 353 raise ArgumentError, 'denominator invalid for type' 354 end 355 if @type == PK_PERCENT && denominator != 100 356 raise ArgumentError, 'denominator invaild for type' 341 case @type 342 when PK_UNITARY 343 unless denominator == 1 344 raise ArgumentError, 'Unitary denominator must be 1' 345 end 346 when PK_PERCENT 347 unless denominator == 100 348 raise ArgumentError, 'Percent denominator must be 100' 349 end 350 when PK_FRACTION, PK_INTEGER_FRACTION 351 unless denominator.integer? and @numerator.integer? 352 raise ArgumentError, 'Fraction numerator/denominator must be integer' 353 end 357 354 end 358 355 @denominator = denominator … … 373 370 def precision=(precision) 374 371 unless precision.nil? 375 unless precision == 0 || self.is_integral? 376 @precision = precision 377 else 372 if (self.is_integral? && precision !=0) 378 373 raise ArgumentError, 'precision invalid' 379 374 end 380 375 end 376 @precision = precision 381 377 end 382 378 … … 386 382 387 383 def is_strictly_comparable_to?(other) 388 unless other.instance_of?(DvProportion)384 unless super(other) 389 385 return false 390 386 end
Note:
See TracChangeset
for help on using the changeset viewer.