Class: MiniMagick::Tool
- Inherits:
-
Object
- Object
- MiniMagick::Tool
- Defined in:
- lib/mini_magick/tool.rb
Overview
Class that wraps command-line tools directly, as opposed MiniMagick::Image which is more high-level.
Constant Summary collapse
- CREATION_OPERATORS =
%w[xc canvas logo rose gradient radial-gradient plasma pattern text pango]
Instance Attribute Summary collapse
- #args ⇒ Object readonly
- #name ⇒ Object readonly
Class Method Summary collapse
-
.new(*args, **options) ⇒ MiniMagick::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 image paths.
-
#call(**options) {|Array| ... } ⇒ String
Executes the command that has been built up.
-
#clone(*args) ⇒ Object
This option is a valid ImageMagick option, but it's also a Ruby method, so we need to override it so that it correctly acts as an option method.
-
#command ⇒ Array<String>
The currently built-up command.
-
#executable ⇒ Array<String>
The executable used for this tool.
-
#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.
-
#operator ⇒ Object
Define creator operator methods.
-
#stack(*args) {|_self| ... } ⇒ Object
Create an ImageMagick stack in the command (surround..
-
#stdin ⇒ Object
Adds ImageMagick's pseudo-filename
-for standard input. -
#stdout ⇒ Object
Adds ImageMagick's pseudo-filename
-for standard output.
Constructor Details
#initialize(name, **options) ⇒ Tool
Returns a new instance of Tool.
55 56 57 58 59 |
# File 'lib/mini_magick/tool.rb', line 55 def initialize(name, **) @name = name @args = [] @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
258 259 260 261 262 263 |
# File 'lib/mini_magick/tool.rb', line 258 def method_missing(name, *args) option = "-#{name.to_s.tr('_', '-')}" self << option self.merge!(args) self end |
Instance Attribute Details
#args ⇒ Object (readonly)
42 43 44 |
# File 'lib/mini_magick/tool.rb', line 42 def args @args end |
#name ⇒ Object (readonly)
42 43 44 |
# File 'lib/mini_magick/tool.rb', line 42 def name @name end |
Class Method Details
.new(*args, **options) ⇒ MiniMagick::Tool, String
Aside from classic instantiation, it also accepts a block, and then executes the command in the end.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mini_magick/tool.rb', line 30 def self.new(*args, **) instance = super if block_given? yield instance instance.call else instance end end |
Instance Method Details
#+(*values) ⇒ self
Changes the last operator to its "plus" form.
163 164 165 166 167 |
# File 'lib/mini_magick/tool.rb', line 163 def +(*values) args[-1] = args[-1].sub(/^-/, '+') self.merge!(values) self end |
#<<(arg) ⇒ self
Appends raw options, useful for appending image paths.
136 137 138 139 |
# File 'lib/mini_magick/tool.rb', line 136 def <<(arg) args << arg.to_s self end |
#call(**options) {|Array| ... } ⇒ String
Executes the command that has been built up.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mini_magick/tool.rb', line 81 def call(**) = @options.merge() [:warnings] = false if block_given? shell = MiniMagick::Shell.new stdout, stderr, status = shell.run(command, **) yield stdout, stderr, status if block_given? stdout.chomp("\n") end |
#clone(*args) ⇒ Object
This option is a valid ImageMagick option, but it's also a Ruby method, so we need to override it so that it correctly acts as an option method.
243 244 245 246 247 |
# File 'lib/mini_magick/tool.rb', line 243 def clone(*args) self << '-clone' self.merge!(args) self end |
#command ⇒ Array<String>
The currently built-up command.
103 104 105 |
# File 'lib/mini_magick/tool.rb', line 103 def command [*executable, *args] end |
#executable ⇒ Array<String>
The executable used for this tool. Respects Configuration#cli_prefix.
124 125 126 127 128 129 |
# File 'lib/mini_magick/tool.rb', line 124 def executable exe = Array(MiniMagick.cli_prefix).dup exe << "magick" if MiniMagick.imagemagick7? && name != "magick" exe << "gm" if MiniMagick.graphicsmagick exe << name end |
#merge!(new_args) ⇒ self
Merges a list of raw options.
146 147 148 149 |
# File 'lib/mini_magick/tool.rb', line 146 def merge!(new_args) new_args.each { |arg| self << arg } self end |
#operator ⇒ Object
Define creator operator methods
232 233 234 235 236 237 |
# File 'lib/mini_magick/tool.rb', line 232 CREATION_OPERATORS.each do |operator| define_method(operator.tr('-', '_')) do |value = nil| self << "#{operator}:#{value}" self end end |
#stack(*args) {|_self| ... } ⇒ Object
Create an ImageMagick stack in the command (surround.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/mini_magick/tool.rb', line 184 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 ImageMagick's pseudo-filename - for standard input.
205 206 207 |
# File 'lib/mini_magick/tool.rb', line 205 def stdin self << "-" end |
#stdout ⇒ Object
Adds ImageMagick's pseudo-filename - for standard output.
220 221 222 |
# File 'lib/mini_magick/tool.rb', line 220 def stdout self << "-" end |