Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dicom/ruby_extensions.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
-
#__original_unpack__ ⇒ Object
Renames the original unpack method.
-
#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.
-
#unpack(format) ⇒ Array<String, Integer, Float>
Redefines the core library unpack method, adding the ability to decode signed integers in big endian.
Instance Method Details
#__original_unpack__ ⇒ Object
Renames the original unpack method.
13 |
# File 'lib/dicom/ruby_extensions.rb', line 13 alias __original_unpack__ unpack |
#dicom_method? ⇒ Boolean
Checks if a string value LOOKS like a DICOM method name - it may still not be valid one.
27 28 29 |
# File 'lib/dicom/ruby_extensions.rb', line 27 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.
19 20 21 |
# File 'lib/dicom/ruby_extensions.rb', line 19 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.
35 36 37 |
# File 'lib/dicom/ruby_extensions.rb', line 35 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.
43 44 45 46 47 48 |
# File 'lib/dicom/ruby_extensions.rb', line 43 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dicom/ruby_extensions.rb', line 56 def divide(parts) raise ArgumentError, "Expected an integer (Fixnum). Got #{parts.class}." unless parts.is_a?(Fixnum) 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})." unless (self.length/parts).to_f == self.length/parts.to_f if parts > 1 sub_strings = Array.new sub_length = self.length/parts parts.times { sub_strings << self.slice!(0..(sub_length-1)) } return sub_strings else return [self] end end |
#element ⇒ String
Extracts the element part of the tag string: The last 4 characters.
74 75 76 |
# File 'lib/dicom/ruby_extensions.rb', line 74 def element return self[5..8] end |
#group ⇒ String
Returns the group part of the tag string: The first 4 characters.
82 83 84 |
# File 'lib/dicom/ruby_extensions.rb', line 82 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.
91 92 93 94 95 96 97 |
# File 'lib/dicom/ruby_extensions.rb', line 91 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’).
103 104 105 |
# File 'lib/dicom/ruby_extensions.rb', line 103 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).
111 112 113 |
# File 'lib/dicom/ruby_extensions.rb', line 111 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’).
119 120 121 122 |
# File 'lib/dicom/ruby_extensions.rb', line 119 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.
128 129 130 |
# File 'lib/dicom/ruby_extensions.rb', line 128 def to_element_method self.gsub(/^3/,'three_').gsub(/[#*?!]/,' ').gsub(', ',' ').gsub('&','and').gsub(' - ','_').gsub(' / ','_').gsub(/[\s\-\.\,\/\\]/,'_').gsub(/[\(\)\']/,'').gsub(/\_+/, '_').downcase.to_sym end |
#unpack(format) ⇒ Array<String, Integer, Float>
Redefines the core library unpack method, adding the ability to decode signed integers in big endian.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dicom/ruby_extensions.rb', line 138 def unpack(format) # Check for some custom unpack strings that we've invented: case format when "k*" # SS # Unpack BE US, repack LE US, then finally unpack LE SS: wrongly_unpacked = self.__original_unpack__('n*') repacked = wrongly_unpacked.__original_pack__('S*') correct = repacked.__original_unpack__('s*') when 'r*' # SL # Unpack BE UL, repack LE UL, then finally unpack LE SL: wrongly_unpacked = self.__original_unpack__('N*') repacked = wrongly_unpacked.__original_pack__('I*') correct = repacked.__original_unpack__('l*') else # Call the original method for all other (normal) cases: self.__original_unpack__(format) end end |