Class: GDCM::Tool
- Inherits:
-
Object
- Object
- GDCM::Tool
- 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.
Defined Under Namespace
Classes: Convert, Dump, Identify
Instance Attribute Summary collapse
- #args ⇒ Object readonly
- #name ⇒ Object readonly
Class Method Summary collapse
-
.new(*args) ⇒ GDCM::Tool, String
Aside from classic instantiation, it also accepts a block, and then executes the command in the end.
Instance Method Summary collapse
-
#+(*values) ⇒ self
Changes the last operator to its “plus” form.
-
#<<(arg) ⇒ self
Appends raw options, useful for appending file paths.
-
#call(*args) {|Array| ... } ⇒ String
Executes the command that has been built up.
-
#command ⇒ Array<String>
The currently built-up command.
- #executable ⇒ Object
-
#initialize(name, options = {}) ⇒ Tool
constructor
A new instance of Tool.
-
#merge!(new_args) ⇒ self
Merges a list of raw options.
-
#method_missing(name, *args) ⇒ Object
Any undefined method will be transformed into a CLI option.
-
#stack(*args) {|_self| ... } ⇒ Object
Create an GDCM stack in the command (surround..
-
#stdin ⇒ Object
Adds GDCM’s pseudo-filename ‘-` for standard input.
-
#stdout ⇒ Object
Adds GDCM’s pseudo-filename ‘-` for standard output.
Constructor Details
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
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
#args ⇒ Object (readonly)
35 36 37 |
# File 'lib/gdcm/tool.rb', line 35 def args @args end |
#name ⇒ Object (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.
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.
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.
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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gdcm/tool.rb', line 71 def call(*args) = args[-1].is_a?(Hash) ? args.pop : {} whiny = args.fetch(0, @whiny) [:whiny] = whiny [:stderr] = false if block_given? shell = GDCM::Shell.new stdout, stderr, status = shell.run(command, ) yield stdout, stderr, status if block_given? stdout.chomp("\n") end |
#command ⇒ Array<String>
The currently built-up command.
96 97 98 |
# File 'lib/gdcm/tool.rb', line 96 def command [*executable, *args] end |
#executable ⇒ Object
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.
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.
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 |
#stdin ⇒ Object
Adds GDCM’s pseudo-filename ‘-` for standard input.
179 180 181 |
# File 'lib/gdcm/tool.rb', line 179 def stdin self << "-" end |
#stdout ⇒ Object
Adds GDCM’s pseudo-filename ‘-` for standard output.
194 195 196 |
# File 'lib/gdcm/tool.rb', line 194 def stdout self << "-" end |