source: ruby/trunk/lib/models/assumed_library_types.rb@ 70

Last change on this file since 70 was 70, checked in by (none), 16 years ago

refs #38

File size: 4.3 KB
Line 
1require 'date'
2
3module OpenEHR
4 module Assumed_Library_Types
5 class Any < Object
6
7 end # of Any
8 class Interval < Any
9 attr_reader :lower, :lower_included, :lower_unbounded
10 attr_reader :upper, :upper_included, :upper_unbounded
11 def initialize(lower, upper,
12 lower_included = nil, upper_included = nil)
13 check_lower_upper(lower, upper)
14 set_lower_included(lower_included)
15 set_upper_included(upper_included)
16 end
17
18 def set_lower(lower)
19 check_lower_upper(lower, @upper)
20 end
21
22 def set_upper(upper)
23 check_lower_upper(@lower, upper)
24 end
25
26 def set_lower_included(lower_included)
27 if (lower == nil) && (lower_included != nil)
28 raise ArgumentError, "lower is not set"
29 end
30 @lower_included = lower_included
31 end
32
33 def set_upper_included(upper_included)
34 @upper_included = upper_included
35 if (upper == nil) && (upper_included != nil)
36 raise ArgumentError, "upper is not set"
37 end
38 end
39
40 def has?(value)
41 if ((@lower < value) && (value < @upper) ||
42 (@lower_included == true) && (@lower == value) ||
43 (@upper_included == true) && (@upper == value))
44 true
45 else
46 false
47 end
48 end
49
50 private
51
52 def check_lower_upper(lower, upper)
53 if (lower == nil) && (upper == nil)
54 raise ArgumentError, "Either lower or upper must be assigned"
55 end
56 if (lower == nil) && (upper != nil)
57 @lower_unbounded = true
58 elsif (lower != nil) && (upper == nil)
59 @upper_unbounded = true
60 elsif lower > upper
61 raise ArgumentError, "Upper must be larger than lower."
62 end
63 @lower = lower
64 @upper = upper
65 end
66 end # end of Interval
67
68 class TIME_DEFINITIONS < Any
69 DAYS_IN_LEAP_YEAR = 366
70 DAYS_IN_WEEK = 7
71 DAYS_IN_YEAR = 365
72 HOURS_IN_DAY = 24
73 MAX_DAYS_IN_MONTH = 31
74 MAX_DAYS_IN_YEAR = 366
75 MINUTES_IN_HOUR = 60
76 MONTH_IN_YEAR = 12
77 NOMINAL_DAYS_IN_MONTH = 30.42
78 NOMINAL_DAYS_IN_YEAR = 365.24
79 SECONDS_IN_MINUTE = 60
80
81 def self.valid_year?(year)
82 year >= 0
83 end
84
85 def self.valid_day?(y, m, d)
86 Date.valid_date?(y,m,d) and valid_year? y
87 end
88
89 def self.valid_hour?(h,m,s)
90 valid_minute?(m) and valid_second?(s) and ((h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0))
91 end
92 def self.valid_minute?(m)
93 m >= 0 and m < MINUTES_IN_HOUR
94 end
95 def self.valid_second?(s)
96 s >= 0 and s < SECONDS_IN_MINUTE
97 end
98 def self.valid_month?(m)
99 m >= 1 and m <= MONTH_IN_YEAR
100 end
101 end # end of TIME_DEFINITIONS
102
103 class ISO8601_DATE < TIME_DEFINITIONS
104 attr_reader :year, :month, :day
105 def initialize(year = nil, month = nil, day = nil)
106 @year = @month = @day = nil
107 if !year.nil?
108 self.year = year
109 end
110 if !month.nil?
111 self.month = month
112 end
113 if !day.nil?
114 self.day = day
115 end
116 end
117 def year=(year)
118 raise ArgumentError, "Year is not valid" if !ISO8601_DATE.valid_year?(year)
119 @year = year
120 end
121 def month=(month)
122 raise ArgumentError, "Month is not valid" if !ISO8601_DATE.valid_month?(month)
123 @month = month
124 end
125 def day=(day)
126 raise ArgumentError, "Day is not valid" if !ISO8601_DATE.valid_day?(@year, @month, day)
127 @day = day
128 end
129 def as_string
130 if (!@year.nil? and !@month.nil? and !@day.nil?)
131 Date.new(@year, @month, @day).to_s
132 elsif (!@year.nil? and !@month.nil? and @day.nil?)
133 Date.new(@year, @month).to_s[0,7]
134 elsif (!@year.nil? and @month.nil? and @day.nil?)
135 Date.new(@year).to_s[0,4]
136 end
137 end
138 def month_unknown?
139 @month.nil?
140 end
141 def day_unknown?
142 @day.nil?
143 end
144 def is_extended?
145 true
146 end
147 def is_partial?
148 month_unknown? or day_unknown?
149 end
150 def valid_iso8601_date(string)
151 begin
152 date = Date.parse(string)
153 rescue
154 return false
155 end
156 true
157 end
158 end # end of ISO_8601_DATE
159 end # end of Assumed_Types
160end # end of OpenEHR
Note: See TracBrowser for help on using the repository browser.