Class: GDCM::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/gdcm/package.rb,
lib/gdcm/package/info.rb

Defined Under Namespace

Classes: Info

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, tempfile = nil) ⇒ Package

Returns a new instance of Package.



88
89
90
91
# File 'lib/gdcm/package.rb', line 88

def initialize(input_path, tempfile = nil)
  @path = input_path.to_s
  @tempfile = tempfile
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



86
87
88
# File 'lib/gdcm/package.rb', line 86

def meta
  @meta
end

#pathObject (readonly)

Returns the value of attribute path.



84
85
86
# File 'lib/gdcm/package.rb', line 84

def path
  @path
end

#tempfileObject (readonly)

Returns the value of attribute tempfile.



85
86
87
# File 'lib/gdcm/package.rb', line 85

def tempfile
  @tempfile
end

Class Method Details

.create(ext = nil, validate = GDCM.validate_on_create) {|Tempfile| ... } ⇒ GDCM::Image

Used to create a new file object data-copy.

Takes an extension in a block and can be used to build a new file object. Used by both open and read to create a new object. Ensures we have a good tempfile.

Parameters:

  • ext (String) (defaults to: nil)

    Specify the extension you want to read it as

  • validate (Boolean) (defaults to: GDCM.validate_on_create)

    If false, skips validation of the created file. Defaults to true.

Yields:

  • (Tempfile)

    You can #write bits to this object to create the new file

Returns:

  • (GDCM::Image)

    The created file



76
77
78
79
80
81
82
# File 'lib/gdcm/package.rb', line 76

def self.create(ext = nil, validate = GDCM.validate_on_create, &block)
  tempfile = GDCM::Utilities.tempfile(ext.to_s.downcase, &block)

  new(tempfile.path, tempfile).tap do |file|
    file.validate! if validate
  end
end

.open(path, ext = nil, options = {}) ⇒ GDCM::Image

Opens a specific file either on the local file system. Use this if you don’t want to overwrite file.

Extension is either guessed from the path or you can specify it as a second parameter.

Parameters:

  • path (String)

    Either a local file path

  • ext (String) (defaults to: nil)

    Specify the extension you want to read it as

  • options (Hash) (defaults to: {})

    Specify options for the open method

Returns:

  • (GDCM::Image)

    The loaded file



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gdcm/package.rb', line 44

def self.open(path, ext = nil, options = {})
  options, ext = ext, nil if ext.is_a?(Hash)

  # Don't use Kernel#open, but reuse its logic
  openable =
    if path.respond_to?(:open)
      path
    else
      options = { binmode: true }.merge(options)
      Pathname(path)
    end

  ext ||= File.extname(openable.to_s)
  ext.sub!(/:.*/, '') # hack for filenames that include a colon

  openable.open(**options) { |file| read(file, ext) }
end

.read(stream, ext = nil) ⇒ GDCM::Image

This is the primary loading method used by all of the other class methods.

Use this to pass in a stream object. Must respond to #read(size) or be a binary string object (BLOB)

Parameters:

  • stream (#read, String)

    Some kind of stream object that needs to be read or is a binary String blob

  • ext (String) (defaults to: nil)

    A manual extension to use for reading the file. Not required, but if you are having issues, give this a try.

Returns:

  • (GDCM::Image)


24
25
26
27
28
29
30
# File 'lib/gdcm/package.rb', line 24

def self.read(stream, ext = nil)
  if stream.is_a?(String)
    stream = StringIO.new(stream)
  end

  create(ext) { |file| IO.copy_stream(stream, file) }
end

Instance Method Details

#convertObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gdcm/package.rb', line 114

def convert
  if @tempfile
    new_tempfile = GDCM::Utilities.tempfile(".dcm")
    new_path = new_tempfile.path
  else
    new_path = Pathname(path).sub_ext(".dcm").to_s
  end

  input_path = path.dup

  GDCM::Tool::Convert.new do |convert|
    yield convert if block_given?
    convert << input_path
    convert << new_path
  end

  if @tempfile
    destroy!
    @tempfile = new_tempfile
  else
    File.delete(path) unless path == new_path
  end

  path.replace new_path

  info.meta = nil

  self
rescue GDCM::Invalid, GDCM::Error => e
  new_tempfile.unlink if new_tempfile && @tempfile != new_tempfile
  raise e
end

#destroy!Object

Destroys the tempfile (created by open) if it exists.



167
168
169
170
171
172
# File 'lib/gdcm/package.rb', line 167

def destroy!
  if @tempfile
    FileUtils.rm_f @tempfile.path.sub(/mpc$/, "cache") if @tempfile.path.end_with?(".mpc")
    @tempfile.unlink
  end
end

#infoObject



93
94
95
# File 'lib/gdcm/package.rb', line 93

def info
  @info ||= GDCM::Package::Info.new(self)
end

#to_blobObject



97
98
99
# File 'lib/gdcm/package.rb', line 97

def to_blob
  File.binread(path)
end

#valid?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/gdcm/package.rb', line 101

def valid?
  validate!
  true
rescue GDCM::Invalid
  false
end

#validate!Object



108
109
110
111
112
# File 'lib/gdcm/package.rb', line 108

def validate!
  info.meta
rescue GDCM::Error => error
  raise GDCM::Invalid, error.message
end

#write(output_to) ⇒ Object

Writes the temporary file out to either a file location (by passing in a String) or by passing in a Stream that you can #write(chunk) to repeatedly

Parameters:

  • output_to (String, Pathname, #read)

    Some kind of stream object that needs to be read or a file path as a String



155
156
157
158
159
160
161
162
# File 'lib/gdcm/package.rb', line 155

def write(output_to)
  case output_to
  when String, Pathname
    FileUtils.copy_file path, output_to unless path == output_to.to_s
  else
    IO.copy_stream File.open(path, "rb"), output_to
  end
end