Ignore:
Timestamp:
Jun 26, 2009, 12:54:07 AM (15 years ago)
Author:
KOBAYASHI, Shinji
Message:

ref #50
DV_Quantity finished

File:
1 edited

Legend:

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

    r145 r146  
    5656
    5757        class DV_Quantified < DV_Ordered
    58           attr_reader :magnitude, :magnitude_status, :accuracy
     58          attr_reader :magnitude, :magnitude_status
    5959
    6060          def initialize(magnitude, magnitude_status=nil,
     
    165165
    166166        class DV_Amount < DV_Quantified
    167           attr_reader :accuracy_is_percent
     167          attr_reader :accuracy, :accuracy_percent
    168168          def initialize(magnitude, magnitude_status=nil, accuracy=nil,
    169169                         accuracy_percent=nil, normal_range=nil,
     
    171171            super(magnitude, magnitude_status, normal_range,
    172172                  normal_status, other_reference_ranges)
    173             set_accuracy(accuracy, accuracy_percent) unless accuracy.nil?
     173            unless accuracy.nil?
     174              set_accuracy(accuracy, accuracy_percent)
     175            else
     176              @accuracy, @accuracy_percent = nil, nil
     177            end
    174178          end
    175179          def +(other)
     
    196200
    197201        class DV_Quantity < DV_Amount
    198 
     202          attr_reader :units, :precision
     203          def initialize(magnitude, units, magnitude_status=nil, precision=nil,
     204                         accuracy=nil, accuracy_percent=nil, normal_range=nil,
     205                         normal_status = nil, other_reference_ranges=nil)
     206            super(magnitude, magnitude_status, accuracy, accuracy_percent,
     207                  normal_range, normal_status, other_reference_ranges)
     208            self.units = units
     209            self.precision = precision
     210          end
     211
     212          def units=(units)
     213            raise ArgumentError, 'units should not be nil' if units.nil?
     214            @units = units
     215          end
     216
     217          def precision=(precision)
     218            unless precision.nil? || precision >= -1
     219              raise ArgumentError, 'precision invalid'
     220            end
     221            @precision = precision
     222          end
     223
     224          def is_strictly_comparable_to?(others)
     225            return false if others.nil?
     226            if others.instance_of?(DV_Quantity) && others.units == @units
     227              return true
     228            else
     229              return false
     230            end
     231          end
     232
     233          def is_integral?
     234            if @precision.nil? || precision != 0
     235              return false
     236            else
     237              return true
     238            end
     239          end
     240# accuracy???
     241          def +(other)
     242            unless self.is_strictly_comparable_to?(other)
     243              raise ArgumentError, 'type mismatch'
     244            end
     245            return DV_Quantity.new(@magnitude+other.magnitude, @units,
     246                                   @magnitude_status, @precision,
     247                                   @accuracy, @accuracy_percent, @normal_range,
     248                                   @normal_status, @other_reference_ranges)
     249          end
     250          def -(other)
     251            other.magnitude = - other.magnitude
     252            self+(other)
     253          end
    199254        end
    200255
Note: See TracChangeset for help on using the changeset viewer.