Class: MiniMagick::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, *options) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



452
453
454
455
456
# File 'lib/mini_magick.rb', line 452

def initialize(command, *options)
  @command = command
  @args = []
  options.each { |arg| push(arg) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *options) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/mini_magick.rb', line 462

def method_missing(symbol, *options)
  guessed_command_name = symbol.to_s.gsub('_','-')
  if guessed_command_name == "format"
    raise Error, "You must call 'format' on the image object directly!"
  elsif MOGRIFY_COMMANDS.include?(guessed_command_name)
    add_command(guessed_command_name, *options)
    self
  elsif IMAGE_CREATION_OPERATORS.include?(guessed_command_name)
    add_creation_operator(guessed_command_name, *options)
    self
  else
    super(symbol, *args)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



449
450
451
# File 'lib/mini_magick.rb', line 449

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



450
451
452
# File 'lib/mini_magick.rb', line 450

def command
  @command
end

Instance Method Details

#+(*options) ⇒ Object



477
478
479
480
481
482
483
484
# File 'lib/mini_magick.rb', line 477

def +(*options)
  push(@args.pop.gsub(/^-/, '+'))
  if options.any?
    options.each do |o|
      push escape_string(o)
    end
  end
end

#add_command(command, *options) ⇒ Object



486
487
488
489
490
491
492
493
# File 'lib/mini_magick.rb', line 486

def add_command(command, *options)
  push "-#{command}"
  if options.any?
    options.each do |o|
      push escape_string(o)
    end
  end
end

#add_creation_operator(command, *options) ⇒ Object



499
500
501
502
503
504
505
506
507
# File 'lib/mini_magick.rb', line 499

def add_creation_operator(command, *options)
  creation_command = command
  if options.any?
    options.each do |option|
      creation_command << ":#{option}"
    end
  end
  push creation_command
end

#escape_string(value) ⇒ Object



495
496
497
# File 'lib/mini_magick.rb', line 495

def escape_string(value)
  Shellwords.escape(value.to_s)
end

#push(arg) ⇒ Object Also known as: <<



509
510
511
# File 'lib/mini_magick.rb', line 509

def push(arg)
  @args << arg.to_s.strip
end