Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dicom/extensions/string.rb
Overview
Extension to the String class. These mainly facilitate the processing and analysis of element tags. A tag string (as used by the ruby-dicom library) is 9 characters long and of the form ‘GGGG,EEEE’ (where G represents a group hexadecimal, and E represents an element hexadecimal).
Instance Method Summary collapse
-
#dicom_method? ⇒ Boolean
Checks if a string value LOOKS like a DICOM method name - it may still not be valid one.
-
#dicom_name? ⇒ Boolean
Checks if a string value LOOKS like a DICOM name - it may still not be valid one.
-
#dicom_titleize ⇒ String
Capitalizes all the words in the string and replaces some characters to make a nicer looking title.
-
#dicom_underscore ⇒ String
Makes an underscored, lowercased version of the string.
-
#divide(parts) ⇒ Array<String>
Divides a string into a number of sub-strings of exactly equal length.
-
#element ⇒ String
Extracts the element part of the tag string: The last 4 characters.
-
#group ⇒ String
Returns the group part of the tag string: The first 4 characters.
-
#group_length ⇒ String
Returns the “Group Length” (‘GGGG,0000’) tag which corresponds to the original tag/group string.
-
#group_length? ⇒ Boolean
Checks if the string is a “Group Length” tag (its element part is ‘0000’).
-
#private? ⇒ Boolean
Checks if the string is a private tag (has an odd group number).
-
#tag? ⇒ Boolean
Checks if the string is a valid tag (as defined by ruby-dicom: ‘GGGG,EEEE’).
-
#to_element_method ⇒ Symbol
Converts the string to a proper DICOM element method name symbol.
Instance Method Details
#dicom_method? ⇒ Boolean
Checks if a string value LOOKS like a DICOM method name - it may still not be valid one.
21 22 23 |
# File 'lib/dicom/extensions/string.rb', line 21 def dicom_method? self == self.dicom_underscore end |
#dicom_name? ⇒ Boolean
Checks if a string value LOOKS like a DICOM name - it may still not be valid one.
13 14 15 |
# File 'lib/dicom/extensions/string.rb', line 13 def dicom_name? self == self.dicom_titleize end |
#dicom_titleize ⇒ String
Capitalizes all the words in the string and replaces some characters to make a nicer looking title.
29 30 31 |
# File 'lib/dicom/extensions/string.rb', line 29 def dicom_titleize self.dicom_underscore.gsub(/_/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize } end |
#dicom_underscore ⇒ String
Makes an underscored, lowercased version of the string.
37 38 39 40 41 42 |
# File 'lib/dicom/extensions/string.rb', line 37 def dicom_underscore word = self.dup word.tr!('-', '_') word.downcase! word end |
#divide(parts) ⇒ Array<String>
The length of self must be a multiple of parts, or an exception will be raised.
Divides a string into a number of sub-strings of exactly equal length.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dicom/extensions/string.rb', line 50 def divide(parts) parts = parts.to_i raise ArgumentError, "Argument must be in the range <1 - self.length (#{self.length})>. Got #{parts}." if parts < 1 or parts > self.length raise ArgumentError, "Length of self (#{self.length}) must be a multiple of parts (#{parts})." if (self.length/parts.to_f % 1) != 0 sub_strings = Array.new sub_length = self.length/parts start_index = 0 parts.times { sub_strings << self.slice(start_index, sub_length) start_index += sub_length } sub_strings end |
#element ⇒ String
Extracts the element part of the tag string: The last 4 characters.
68 69 70 |
# File 'lib/dicom/extensions/string.rb', line 68 def element return self[5..8] end |
#group ⇒ String
Returns the group part of the tag string: The first 4 characters.
76 77 78 |
# File 'lib/dicom/extensions/string.rb', line 76 def group return self[0..3] end |
#group_length ⇒ String
Returns the “Group Length” (‘GGGG,0000’) tag which corresponds to the original tag/group string. The original string may either be a 4 character group string, or a 9 character custom tag.
85 86 87 88 89 90 91 |
# File 'lib/dicom/extensions/string.rb', line 85 def group_length if self.length == 4 return self + ',0000' else return self.group + ',0000' end end |
#group_length? ⇒ Boolean
Checks if the string is a “Group Length” tag (its element part is ‘0000’).
97 98 99 |
# File 'lib/dicom/extensions/string.rb', line 97 def group_length? return (self.element == '0000' ? true : false) end |
#private? ⇒ Boolean
Checks if the string is a private tag (has an odd group number).
105 106 107 |
# File 'lib/dicom/extensions/string.rb', line 105 def private? return ((self.upcase =~ /\A\h{3}[1,3,5,7,9,B,D,F],\h{4}\z/) == nil ? false : true) end |
#tag? ⇒ Boolean
Checks if the string is a valid tag (as defined by ruby-dicom: ‘GGGG,EEEE’).
113 114 115 116 |
# File 'lib/dicom/extensions/string.rb', line 113 def tag? # Test that the string is composed of exactly 4 HEX characters, followed by a comma, then 4 more HEX characters: return ((self.upcase =~ /\A\h{4},\h{4}\z/) == nil ? false : true) end |
#to_element_method ⇒ Symbol
Converts the string to a proper DICOM element method name symbol.
122 123 124 |
# File 'lib/dicom/extensions/string.rb', line 122 def to_element_method self.gsub(/^3/,'three_').gsub(/[#*?!]/,' ').gsub(', ',' ').gsub('&','and').gsub(' - ','_').gsub(' / ','_').gsub(/[\s\-\.\,\/\\]/,'_').gsub(/[\(\)\']/,'').gsub(/\_+/, '_').downcase.to_sym end |