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.



446
447
448
449
450
# File 'lib/mini_magick.rb', line 446

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



456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/mini_magick.rb', line 456

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.



443
444
445
# File 'lib/mini_magick.rb', line 443

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



444
445
446
# File 'lib/mini_magick.rb', line 444

def command
  @command
end

Instance Method Details

#+(*options) ⇒ Object



471
472
473
474
475
476
477
478
# File 'lib/mini_magick.rb', line 471

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



480
481
482
483
484
485
486
487
# File 'lib/mini_magick.rb', line 480

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



493
494
495
496
497
498
499
500
501
# File 'lib/mini_magick.rb', line 493

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



489
490
491
# File 'lib/mini_magick.rb', line 489

def escape_string(value)
  '"' + value + '"'
end

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



503
504
505
# File 'lib/mini_magick.rb', line 503

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