Class: PdfTools::Tool
- Inherits:
-
Object
- Object
- PdfTools::Tool
- Defined in:
- lib/pdf_tools/tool.rb,
lib/pdf_tools/tool/merge_split.rb,
lib/pdf_tools/tool/image_to_pdf.rb
Overview
Abstract class that wraps command-line tools.
Direct Known Subclasses
Defined Under Namespace
Classes: ImageToPdf, MergeSplit
Constant Summary collapse
- KEY_VALUE_OPTIONS =
%w[i]
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .available_options ⇒ Object
-
.new(*args) ⇒ PdfTools::Tool, String
Execute the command.
Instance Method Summary collapse
-
#<<(arg) ⇒ self
Append raw options.
-
#call(*args) {|Array| ... } ⇒ String
Build and execute the command.
-
#command ⇒ Array<String>
The built command as array.
-
#initialize(name) ⇒ Tool
constructor
A new instance of Tool.
-
#merge!(add_args) ⇒ self
Merge a list of raw options.
-
#method_missing(name, *args) ⇒ Object
Any missing method will be transformed into a CLI option.
Constructor Details
#initialize(name) ⇒ Tool
Returns a new instance of Tool.
39 40 41 42 |
# File 'lib/pdf_tools/tool.rb', line 39 def initialize(name) @name = name @args = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Any missing method will be transformed into a CLI option
128 129 130 131 132 133 |
# File 'lib/pdf_tools/tool.rb', line 128 def method_missing(name, *args) option = "-#{name.to_s.tr("_", "-")}" self << option merge!(args) self end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
37 38 39 |
# File 'lib/pdf_tools/tool.rb', line 37 def args @args end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
37 38 39 |
# File 'lib/pdf_tools/tool.rb', line 37 def name @name end |
Class Method Details
.available_options ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/pdf_tools/tool.rb', line 135 def self. tool = new tool << "-version" help_page = tool.call(stderr: false) = help_page.scan(/^\s+-[a-z-]+/).map(&:strip) .map { |o| o[1..].tr("-", "_") } end |
.new(*args) ⇒ PdfTools::Tool, String
Execute the command.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pdf_tools/tool.rb', line 26 def self.new(*args) instance = super(*args) if block_given? yield instance instance.call else instance end end |
Instance Method Details
#<<(arg) ⇒ self
Append raw options.
92 93 94 95 |
# File 'lib/pdf_tools/tool.rb', line 92 def <<(arg) args << arg.to_s self end |
#call(*args) {|Array| ... } ⇒ String
Build and execute the command.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pdf_tools/tool.rb', line 63 def call(*args) = args[-1].is_a?(Hash) ? args.pop : {} [:stderr] = false if block_given? shell = PdfTools::Shell.new stdout, stderr, status = shell.run(command, ) yield stdout, stderr, status if block_given? stdout.chomp("\n") end |
#command ⇒ Array<String>
The built command as array.
85 86 87 |
# File 'lib/pdf_tools/tool.rb', line 85 def command [*name, *args] end |
#merge!(add_args) ⇒ self
Merge a list of raw options.
100 101 102 103 |
# File 'lib/pdf_tools/tool.rb', line 100 def merge!(add_args) add_args.each { |arg| self << arg } self end |