rtp-connect 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,7 @@ module RTP
19
19
  raise ArgumentError, "Invalid line encountered; No comma present in the string: #{line}" unless last_comma_pos
20
20
  string_to_check = line[0..last_comma_pos]
21
21
  string_remaining = line[(last_comma_pos+1)..-1]
22
- raise ArgumentError, "Invalid line encountered; Valid checksum missing at end of string: #{string_remaining}" unless string_remaining.length > 3
22
+ raise ArgumentError, "Invalid line encountered; Valid checksum missing at end of string: #{string_remaining}" unless string_remaining.length >= 3
23
23
  checksum_extracted = string_remaining.value.to_i
24
24
  checksum_computed = string_to_check.checksum
25
25
  raise ArgumentError, "Invalid line encountered: Specified checskum #{checksum_extracted} deviates from the computed checksum #{checksum_computed}." if checksum_extracted != checksum_computed
@@ -71,9 +71,8 @@ module RTP
71
71
  # * <tt>string</tt> -- A string containing a plan definition record.
72
72
  #
73
73
  def self.load(string)
74
- raise ArgumentError, "Invalid argument 'string'. Expected String, got #{string.class}." unless string.is_a?(String)
75
74
  # Get the quote-less values:
76
- values = string.values
75
+ values = string.to_s.values
77
76
  raise ArgumentError, "Invalid argument 'string': Expected exactly 28 elements, got #{values.length}." unless values.length == 28
78
77
  rtp = self.new
79
78
  # Assign the values to attributes:
@@ -116,10 +115,7 @@ module RTP
116
115
  # * <tt>string</tt> -- An RTPConnect ascii string.
117
116
  #
118
117
  def self.parse(string)
119
- raise ArgumentError, "Invalid argument 'string'. Expected String, got #{string.class}." unless string.is_a?(String)
120
- raise ArgumentError, "Invalid argument 'string': String too short to contain valid RTP data (length: #{string.length})." if string.length < 10
121
- #lines = string.lines
122
- lines = string.split("\r\n")
118
+ lines = string.to_s.split("\r\n")
123
119
  # Create the Plan object:
124
120
  line = lines.first
125
121
  RTP::verify(line)
@@ -185,11 +181,20 @@ module RTP
185
181
  @keyword = 'PLAN_DEF'
186
182
  end
187
183
 
184
+ # Returns true if the argument is an instance with attributes equal to self.
185
+ #
186
+ def ==(other)
187
+ if other.respond_to?(:to_plan)
188
+ other.send(:state) == state
189
+ end
190
+ end
191
+
192
+ alias_method :eql?, :==
193
+
188
194
  # Adds a prescription site record to this instance.
189
195
  #
190
196
  def add_prescription(child)
191
- raise ArgumentError, "Invalid argument 'child'. Expected RTP::Prescription, got #{child.class}." unless child.is_a?(RTP::Prescription)
192
- @prescriptions << child
197
+ @prescriptions << child.to_prescription
193
198
  end
194
199
 
195
200
  # Returns the a properly sorted array of the child records of this instance.
@@ -198,6 +203,12 @@ module RTP
198
203
  return [@prescriptions, @dose_trackings].flatten.compact
199
204
  end
200
205
 
206
+ # Generates a Fixnum hash value for this instance.
207
+ #
208
+ def hash
209
+ state.hash
210
+ end
211
+
201
212
  # Returns the values of this instance in an array.
202
213
  # The values does not include the CRC.
203
214
  #
@@ -233,17 +244,25 @@ module RTP
233
244
  ]
234
245
  end
235
246
 
247
+ # Returns self.
248
+ #
249
+ def to_plan
250
+ self
251
+ end
252
+
236
253
  # Writes the Plan object + any hiearchy of child objects,
237
254
  # to a properly formatted RTPConnect ascii string.
238
255
  #
239
- def to_str
256
+ def to_s
240
257
  str = encode #.force_encoding('utf-8')
241
258
  children.each do |child|
242
- str += child.to_str #.force_encoding('utf-8')
259
+ str += child.to_s #.force_encoding('utf-8')
243
260
  end
244
261
  return str
245
262
  end
246
263
 
264
+ alias :to_str :to_s
265
+
247
266
  # Writes the Plan object, along with its hiearchy of child objects,
248
267
  # to a properly formatted RTPConnect ascii file.
249
268
  #
@@ -253,198 +272,172 @@ module RTP
253
272
  #
254
273
  def write(file)
255
274
  f = open_file(file)
256
- f.write(to_str)
275
+ f.write(to_s)
257
276
  f.close
258
277
  end
259
278
 
260
279
  # Sets the keyword attribute.
261
280
  #
262
281
  def keyword=(value)
263
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
264
- raise ArgumentError, "Invalid keyword. Expected 'PLAN_DEF', got #{value}." unless value.upcase == "PLAN_DEF"
282
+ value = value.to_s.upcase
283
+ raise ArgumentError, "Invalid keyword. Expected 'PLAN_DEF', got #{value}." unless value == "PLAN_DEF"
265
284
  @keyword = value
266
285
  end
267
286
 
268
287
  # Sets the patient_id attribute.
269
288
  #
270
289
  def patient_id=(value)
271
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
272
- @patient_id = value
290
+ @patient_id = value && value.to_s
273
291
  end
274
292
 
275
293
  # Sets the patient_last_name attribute.
276
294
  #
277
295
  def patient_last_name=(value)
278
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
279
- @patient_last_name = value
296
+ @patient_last_name = value && value.to_s
280
297
  end
281
298
 
282
299
  # Sets the patient_first_name attribute.
283
300
  #
284
301
  def patient_first_name=(value)
285
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
286
- @patient_first_name = value
302
+ @patient_first_name = value && value.to_s
287
303
  end
288
304
 
289
305
  # Sets the patient_middle_initial attribute.
290
306
  #
291
307
  def patient_middle_initial=(value)
292
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
293
- @patient_middle_initial = value
308
+ @patient_middle_initial = value && value.to_s
294
309
  end
295
310
 
296
311
  # Sets the plan_id attribute.
297
312
  #
298
313
  def plan_id=(value)
299
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
300
- @plan_id = value
314
+ @plan_id = value && value.to_s
301
315
  end
302
316
 
303
317
  # Sets the plan_date attribute.
304
318
  #
305
319
  def plan_date=(value)
306
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
307
- @plan_date = value
320
+ @plan_date = value && value.to_s
308
321
  end
309
322
 
310
323
  # Sets the plan_time attribute.
311
324
  #
312
325
  def plan_time=(value)
313
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
314
- @plan_time = value
326
+ @plan_time = value && value.to_s
315
327
  end
316
328
 
317
329
  # Sets the course_id attribute.
318
330
  #
319
331
  def course_id=(value)
320
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
321
- @course_id = value
332
+ @course_id = value && value.to_s
322
333
  end
323
334
 
324
335
  # Sets the diagnosis attribute.
325
336
  #
326
337
  def diagnosis=(value)
327
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
328
- @diagnosis = value
338
+ @diagnosis = value && value.to_s
329
339
  end
330
340
 
331
341
  # Sets the md_last_name attribute.
332
342
  #
333
343
  def md_last_name=(value)
334
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
335
- @md_last_name = value
344
+ @md_last_name = value && value.to_s
336
345
  end
337
346
 
338
347
  # Sets the md_first_name attribute.
339
348
  #
340
349
  def md_first_name=(value)
341
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
342
- @md_first_name = value
350
+ @md_first_name = value && value.to_s
343
351
  end
344
352
 
345
353
  # Sets the md_middle_initial attribute.
346
354
  #
347
355
  def md_middle_initial=(value)
348
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
349
- @md_middle_initial = value
356
+ @md_middle_initial = value && value.to_s
350
357
  end
351
358
 
352
359
  # Sets the md_approve_last_name attribute.
353
360
  #
354
361
  def md_approve_last_name=(value)
355
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
356
- @md_approve_last_name = value
362
+ @md_approve_last_name = value && value.to_s
357
363
  end
358
364
 
359
365
  # Sets the md_approve_first_name attribute.
360
366
  #
361
367
  def md_approve_first_name=(value)
362
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
363
- @md_approve_first_name = value
368
+ @md_approve_first_name = value && value.to_s
364
369
  end
365
370
 
366
371
  # Sets the md_approve_middle_initial attribute.
367
372
  #
368
373
  def md_approve_middle_initial=(value)
369
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
370
- @md_approve_middle_initial = value
374
+ @md_approve_middle_initial = value && value.to_s
371
375
  end
372
376
 
373
377
  # Sets the phy_approve_last_name attribute.
374
378
  #
375
379
  def phy_approve_last_name=(value)
376
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
377
- @phy_approve_last_name = value
380
+ @phy_approve_last_name = value && value.to_s
378
381
  end
379
382
 
380
383
  # Sets the phy_approve_first_name attribute.
381
384
  #
382
385
  def phy_approve_first_name=(value)
383
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
384
- @phy_approve_first_name = value
386
+ @phy_approve_first_name = value && value.to_s
385
387
  end
386
388
 
387
389
  # Sets the phy_approve_middle_initial attribute.
388
390
  #
389
391
  def phy_approve_middle_initial=(value)
390
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
391
- @phy_approve_middle_initial = value
392
+ @phy_approve_middle_initial = value && value.to_s
392
393
  end
393
394
 
394
395
  # Sets the author_last_name attribute.
395
396
  #
396
397
  def author_last_name=(value)
397
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
398
- @author_last_name = value
398
+ @author_last_name = value && value.to_s
399
399
  end
400
400
 
401
401
  # Sets the author_first_name attribute.
402
402
  #
403
403
  def author_first_name=(value)
404
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
405
- @author_first_name = value
404
+ @author_first_name = value && value.to_s
406
405
  end
407
406
 
408
407
  # Sets the author_middle_initial attribute.
409
408
  #
410
409
  def author_middle_initial=(value)
411
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
412
- @author_middle_initial = value
410
+ @author_middle_initial = value && value.to_s
413
411
  end
414
412
 
415
413
  # Sets the rtp_mfg attribute.
416
414
  #
417
415
  def rtp_mfg=(value)
418
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
419
- @rtp_mfg = value
416
+ @rtp_mfg = value && value.to_s
420
417
  end
421
418
 
422
419
  # Sets the rtp_model attribute.
423
420
  #
424
421
  def rtp_model=(value)
425
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
426
- @rtp_model = value
422
+ @rtp_model = value && value.to_s
427
423
  end
428
424
 
429
425
  # Sets the rtp_version attribute.
430
426
  #
431
427
  def rtp_version=(value)
432
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
433
- @rtp_version = value
428
+ @rtp_version = value && value.to_s
434
429
  end
435
430
 
436
431
  # Sets the rtp_if_protocol attribute.
437
432
  #
438
433
  def rtp_if_protocol=(value)
439
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
440
- @rtp_if_protocol = value
434
+ @rtp_if_protocol = value && value.to_s
441
435
  end
442
436
 
443
437
  # Sets the rtp_if_version attribute.
444
438
  #
445
439
  def rtp_if_version=(value)
446
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
447
- @rtp_if_version = value
440
+ @rtp_if_version = value && value.to_s
448
441
  end
449
442
 
450
443
 
@@ -529,6 +522,10 @@ module RTP
529
522
  @current_parent = s
530
523
  end
531
524
 
525
+ # Returns the attributes of this instance in an array (for comparison purposes).
526
+ #
527
+ alias_method :state, :values
528
+
532
529
  # Creates a treatment field record from the given string.
533
530
  #
534
531
  # === Parameters
@@ -35,10 +35,8 @@ module RTP
35
35
  # * <tt>parent</tt> -- A Record which is used to determine the proper parent of this instance.
36
36
  #
37
37
  def self.load(string, parent)
38
- raise ArgumentError, "Invalid argument 'string'. Expected String, got #{string.class}." unless string.is_a?(String)
39
- raise ArgumentError, "Invalid argument 'parent'. Expected RTP::Record, got #{parent.class}." unless parent.is_a?(RTP::Record)
40
38
  # Get the quote-less values:
41
- values = string.values
39
+ values = string.to_s.values
42
40
  raise ArgumentError, "Invalid argument 'string': Expected exactly 13 elements, got #{values.length}." unless values.length == 13
43
41
  p = self.new(parent)
44
42
  # Assign the values to attributes:
@@ -65,28 +63,35 @@ module RTP
65
63
  # * <tt>parent</tt> -- A Record which is used to determine the proper parent of this instance.
66
64
  #
67
65
  def initialize(parent)
68
- raise ArgumentError, "Invalid argument 'parent'. Expected RTP::Record, got #{parent.class}." unless parent.is_a?(RTP::Record)
69
66
  # Child objects:
70
67
  @site_setup = nil
71
68
  @fields = Array.new
72
- # Parent relation:
73
- @parent = get_parent(parent, Plan)
69
+ # Parent relation (may get more than one type of record here):
70
+ @parent = get_parent(parent.to_record, Plan)
74
71
  @parent.add_prescription(self)
75
72
  @keyword = 'RX_DEF'
76
73
  end
77
74
 
75
+ # Returns true if the argument is an instance with attributes equal to self.
76
+ #
77
+ def ==(other)
78
+ if other.respond_to?(:to_prescription)
79
+ other.send(:state) == state
80
+ end
81
+ end
82
+
83
+ alias_method :eql?, :==
84
+
78
85
  # Adds a treatment Field record to this instance.
79
86
  #
80
87
  def add_field(child)
81
- raise ArgumentError, "Invalid argument 'child'. Expected RTP::Field, got #{child.class}." unless child.is_a?(RTP::Field)
82
- @fields << child
88
+ @fields << child.to_field
83
89
  end
84
90
 
85
91
  # Connects a Site setup record to this instance.
86
92
  #
87
93
  def add_site_setup(child)
88
- raise ArgumentError, "Invalid argument 'child'. Expected RTP::SiteSetup, got #{child.class}." unless child.is_a?(RTP::SiteSetup)
89
- @site_setup = child
94
+ @site_setup = child.to_site_setup
90
95
  end
91
96
 
92
97
  # Returns the a properly sorted array of the child records of this instance.
@@ -95,6 +100,12 @@ module RTP
95
100
  return [@site_setup, @fields].flatten.compact
96
101
  end
97
102
 
103
+ # Generates a Fixnum hash value for this instance.
104
+ #
105
+ def hash
106
+ state.hash
107
+ end
108
+
98
109
  # Returns the values of this instance in an array.
99
110
  # The values does not include the CRC.
100
111
  #
@@ -115,104 +126,109 @@ module RTP
115
126
  ]
116
127
  end
117
128
 
129
+ # Returns self.
130
+ #
131
+ def to_prescription
132
+ self
133
+ end
134
+
118
135
  # Writes the Prescription object + any hiearchy of child objects,
119
136
  # to a properly formatted RTPConnect ascii string.
120
137
  #
121
- def to_str
138
+ def to_s
122
139
  str = encode
123
140
  if children
124
141
  children.each do |child|
125
- str += child.to_str
142
+ str += child.to_s
126
143
  end
127
144
  end
128
145
  return str
129
146
  end
130
147
 
148
+ alias :to_str :to_s
149
+
131
150
  # Sets the keyword attribute.
132
151
  #
133
152
  def keyword=(value)
134
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
135
- raise ArgumentError, "Invalid keyword. Expected 'RX_DEF', got #{value}." unless value.upcase == "RX_DEF"
153
+ value = value.to_s.upcase
154
+ raise ArgumentError, "Invalid keyword. Expected 'RX_DEF', got #{value}." unless value == "RX_DEF"
136
155
  @keyword = value
137
156
  end
138
157
 
139
158
  # Sets the course_id attribute.
140
159
  #
141
160
  def course_id=(value)
142
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
143
- @course_id = value
161
+ @course_id = value && value.to_s
144
162
  end
145
163
 
146
164
  # Sets the rx_site_name attribute.
147
165
  #
148
166
  def rx_site_name=(value)
149
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
150
- @rx_site_name = value
167
+ @rx_site_name = value && value.to_s
151
168
  end
152
169
 
153
170
  # Sets the technique attribute.
154
171
  #
155
172
  def technique=(value)
156
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
157
- @technique = value
173
+ @technique = value && value.to_s
158
174
  end
159
175
 
160
176
  # Sets the modality attribute.
161
177
  #
162
178
  def modality=(value)
163
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
164
- @modality = value
179
+ @modality = value && value.to_s
165
180
  end
166
181
 
167
182
  # Sets the dose_spec attribute.
168
183
  #
169
184
  def dose_spec=(value)
170
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
171
- @dose_spec = value
185
+ @dose_spec = value && value.to_s
172
186
  end
173
187
 
174
188
  # Sets the rx_depth attribute.
175
189
  #
176
190
  def rx_depth=(value)
177
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
178
- @rx_depth = value
191
+ @rx_depth = value && value.to_s
179
192
  end
180
193
 
181
194
  # Sets the dose_ttl attribute.
182
195
  #
183
196
  def dose_ttl=(value)
184
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
185
- @dose_ttl = value
197
+ @dose_ttl = value && value.to_s
186
198
  end
187
199
 
188
200
  # Sets the dose_tx attribute.
189
201
  #
190
202
  def dose_tx=(value)
191
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
192
- @dose_tx = value
203
+ @dose_tx = value && value.to_s
193
204
  end
194
205
 
195
206
  # Sets the pattern attribute.
196
207
  #
197
208
  def pattern=(value)
198
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
199
- @pattern = value
209
+ @pattern = value && value.to_s
200
210
  end
201
211
 
202
212
  # Sets the rx_note attribute.
203
213
  #
204
214
  def rx_note=(value)
205
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
206
- @rx_note = value
215
+ @rx_note = value && value.to_s
207
216
  end
208
217
 
209
218
  # Sets the number_of_fields attribute.
210
219
  #
211
220
  def number_of_fields=(value)
212
- raise ArgumentError, "Invalid argument 'value'. Expected String, got #{value.class}." unless value.is_a?(String)
213
- @number_of_fields = value
221
+ @number_of_fields = value && value.to_s
214
222
  end
215
223
 
224
+
225
+ private
226
+
227
+
228
+ # Returns the attributes of this instance in an array (for comparison purposes).
229
+ #
230
+ alias_method :state, :values
231
+
216
232
  end
217
233
 
218
234
  end