Changeset 298 for ruby


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

rake spec works on trunk

Location:
ruby/trunk/lib/open_ehr
Files:
1 deleted
3 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
  • ruby/trunk/lib/open_ehr/rm/data_types/text.rb

    r297 r298  
    22# http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109067605961_209522_3179Report.html
    33# Ticket refs #48
     4include OpenEHR::RM::DataTypes::Basic
     5
    46module OpenEHR
    57  module RM
     
    810        class TermMapping
    911          attr_reader :match, :purpose, :target
    10           def initialize(match, purpose, target)
    11             match_valid(match)
    12             purpose_valid(purpose)
    13             target_valid(target)
    14             @match, @purpose, @target = match, purpose, target
     12
     13          def initialize(args ={})
     14            self.match = args[:match]
     15            self.purpose = args[:purpose]
     16            self.target = args[:target]
    1517          end
     18
     19          def match=(match)
     20            unless TermMapping.is_valid_mach_code? match
     21              raise ArgumentError, 'invalid match character'
     22            end
     23            @match = match
     24          end
     25
     26          def purpose=(purpose)
     27#            if !purpose.nil? and !purpose.instance_of?(DvCodedText)
     28#              raise ArgumentError, "purpose is not valid"
     29#            end
     30            # should be settled after terminology service implemented
     31            @purpose = purpose
     32          end
     33
     34          def target=(target)
     35            raise ArgumentError, "target must not be nil" if target.nil?
     36            @target = target
     37          end
     38
    1639          def broader?
    1740            match == '>'
    1841          end
     42
    1943          def equivalent?
    2044            match == '='
    2145          end
     46
    2247          def narrower?
    2348            match == '<'
    2449          end
     50
    2551          def unknown?
    2652            match == '?'
    2753          end
     54
    2855          def TermMapping.is_valid_mach_code?(c)
    2956            c == '>' or c == '=' or c == '<' or c == '?'
    30           end
    31           private
    32           def match_valid(match)
    33             raise ArgumentError, "match is not valid" if !TermMapping.is_valid_mach_code? match
    34           end
    35           def purpose_valid(purpose)
    36             if purpose.nil? or !purpose.instance_of? DvCodedText
    37               raise ArgumentError, "purpose is not valid"
    38             end
    39             # should be settled after terminology service implemented
    40           end
    41           def target_valid(target)
    42             raise ArgumentError, "target must not be nil" if target.nil?
    4357          end
    4458        end
     
    4660        class CodePhrase
    4761          attr_reader :terminology_id, :code_string
    48           def initialize(code_string, terminology_id)
    49             self.code_string = code_string
    50             self.terminology_id = terminology_id
     62
     63          def initialize(args = {})
     64            self.code_string = args[:code_string]
     65            self.terminology_id = args[:terminology_id]
    5166          end
    5267
    5368          def terminology_id=(terminology_id)
    54             raise ArgumentError, "terminology_id should not be nil" if terminology_id.nil?
     69            if terminology_id.nil?
     70              raise ArgumentError, "terminology_id should not be nil"
     71            end
    5572            @terminology_id = terminology_id
    5673          end
    5774
    5875          def code_string=(code_string)
    59             raise ArgumentError, "code_string should not be empty" if code_string.nil? or code_string.empty?
     76            if code_string.nil? or code_string.empty?
     77              raise ArgumentError, "code_string should not be empty"
     78            end
    6079            @code_string = code_string
    6180          end
     
    6382
    6483        class DvText < OpenEHR::RM::DataTypes::Basic::DataValue
    65           attr_reader :value, :formatting, :hyperlink, :mappings
    66           attr_reader :language, :encoding
    67           def initialize(value, formatting = nil, hyperlink = nil,
    68                          mappings = nil, language = nil, encoding = nil)
    69             value_valid(value)
    70             DvText.formatting_valid(formatting)
    71             DvText.encoding_valid(encoding)
    72             DvText.mapping_valid(mappings)
    73             DvText.language_valid(language)
    74             @value, @formatting, @encoding = value, formatting, encoding
    75             @mappings, @language = mappings, language
     84          attr_reader :formatting, :hyperlink, :mappings,
     85                      :language, :encoding
     86
     87          def initialize(args = {})
     88            super(args)
     89            self.formatting = args[:formatting]
     90            self.encoding = args[:encoding]
     91            self.mappings = args[:mappings]
     92            self.language = args[:language]
    7693          end
     94
    7795          def value=(value)
    78             value_valid(value)
     96            if value.nil? or value.empty? or
     97                value.include? CR or value.include? LF
     98              raise ArgumentError, "value is not valid"
     99              # CR and LF are defined in Basic_Definition inherited DataValue.
     100            end
    79101            @value = value
    80102          end
     103
    81104          def formatting=(formatting)
    82             DvText.formatting_valid(formatting)
     105            if !formatting.nil? and formatting.empty?
     106              raise ArgumentError, "formatting is not valid"
     107            end
    83108            @formatting = formatting
    84109          end
     110
    85111          def encoding=(encoding)
    86             DvText.encoding_valid(encoding)
     112            if !encoding.nil? and encoding.code_string.empty?
     113              raise ArgumentError, "encoding is not valid"
     114            end
    87115            @encoding = encoding
    88116          end
    89117          def mappings=(mappings)
    90             DvText.mapping_valid(mappings)
     118            if !mappings.nil? and !mappings.instance_of? Set
     119              raise ArgumentError, "mapping(s) is(are) not valid"
     120            elsif !mappings.nil? and mappings.instance_of? Set and
     121                mappings.empty?
     122              raise ArgumentError, "mapping(s) is(are) not valid"
     123            end
    91124            @mappings = mappings
    92125          end
    93126          def language=(language)
    94             DvText.language_valid(language)
     127            if !@language.nil? and language.empty?
     128              raise ArgumentError, "langage is not valid"
     129            end
    95130            @language = language
    96131          end
    97           private
    98           def value_valid(value)
    99             if value.nil? or value.empty? or value.include? CR or value.include? LF
    100               raise ArgumentError, "value is not valid"
    101               # CR and LF are defined in Basic_Definition inherited DataValue.
    102             end
    103           end
    104 
    105           class << self
    106 
    107             def formatting_valid(formatting)
    108               raise ArgumentError, "formatting is not valid" if formatting != nil and formatting.empty?
    109             end
    110 
    111             def encoding_valid(encoding)
    112               raise ArgumentError, "encoding is not valid" if encoding != nil and encoding.empty?
    113             end
    114 
    115             def mapping_valid(mapping)
    116               if !mapping.nil? and !mapping.instance_of? Set
    117                 raise ArgumentError, "mapping(s) is(are) not valid"
    118               elsif !mapping.nil? and mapping.instance_of? Set
    119                 raise ArgumentError, "mapping(s) is(are) not valid" if mapping.empty?
    120               end
    121             end
    122 
    123             def language_valid(language)
    124               raise ArgumentError, "langage is not valid" if language != nil and language.empty?
    125             end
    126           end
    127 
    128132        end
    129133
     
    131135          attr_reader :defining_code
    132136
    133           def initialize(value, defining_code, formatting = nil,
    134                          hyperlink = nil, mappings = nil, language = nil,
    135                          encoding = nil)
    136             super(value, formatting, hyperlink, mappings, language, encoding)
    137             self.defining_code = defining_code
     137          def initialize(args = {})
     138            super(args)
     139            self.defining_code = args[:defining_code]
    138140          end
    139141
    140142          def defining_code=(defining_code)
    141143            if defining_code.nil?
    142               raise ArgumentError, "Defiinition must be exist"
     144              raise ArgumentError, 'defiining code is mandatory'
    143145            end
    144146            @defining_code = defining_code
     
    148150        class DvParagraph < OpenEHR::RM::DataTypes::Basic::DataValue
    149151          attr_reader :items
    150           def initialize(items)
    151             items_exists(items)
     152
     153          def initialize(args ={})
     154            self.items = args[:items]
     155          end
     156
     157          def items=(items)
     158            if items.nil? or items.empty?
     159              raise ArgumentError, "Items are not valid"
     160            end
    152161            @items = items
    153           end
    154           def items=(items)
    155             items_exists(items)
    156             @items = items
    157           end
    158           private
    159           def items_exists(items)
    160             if !items.nil? and !items.instance_of? Set
    161               raise ArgumentError, "Items are not valid"
    162             elsif !items.nil? and items.instance_of? Set
    163               raise ArgumentError, "Items must be exist" if items.empty?
    164             end
    165162          end
    166163        end
  • ruby/trunk/lib/open_ehr/rm/support/terminology.rb

    r167 r298  
    11
    2 module OpenEhr
     2module OpenEHR
    33  module RM
    44    module Support
     
    88            raise NotImplementedError, "all_codes must be implemented"
    99          end
     10
    1011          def has_code(a_code)
    1112            raise NotImplementedError, "has_code must be implemented"
    1213          end
     14
    1315          def has_lang(a_lang)
    1416            raise NotImplementedError, "has_lang must be implemented"
    1517          end
     18
    1619          def id
    1720            raise NotImplementedError, "id must be returned"
    1821          end
    1922        end
    20         module OpenEhrCodeSetIdentifier
     23
     24        module OpenEHRCodeSetIdentifier
    2125          CODE_SET_ID_CHARACER_SETS = "character sets".freeze
    2226          CODE_SET_ID_COMPRESSION_ALGORITHMS = "compression algorithms".freeze
     
    2933          end
    3034        end
    31         module OpenEhrTerminologyGroupIdentifiers
     35
     36        module OpenEHRTerminologyGroupIdentifiers
    3237          GROUP_ID_ATTESTATION_REASON = "attestation reason".freeze
    3338          GROUP_ID_AUDIT_CHANGE_TYPE = "audit change type".freeze
     
    4651          TERMINOLOGY_ID = "openehr".freeze
    4752        end
     53
    4854        class TerminologyAccess
     55          attr_reader :id
     56
     57          def initialize(args = {})
     58            self.id = args[:id]
     59          end
     60
    4961          def all_codes
    5062            raise NotImplementedError, "all_codes is not implemented"
    5163          end
     64
    5265          def codes_for_group_id(group_id)
    5366            raise NotImplementedError, "codes_for_group_id is not implemented"
    5467          end
     68
    5569          def codes_for_group_name(name, lang)
    5670            raise NotImplementedError, "codes_for_group_name is not implemented"
    5771          end
     72
    5873          def has_code_for_group_id(group_id, a_code)
    59             raise NotImplementedError, "has_code_for_group_id is not implemented"
     74           
    6075          end
    61           def id
    62             raise NotImplementedError, "id is not implemented"
     76
     77          def id=(id)
     78            @terminology = Terminology.find_all_by_name(id)
     79            @id = id
    6380          end
     81
    6482          def rubric_for_code(code, lang)
    65             raise NotImplementedError, "rubic_for_code is not implemented"
     83            return Terminology.find(:first, :conditions => {:code => code,
     84                                      :lang => lang})
    6685          end
     86
    6787          private
    6888          def id_exists
     
    7494          end
    7595        end
     96
    7697        class TerminologyService
    77           include OpenEhrCodeSetIdentifier, OpenEhrTerminologyGroupIdentifiers
     98          include OpenEHRCodeSetIdentifier, OpenEHRTerminologyGroupIdentifiers
     99
    78100          def code_set(name)
    79101            raise NotImplementedError, "code_set is not implemented"
    80102          end
     103
    81104          def code_set_for_id(id)
    82105            raise NotImplementedError, "code_set_for_id is not implemented"
    83106          end
     107
    84108          def code_set_identifiers
    85109            raise NotImplementedError, "code_set_for_identifiers is not implemented"
    86110          end
     111
    87112          def has_code_set(name)
    88113            raise NotImplementedError, "has_code_set is not implemented"
    89114          end
     115
    90116          def has_terminology?(name)
    91117            raise NotImplementedError, "has_terminology is not implemented"
    92118          end
     119
    93120          def openehr_code_sets
    94121            raise NotImplementedError, "openehr_code_set is not implemented"
    95122          end
     123
    96124          def terminology(name)
    97             raise NotImplementedError, "terminology is not implemented"
     125            return TerminologyAccess.new(:id => name)
    98126          end
     127         
    99128          def terminology_identifiers
    100129            raise NotImplementedError, "terminology_identiferes is not implemented"
Note: See TracChangeset for help on using the changeset viewer.