Class: FileFilter::Formatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Formatter

Returns a new instance of Formatter.



440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/inplace.rb', line 440

def initialize(template)
  @template = template.dup

  begin
    self.format("0", "1", "2")
  rescue => e
    raise e
  end

  if @arity == 0
    @template = "(#{@template}) < %1 > %2"
    @arity = 2
  end
end

Instance Attribute Details

#arityObject (readonly)

Returns the value of attribute arity.



455
456
457
# File 'lib/inplace.rb', line 455

def arity
  @arity
end

#templateObject (readonly)

Returns the value of attribute template.



455
456
457
# File 'lib/inplace.rb', line 455

def template
  @template
end

Instance Method Details

#format(origfile, infile, outfile = nil) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/inplace.rb', line 457

def format(origfile, infile, outfile = nil)
  s = ''
  template = @template.dup
  arity_bits = 0

  until template.empty?
    template.sub!(/\A([^%]+)/) {
      s << $1
      ''
    }
    template.sub!(/\A%(.)/) {
      case c = $1
      when '%'
        s << c
      when '0'
        s << origfile.shellescape
      when '1'
        s << infile.shellescape
        arity_bits |= 0x1
      when '2'
        s << outfile.shellescape
        arity_bits |= 0x2
      else
        raise ArgumentError, "invalid placeholder specification (%#{c}): #{@template}"
      end
      ''
    }
  end

  case arity_bits
  when 0x0
    @arity = 0
  when 0x1
    @arity = 1
  when 0x2
    raise ArgumentError, "%1 is missing while %2 is specified: #{@template}"
  when 0x3
    @arity = 2
  end

  return s
end