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

hand merge

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk/lib/open_ehr/assumed_library_types.rb

    r167 r297  
    33require 'time'
    44
    5 module OpenEhr
     5module OpenEHR
    66  module AssumedLibraryTypes
    77    class Any < Object
     
    1212      attr_reader :lower, :upper
    1313     
    14       def initialize(lower, upper,
    15                      lower_included = nil, upper_included = nil)
    16         check_lower_upper(lower, upper)
    17         self.lower_included = lower_included
    18         self.upper_included = upper_included
     14      def initialize(args = {})
     15        check_lower_upper(args[:lower], args[:upper])
     16        self.lower_included = args[:lower_included]
     17        self.upper_included = args[:upper_included]
    1918      end
    2019
     
    4746
    4847      def upper_included=(upper_included)
    49         if (upper == nil) && (upper_included != nil)
     48        if (@upper.nil?) && (upper_included != nil)
    5049          raise ArgumentError, "upper is not set"
    5150        end
     
    5857
    5958      def has?(value)
    60         if ((@lower < value) && (value < @upper) ||
    61             (@lower_included == true) && (@lower == value) ||
    62             (@upper_included == true) && (@upper == value))
     59        if ((@lower.nil?||@lower < value||((@lower_included == true) && (@lower == value)))&&
     60            (@upper.nil?||value < @upper||((@upper_included == true) && (@upper == value))))
    6361          true
    6462        else
     
    7068
    7169      def check_lower_upper(lower, upper)
    72         if (lower == nil) && (upper == nil)
     70        if lower.nil? && upper.nil?
    7371          raise ArgumentError, "Either lower or upper must be assigned"
    7472        end
     
    8381    end # end of Interval
    8482 
    85     class TIME_DEFINITIONS < Any
     83    class TimeDefinitions < Any
    8684      DAYS_IN_LEAP_YEAR = 366
    8785      DAYS_IN_WEEK = 7
     
    9795
    9896      def self.valid_year?(year)
    99         year >= 0
     97        return !year.nil? && year >= 0
    10098      end
    10199
    102100      def self.valid_day?(y, m, d)
    103         Date.valid_date?(y,m,d) and valid_year? y
     101        unless y.nil? || m.nil? || d.nil?
     102          return Date.valid_date?(y,m,d)
     103        end
     104        if (y.nil?) || (m.nil? && !d.nil?)
     105          return false
     106        end
     107        return self.valid_year?(y) && self.valid_month?(m)
    104108      end
    105109
    106110      def self.valid_hour?(h,m = nil, s = nil)
     111        if h.nil?
     112          return false
     113        end
    107114        if !m.nil? and !valid_minute?(m)
    108115          return false
     
    113120        (h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0)
    114121      end
     122
    115123      def self.valid_minute?(mi)
    116124        mi >= 0 and mi < MINUTES_IN_HOUR
    117125      end
     126
    118127      def self.valid_second?(s)
    119128        s >= 0 and s < SECONDS_IN_MINUTE
    120129      end
     130
    121131      def self.valid_month?(mo)
    122132        mo >= 1 and mo <= MONTH_IN_YEAR
    123133      end
    124     end # end of TIME_DEFINITIONS
    125 
    126     module ISO8601_DATE_MODULE
     134    end # end of TimeDefinitions
     135
     136    module ISO8601DateModule
    127137      attr_reader :year, :month, :day
     138
    128139      def year=(year)
    129         raise ArgumentError, "Year is not valid" unless ISO8601_DATE.valid_year?(year)
     140        unless ISO8601Date.valid_year?(year)
     141          raise ArgumentError, "Year is not valid"
     142        end
    130143        @year = year
    131144      end
     145
    132146      def month=(month)
    133         raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601_DATE.valid_month?(month)
     147        raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601Date.valid_month?(month)
    134148        @month = month
    135149      end
    136150
    137151      def day=(day)
    138         raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601_DATE.valid_day?(@year, @month, day)
     152       
     153        raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601Date.valid_day?(@year, @month, day)
    139154        @day = day
    140155      end
     
    176191    end
    177192
    178     class ISO8601_DATE < TIME_DEFINITIONS
    179       include ISO8601_DATE_MODULE
     193    class ISO8601Date < TimeDefinitions
     194      include ISO8601DateModule
    180195      def initialize(string)
    181196        /(\d{4})(?:-(\d{2})(?:-(\d{2})?)?)?/ =~ string
     
    207222    end # end of ISO8601_DATE
    208223
    209     module ISO8601_TIME_MODULE
     224    module ISO8601TimeModule
    210225      attr_reader :hour, :minute, :second, :fractional_second, :timezone
    211226
    212227      def hour=(hour)
    213         raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second)
     228        unless ISO8601Time.valid_hour?(hour, @minute, @second)
     229          raise ArgumentError, "hour is not valid"
     230        end
    214231        @hour = hour
    215232      end
     
    220237
    221238      def minute=(minute)
    222         raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601_TIME.valid_minute?(minute)
     239        raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601Time.valid_minute?(minute)
    223240        @minute = minute
    224241      end
     
    230247      def second=(second)
    231248        raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil?
    232         raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601_TIME.valid_second?(second)
     249        raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601Time.valid_second?(second)
    233250        @second = second
    234251      end
     
    237254        raise ArgumentError, "minute not defined" if minute_unknown? and !fractional_second.nil?
    238255        raise ArgumentError, "second not defined" if second_unknown? and !fractional_second.nil?
    239         raise ArgumentError, "fractional second should be lower than 1.0" if !fractional_second.nil? and fractional_second >= 1.0
     256        if !fractional_second.nil? &&
     257            (fractional_second < 0.0 || fractional_second >= 1.0)
     258          raise ArgumentError, 'fractional second should be between 0.0 - 1.0'
     259        end
    240260        @fractional_second = fractional_second
    241261      end
    242262
    243263      def has_fractional_second?
    244         if @fractional_second.nil?
    245           return false
    246         else
    247           return true
    248         end
     264        return !@fractional_second.nil?
    249265      end
    250266
     
    291307    end
    292308
    293     class ISO8601_TIME < TIME_DEFINITIONS
    294       include ISO8601_TIME_MODULE
     309    class ISO8601Time < TimeDefinitions
     310      include ISO8601TimeModule
    295311      def initialize(string)
    296312        /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ string
     
    321337        end
    322338      end
     339
    323340      def self.valid_iso8601_time?(s)
    324         if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
     341        if /^(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?$/ =~ s
    325342# ISO 8601 regular expression by H. Yuki
    326343#  http://digit.que.ne.jp/work/wiki.cgi?Perl%E3%83%A1%E3%83%A2%2FW3C%E5%BD%A2%E5%BC%8F%E3%81%AE%E6%97%A5%E6%99%82%E3%81%AE%E8%A7%A3%E6%9E%90
     
    352369                return false
    353370              end
    354             else
    355               return false
    356371            end
    357372          end
     
    363378    end # end of ISO8601_TIME
    364379
    365     module ISO8601_DATE_TIME_MODULE
    366       include ISO8601_DATE_MODULE, ISO8601_TIME_MODULE
     380    module ISO8601DateTimeModule
     381      include ISO8601DateModule, ISO8601TimeModule
    367382      def as_string
    368383        if (!@year.nil? and !@month.nil? and !@day.nil?)
     
    392407    end
    393408
    394     class ISO8601_DATE_TIME < ISO8601_DATE
    395       include ISO8601_DATE_TIME_MODULE
     409    class ISO8601DateTime < ISO8601Date
     410      include ISO8601DateTimeModule
    396411      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?
     412        unless /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string
    399413          raise ArgumentError, 'format invalid'
    400414        else
     
    439453    end
    440454 
    441     class ISO8601_TIMEZONE
     455    class ISO8601Timezone
    442456      attr_accessor :sign, :hour, :minute
    443457
     458      def initialize(string)
     459        unless /(Z|(([+-])(\d{2}):?(\d{2})))/ =~ string
     460          raise ArgumentError, 'invaild format'
     461        end
     462        if $1 == 'Z'
     463          @sign, @hour, @minute = +1, 0, 0
     464        else
     465          @sign, @hour, @minute = ($3+'1').to_i, $4.to_i , $5.to_i
     466        end
     467      end
     468
    444469      def is_gmt?
    445         @sign == "+1" and @hour == 0 and @minute == 0
     470        @sign == +1 and @hour == 0 and @minute == 0
    446471      end
    447472
    448473      def as_string
    449         if @sign == "+1"
     474        if @sign == +1
    450475          s = "+"
    451         elsif @sign == "-1"
     476        elsif @sign == -1
    452477          s = "-"
    453478        end
    454         sprintf("Z%s%02d%02d", s, @hour, @minute)
    455       end
    456     end # end of ISO8601_TIMEZONE
    457 
    458     module ISO8601_DURATION_MODULE
     479        sprintf("%s%02d%02d", s, @hour, @minute)
     480      end
     481    end # end of ISO8601Timezone
     482
     483    module ISO8601DurationModule
    459484      attr_reader :years, :months, :weeks, :days
    460485      attr_reader :hours, :minutes, :seconds, :fractional_second
     
    546571      end
    547572    end
    548     class ISO8601_DURATION < TIME_DEFINITIONS
    549       include ISO8601_DURATION_MODULE
     573
     574    class ISO8601Duration < TimeDefinitions
     575      include ISO8601DurationModule
    550576      def initialize(str)
    551577        /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str
     
    559585        self.fractional_second = $16.to_f
    560586      end
    561     end # end of ISO8601_DURATION
     587    end # end of ISO8601Duration
    562588  end # end of Assumed_Types
    563589end # end of OpenEHR
Note: See TracChangeset for help on using the changeset viewer.