Class: GDCM::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/gdcm/tool.rb,
lib/gdcm/tool/dump.rb,
lib/gdcm/tool/convert.rb,
lib/gdcm/tool/identify.rb

Overview

Abstract class that wraps command-line tools. It shouldn’t be used directly, but through one of its subclasses. Use this class if you want to be closer to the metal and execute GDCM commands directly, but still with a nice Ruby interface.

Direct Known Subclasses

Convert, Dump, Identify

Defined Under Namespace

Classes: Convert, Dump, Identify

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Tool

Returns a new instance of Tool.

Examples:

GDCM::Tool::Identify.new(whiny: false) do |identify|
  identify.help # returns exit status 1, which would otherwise throw an error
end

Parameters:

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

Options Hash (options):

  • :whiny (Boolean)

    Whether to raise errors on non-zero exit codes.



45
46
47
48
49
# File 'lib/gdcm/tool.rb', line 45

def initialize(name, options = {})
  @name  = name
  @args  = []
  @whiny = options.is_a?(Hash) ? options.fetch(:whiny, GDCM.whiny) : options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Any undefined method will be transformed into a CLI option

Examples:

convert = GDCM::Tool.new("convert")
convert.adaptive_blur("...")
convert.foo_bar
convert.command.join(" ") # => "convert --adaptive-blur ... --foo-bar"


207
208
209
210
211
212
# File 'lib/gdcm/tool.rb', line 207

def method_missing(name, *args)
  option = "--#{name.to_s.tr('_', '-')}"
  self << option
  self.merge!(args)
  self
end

Instance Attribute Details

#argsObject (readonly)



35
36
37
# File 'lib/gdcm/tool.rb', line 35

def args
  @args
end

#nameObject (readonly)



35
36
37
# File 'lib/gdcm/tool.rb', line 35

def name
  @name
end

Class Method Details

.new(*args) ⇒ GDCM::Tool, String

Aside from classic instantiation, it also accepts a block, and then executes the command in the end.

Examples:

version = GDCM::Tool::Identify.new { |b| b.version }
puts version

Returns:

  • (GDCM::Tool, String)

    If no block is given, returns an instance of the tool, if block is given, returns the output of the command.



23
24
25
26
27
28
29
30
31
32
# File 'lib/gdcm/tool.rb', line 23

def self.new(*args)
  instance = super(*args)

  if block_given?
    yield instance
    instance.call
  else
    instance
  end
end

Instance Method Details

#+(*values) ⇒ self

Changes the last operator to its “plus” form.

Examples:

GDCM::Tool::Convert.new do |convert|
  convert.antialias.+
  convert.distort.+("Perspective", "0,0,4,5 89,0,45,46")
end
# executes `convert +antialias +distort Perspective '0,0,4,5 89,0,45,46'`

Returns:

  • (self)


137
138
139
140
141
# File 'lib/gdcm/tool.rb', line 137

def +(*values)
  args[-1] = args[-1].sub(/^-/, '+')
  self.merge!(values)
  self
end

#<<(arg) ⇒ self

Appends raw options, useful for appending file paths.

Returns:

  • (self)


110
111
112
113
# File 'lib/gdcm/tool.rb', line 110

def <<(arg)
  args << arg.to_s
  self
end

#call(*args) {|Array| ... } ⇒ String

Executes the command that has been built up.

Examples:

convert = GDCM::Tool::Convert.new
convert.resize("500x500")
convert << "path/to/file.dcm"
convert.call # executes `convert --resize 500x500 path/to/file.dcm`
convert = GDCM::Tool::Convert.new
# build the command
convert.call do |stdout, stderr, status|
  # ...
end

Yields:

  • (Array)

    Optionally yields stdout, stderr, and exit status

Returns:

  • (String)

    Returns the output of the command



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gdcm/tool.rb', line 71

def call(*args)
  options = args[-1].is_a?(Hash) ? args.pop : {}
  whiny = args.fetch(0, @whiny)

  options[:whiny] = whiny
  options[:stderr] = false if block_given?

  shell = GDCM::Shell.new
  stdout, stderr, status = shell.run(command, options)
  yield stdout, stderr, status if block_given?

  stdout.chomp("\n")
end

#commandArray<String>

The currently built-up command.

Examples:

convert = GDCM::Tool::Convert.new
convert.resize "500x500"
convert.contrast
convert.command #=> ["convert", "--resize", "500x500", "--contrast"]

Returns:

  • (Array<String>)


96
97
98
# File 'lib/gdcm/tool.rb', line 96

def command
  [*executable, *args]
end

#executableObject



100
101
102
103
# File 'lib/gdcm/tool.rb', line 100

def executable
  exe = [name]
  exe
end

#merge!(new_args) ⇒ self

Merges a list of raw options.

Returns:

  • (self)


120
121
122
123
# File 'lib/gdcm/tool.rb', line 120

def merge!(new_args)
  new_args.each { |arg| self << arg }
  self
end

#stack(*args) {|_self| ... } ⇒ Object

Create an GDCM stack in the command (surround.

Examples:

GDCM::Tool::Convert.new do |convert|
  convert << "1.dcm"
  convert.stack do |stack|
    stack << "2.dcm"
    stack.rotate(30)
  end
  convert.append.+
  convert << "3.dcm"
end
# executes `convert 1.dcm \( 2.dcm --rotate 30 \) +append 3.dcm`

Yields:

  • (_self)

Yield Parameters:

  • _self (GDCM::Tool)

    the object that the method was called on



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gdcm/tool.rb', line 158

def stack(*args)
  self << "("
  args.each do |value|
    case value
    when Hash   then value.each { |key, value| send(key, *value) }
    when String then self << value
    end
  end
  yield self if block_given?
  self << ")"
end

#stdinObject

Adds GDCM’s pseudo-filename ‘-` for standard input.

Examples:

identify = GDCM::Tool::Identify.new
identify.stdin
identify.call(stdin: content)
# executes `identify -` with the given standard input


179
180
181
# File 'lib/gdcm/tool.rb', line 179

def stdin
  self << "-"
end

#stdoutObject

Adds GDCM’s pseudo-filename ‘-` for standard output.

Examples:

content = GDCM::Tool::Convert.new do |convert|
  convert << "1.dcm"
  convert.auto_orient
  convert.stdout
end
# executes `convert 1.dcm --auto-orient -` which returns file contents


194
195
196
# File 'lib/gdcm/tool.rb', line 194

def stdout
  self << "-"
end