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.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/inplace.rb', line 407

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.



422
423
424
# File 'lib/inplace.rb', line 422

def arity
  @arity
end

#templateObject (readonly)

Returns the value of attribute template.



422
423
424
# File 'lib/inplace.rb', line 422

def template
  @template
end

Instance Method Details

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



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/inplace.rb', line 424

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