Dicom_UID 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dicom_uid.rb +76 -29
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e97f4395a5d889ad19cadea15ef9a2f67da63e5964fffaef298e07bd7ca2a83
4
- data.tar.gz: 4f21101d69cbbb846aa9a7ae1ab8e10cee598f7aecc7e34d03879f2d699d28fc
3
+ metadata.gz: adc24bf89d5304fe5b569c6265b828b2942eaa8e60c5d5e1a560ff58534828c9
4
+ data.tar.gz: 640b2f0d0f42a2f4a4419790c609c640dafc6e40cab158686ed3fe944ee3eaae
5
5
  SHA512:
6
- metadata.gz: 5976568e36b27869d5d2caf36bd41b0356695021c264d29fc84c2f0071f48868f396737015900bfaf27bbf89d4a7e5ab4bdea36d415af380170c72fe41d1f777
7
- data.tar.gz: d375cac73d04c5d1820b5c035ad9742d31954d06ff25577eed934d6a7b09b76a62cfff79f4a25a010ed2ce61f5671f2c4a88d0de81694300ac0facafd990d9f7
6
+ metadata.gz: c010e4bcea0c9d193732fccbe3770605f72aa34611bd0b5586ffc5cc2b846b6c8998853d67bcece7e8ac1f0e9c693d0ca94a7fa3f0109e93c39f03e212be1b56
7
+ data.tar.gz: fce7c3a9ffc3d6dd3d525307f6466eae2ae3c07ac50b6137fb966060ecca2613a26a8ccc9cd0a70eeafdfc08265d8df3c970eee446679dca388fc1227c4214f0
data/lib/dicom_uid.rb CHANGED
@@ -4,24 +4,38 @@
4
4
  # The UID looks like the following <org>.<suffix>
5
5
  # UID is composed of two main components, org and suffix. Components are separated by a dot and are only composed of decimals.
6
6
  # The UID must not be more than 64 characters, dots included.
7
- module DicomUID
7
+ module DicomUID#:nodoc:
8
8
 
9
+ # Maximum size of an UID
10
+ MAXIMUM_SIZE = 64
9
11
 
10
- # Generates random component with random, defined, maximum length
11
- def self.random_component length, odd_byte_boundary
12
+
13
+ # Generates random component with defined, maximum length
14
+ # The maximum length is 62. Why ? Because an UID has at least an org_root
15
+ # and a suffix, and they are separated by a dot, which makes
16
+ # 1 character minimum and 62 characters maximum
17
+ def self.random_component length = 62, odd_byte_boundary = true
18
+
19
+ # exceptions
20
+ raise TypeError unless length.is_a? Integer and odd_byte_boundary == !!odd_byte_boundary
21
+ raise RangeError, "Length of a component cannot be negative or null" if length <= 0
22
+ raise OversizedUIDError if length > 62
12
23
 
13
24
  # randing length of number
14
- length_component = length - 1
15
- loop do
25
+ length_component = 0
26
+ while (length_component == length - 1) or length_component == 0
16
27
  srand
17
- length_component = rand(0..length)
18
- break if length_component != length - 1 and length_component != 0
28
+ length_component = rand 0..length
19
29
  end
20
30
 
21
31
  # randing the component
22
- component = (rand ('9' * length_component).to_i).to_s
32
+ component = '9' * (length - 1)
33
+ while component.length == length - 1
34
+ component = (rand ('9' * length_component).to_i).to_s
35
+ end
23
36
 
24
- if odd_byte_boundary and component[-1].to_i % 2 == 1# if odd number
37
+
38
+ if odd_byte_boundary and !odd_byte_rule component# if odd number
25
39
  component << 'O'
26
40
  component = component[1..-1]# removing first int
27
41
  end
@@ -30,31 +44,26 @@ module DicomUID
30
44
  end
31
45
 
32
46
 
33
- # Generates recursively a random dicom uid
34
- def self.rand_duid uid, remain, obb
35
- comp = self.random_component remain, obb
36
- remain -= comp.length
37
- uid << comp
38
-
39
- return uid if remain <= 0
40
-
41
- uid << '.'
42
- self.rand_duid uid, remain - 1, obb
43
- end
44
47
 
45
48
 
46
49
  # set default values, with org_root if needed
47
50
  # the size of the UID is randomized
51
+ # fixed_size is the size ogf the whole UID, with the org taken into account
48
52
  def self.random_dicom_uid org_root, fixed_size, odd_byte_boundary = true
49
53
 
54
+ raise TypeError, 'Org root must be a string or an Integer' unless org_root.is_a? String or org_root.is_a? Integer
55
+ org_root = org_root.to_s if org_root.is_a? Integer
56
+ raise TypeError unless fixed_size.is_a? Integer and odd_byte_boundary == !!odd_byte_boundary
57
+
50
58
  # building the org root
51
- org_root = self.random_component(64, odd_byte_boundary) if org_root.empty?# UID needs at least an org root
59
+ org_root = self.random_component(62, odd_byte_boundary) if org_root.empty?# UID needs at least an org root
52
60
  raise LeadingZeroError if org_root[0] == '0' and org_root.length != 1
53
- raise OddByteError if org_root[-2].to_i % 2 == 1 and org_root[-1] != 0 and odd_byte_boundary
54
- org_root << '.' if org_root[-1] != '.'
61
+ raise OddByteError if !odd_byte_rule(org_root) and odd_byte_boundary
55
62
 
56
- srand
57
- fixed_size ||= rand 64
63
+ # if the fixed size doesn't exist, a random one is created, but
64
+ # it must be generated so that the UID musn't be above 64 chars
65
+ fixed_size ||= rand(0..(64 - org_root.length - 1))
66
+ fixed_size -= 1 if add_missing_dot org_root
58
67
 
59
68
  raise OversizedUIDError if fixed_size > 64
60
69
  raise RangeError("Size of UID can't be negative") if fixed_size < 0
@@ -65,14 +74,11 @@ module DicomUID
65
74
 
66
75
 
67
76
 
68
-
69
-
70
-
71
77
  def self.random_uids org_root, fixed_size, array_size, odd_byte_boundary = true
72
78
  uids = Array.new
73
79
 
74
80
  array_size.times do
75
- uids << random_dicom_uid org_root, fixed_size, odd_byte_boundary
81
+ uids << (self.random_dicom_uid org_root, fixed_size, odd_byte_boundary)
76
82
  end
77
83
 
78
84
  uids
@@ -80,9 +86,50 @@ module DicomUID
80
86
 
81
87
 
82
88
 
89
+ private
83
90
 
91
+ # Generates recursively a random dicom uid
92
+ def self.rand_duid uid, remain, obb = true
93
+
94
+ # Exceptions
95
+ raise OversizedUIDError if uid.length > 64 or (uid.length + remain > 64)
96
+ raise RangeError, "Remaining characters can't be negative or null" if remain <= 0
97
+
98
+ comp = self.random_component remain, obb
99
+ remain -= comp.length
100
+ uid << comp
101
+
102
+ return uid if remain <= 0
103
+
104
+ uid << '.'
105
+ self.rand_duid uid, remain - 1, obb
106
+ end
107
+
108
+
109
+
110
+
111
+
112
+
113
+ end
114
+
115
+
116
+ def add_missing_dot comp
117
+ raise TypeError unless comp.is_a? String
118
+ if comp[-1] != '.'
119
+ comp << '.'
120
+ return true
121
+ end
122
+ false
84
123
  end
85
124
 
125
+
126
+ def odd_byte_rule comp
127
+ return comp[-1].to_i % 2 == 0 || (comp[-2].to_i % 2 == 1 && comp[-1].to_i == 0) if comp.length != 1
128
+ comp[-1].to_i % 2 == 0
129
+ end
130
+
131
+
132
+
86
133
  # EXCEPTIONS
87
134
 
88
135
  class LeadingZeroError < StandardError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Dicom_UID
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Repain Paul