Class: GDCM::Package
- Inherits:
-
Object
- Object
- GDCM::Package
- Defined in:
- lib/gdcm/package.rb,
lib/gdcm/package/info.rb
Defined Under Namespace
Classes: Info
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tempfile ⇒ Object
readonly
Returns the value of attribute tempfile.
Class Method Summary collapse
-
.create(ext = nil, validate = GDCM.validate_on_create) {|Tempfile| ... } ⇒ GDCM::Image
Used to create a new file object data-copy.
-
.open(path, ext = nil, options = {}) ⇒ GDCM::Image
Opens a specific file either on the local file system.
-
.read(stream, ext = nil) ⇒ GDCM::Image
This is the primary loading method used by all of the other class methods.
Instance Method Summary collapse
- #convert ⇒ Object
-
#destroy! ⇒ Object
Destroys the tempfile (created by Package.open) if it exists.
- #info ⇒ Object
-
#initialize(input_path, tempfile = nil) ⇒ Package
constructor
A new instance of Package.
- #to_blob ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
-
#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.
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
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
86 87 88 |
# File 'lib/gdcm/package.rb', line 86 def @meta end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
84 85 86 |
# File 'lib/gdcm/package.rb', line 84 def path @path end |
#tempfile ⇒ Object (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
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.
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, = {}) , 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 = { binmode: true }.merge() Pathname(path) end ext ||= File.extname(openable.to_s) ext.sub!(/:.*/, '') # hack for filenames that include a colon openable.open(**) { |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)
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
#convert ⇒ Object
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 |
# 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. = nil self end |
#destroy! ⇒ Object
Destroys the tempfile (created by open) if it exists.
164 165 166 167 168 169 |
# File 'lib/gdcm/package.rb', line 164 def destroy! if @tempfile FileUtils.rm_f @tempfile.path.sub(/mpc$/, "cache") if @tempfile.path.end_with?(".mpc") @tempfile.unlink end end |
#info ⇒ Object
93 94 95 |
# File 'lib/gdcm/package.rb', line 93 def info @info ||= GDCM::Package::Info.new(self) end |
#to_blob ⇒ Object
97 98 99 |
# File 'lib/gdcm/package.rb', line 97 def to_blob File.binread(path) end |
#valid? ⇒ 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. rescue GDCM::Error => error raise GDCM::Invalid, error. 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
152 153 154 155 156 157 158 159 |
# File 'lib/gdcm/package.rb', line 152 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 |