class MiniMagick::Tool::OptionMethods

Dynamically generates modules with dynamically generated option methods for each command-line tool. It uses the `-help` page of a command-line tool and generates methods from it. It then includes the generated module into the tool class.

@private

Public Class Methods

instances() click to toggle source
# File lib/mini_magick/tool.rb, line 185
def self.instances
  @instances ||= []
end
new(tool_name) click to toggle source
# File lib/mini_magick/tool.rb, line 189
def initialize(tool_name)
  @tool_name = tool_name
  reload_methods
  self.class.instances << self
end

Public Instance Methods

cli_options() click to toggle source
# File lib/mini_magick/tool.rb, line 249
def cli_options
  tool = MiniMagick::Tool.new(@tool_name)
  tool << "-help"
  help_page = tool.call(false, stderr: false)

  cli_options = help_page.scan(/^\s+-[a-z\-]+/).map(&:strip)
  cli_options << "-gravity" if @tool_name == "mogrify" && MiniMagick.graphicsmagick?
  cli_options
end
creation_operator(*operators) click to toggle source

Creates method based on creation operator's name.

mogrify = MiniMagick::Tool.new("mogrify")
mogrify.canvas("khaki")
mogrify.command.join(" ") #=> "mogrify canvas:khaki"
# File lib/mini_magick/tool.rb, line 235
def creation_operator(*operators)
  operators.each do |operator|
    define_method(operator.gsub('-', '_')) do |value = nil|
      self << "#{operator}:#{value}"
      self
    end
  end
end
creation_operators() click to toggle source
# File lib/mini_magick/tool.rb, line 244
def creation_operators
  %w[xc canvas logo rose gradient radial-gradient
     plasma tile pattern label caption text]
end
option(*options) click to toggle source

Creates method based on command-line option's name.

mogrify = MiniMagick::Tool.new("mogrify")
mogrify.antialias
mogrify.depth(8)
mogrify.resize("500x500")
mogrify.command.join(" ")
#=> "mogrify -antialias -depth 8 -resize 500x500"
# File lib/mini_magick/tool.rb, line 218
def option(*options)
  options.each do |option|
    define_method(option[1..-1].tr('-', '_')) do |*values|
      self << option
      self.merge!(values)
      self
    end
  end
end
reload_methods() click to toggle source

Dynamically generates operator methods from the “-help” page.

# File lib/mini_magick/tool.rb, line 202
def reload_methods
  instance_methods(false).each { |method| undef_method(method) }
  creation_operator *creation_operators
  option *cli_options
end
to_s() click to toggle source
# File lib/mini_magick/tool.rb, line 195
def to_s
  "OptionMethods(#{@tool_name})"
end