Class: String

Inherits:
Object
  • Object
show all
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

Instance Method Details

#dicom_method?Boolean

Checks if a string value LOOKS like a DICOM method name - it may still not be valid one.

Returns:

  • (Boolean)

    true if a string looks like a DICOM method name, and false if not



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.

Returns:

  • (Boolean)

    true if a string looks like a DICOM name, and false if not



13
14
15
# File 'lib/dicom/extensions/string.rb', line 13

def dicom_name?
  self == self.dicom_titleize
end

#dicom_titleizeString

Capitalizes all the words in the string and replaces some characters to make a nicer looking title.

Returns:

  • (String)

    a formatted, capitalized string



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_underscoreString

Makes an underscored, lowercased version of the string.

Returns:

  • (String)

    an underscored, lower case 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>

Note:

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.

Parameters:

  • parts (Integer)

    the number of sub-strings to create

Returns:

Raises:

  • (ArgumentError)


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

#elementString

Extracts the element part of the tag string: The last 4 characters.

Returns:

  • (String)

    the element part of the tag



68
69
70
# File 'lib/dicom/extensions/string.rb', line 68

def element
  return self[5..8]
end

#groupString

Returns the group part of the tag string: The first 4 characters.

Returns:

  • (String)

    the group part of the tag



76
77
78
# File 'lib/dicom/extensions/string.rb', line 76

def group
  return self[0..3]
end

#group_lengthString

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.

Returns:

  • (String)

    a group length 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’).

Returns:

  • (Boolean)

    true if it is a group length tag, and false if not



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).

Returns:

  • (Boolean)

    true if it is a private tag, and false if not



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’).

Returns:

  • (Boolean)

    true if it is a valid tag, and false if not



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_methodSymbol

Converts the string to a proper DICOM element method name symbol.

Returns:

  • (Symbol)

    a DICOM element method name



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