Ignore:
Timestamp:
Sep 25, 2009, 12:27:29 PM (15 years ago)
Author:
KOBAYASHI, Shinji
Message:

rake spec works on trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk/lib/open_ehr/rm/data_types/quantity.rb

    r297 r298  
    22# http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109599337877_94556_1510Report.html
    33# Ticket refs #50
    4 require 'assumed_library_types'
     4#require 'assumed_library_types'
    55module OpenEHR
    66  module RM
    77    module DataTypes
    88      module Quantity
    9 
    10         autoload :Date_Time, "rm/data_types/quantity/date_time.rb"
    11 
    129        class DvOrdered < OpenEHR::RM::DataTypes::Basic::DataValue
    1310          include Comparable
    1411          attr_accessor :normal_range, :other_refference_ranges, :normal_status
    1512
    16           def initialize(normal_range=nil, normal_status = nil,
    17                          other_reference_ranges=nil)
    18             self.normal_range = normal_range
    19             self.normal_status = normal_status
    20             self.other_reference_ranges = other_reference_ranges
     13          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]
    2118          end         
    2219
     
    3229
    3330          def is_simple?
    34             normal_status.nil? and other_refference_ranges.nil?
     31            return @other_reference_ranges.nil?
    3532          end
    3633
     
    4037
    4138          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?
    4340              raise ArgumentError, "Other reference ranges validity error"
    4441            end
     
    4643          end
    4744
    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
    5051          end
    5152        end
     
    5859          attr_reader :magnitude, :magnitude_status
    5960
    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]
    6665          end
    6766
    6867          def <=>(others)
    69             @value <=> others.value
     68            @magnitude <=> others.magnitude
    7069          end
    7170
     
    102101          attr_reader :value, :symbol, :limits
    103102
    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]
    110107          end
    111108
     
    120117          end
    121118
    122           def is_strictly_comparable_to?(others)
    123             unless others.instance_of? OpenEhr::RM::DataTypes::Quantity::DvOrdinal
    124               return false
    125             end
    126             unless others.symbol.defining_code.terminology_id.value ==
    127                 @symbol.defining_code.terminology_id.value
    128               return false
    129             end
    130             return true
    131           end
    132 
    133119          def <=>(other)
    134120            @value <=> other.value
     
    142128            end
    143129          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
    144141        end
    145142
     
    147144          attr_accessor :accuracy
    148145
    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]
    155149          end
    156150
    157151          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)
    159155          end
    160156
    161157          def diff(other)
    162             raise NotImplementedError, 'diff must be implemented'
     158            type_check(other)
     159            return result_builder(DvAmount,
     160                                  (@magnitude-other.magnitude).abs)
    163161          end
    164162
    165163          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)
    167183          end
    168184        end
     
    170186        class DvAmount < DvQuantified
    171187          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])
    179193            else
    180194              @accuracy, @accuracy_percent = nil, nil
     
    183197
    184198          def +(other)
    185             unless self.is_strictly_comparable_to?(other)
     199            unless self.is_strictly_comparable_to? other
    186200              raise ArgumentError, 'type mismatch'
    187201            end
    188             return 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)           
    194208            other.magnitude = - other.magnitude
    195209            self+(other)
     
    212226        class DvQuantity < DvAmount
    213227          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]
    221233          end
    222234
     
    234246
    235247          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
    238252              return true
    239253            else
     
    249263            end
    250264          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           end
    259265        end
    260266
    261267        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
    270269        end
    271270
     
    273272          attr_reader :meaning, :range
    274273
    275           def initialize(meaning, range)
    276             self.meaning = meaning
    277             self.range = range
     274          def initialize(args = {})
     275            self.meaning = args[:meaning]
     276            self.range = args[:range]
    278277          end
    279278
     
    314313          attr_reader :numerator, :denominator, :type, :precision
    315314
    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])
    327323            else
    328324              @accuracy, @accuracy_percent = nil, nil
    329325            end
    330             self.normal_range = normal_range
    331             self.normal_status = normal_status
    332             self.other_reference_ranges = other_reference_ranges
     326            self.normal_range = args[:normal_range]
     327            self.normal_status = args[:normal_status]
     328            self.other_reference_ranges = args[:other_reference_ranges]
    333329          end
    334330
     
    343339
    344340          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
    357354            end
    358355            @denominator = denominator
     
    373370          def precision=(precision)
    374371            unless precision.nil?
    375               unless precision == 0 || self.is_integral?
    376                 @precision = precision
    377               else
     372              if (self.is_integral? && precision !=0)
    378373                raise ArgumentError, 'precision invalid'
    379374              end
    380375            end
     376            @precision = precision
    381377          end
    382378
     
    386382
    387383          def is_strictly_comparable_to?(other)
    388             unless other.instance_of?(DvProportion)
     384            unless super(other)
    389385              return false
    390386            end
Note: See TracChangeset for help on using the changeset viewer.