Class: FormatR::Formatter

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

Overview

This is the base class for all the formats, <,>,|, and # of the @ or ^ persuasion. It keeps track of filled variables and the length string should have.

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Formatter

Returns a new instance of Formatter.



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

def initialize (val)
  @len = val.size()
  @filled = false
  if (val =~ /\^.*/)
    @filled = true
  end
end

Instance Method Details

#changeVarValue(var_value, var_name, aBinding) ⇒ Object

if it’s a filled field chop the displayed stuff off in the context given



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/formatr.rb', line 489

def changeVarValue (var_value, var_name, aBinding)
  result = var_value[0, @len]
  max_letter = var_value[0,@len + 1].rindex(' ')
  max_cr = var_value[0,@len + 1].index("\n")
  max_letter = max_cr if (!max_cr.nil?)
  if (var_value.length <= @len)
    result = var_value
    max_letter = @len
  end
  if (max_letter != nil)
    result = var_value[0,max_letter]
  end
  setVarValue( var_name, var_value[result.size(),var_value.size()],
              aBinding)
  return result
end

#formatString(var_value, var_name, aBinding) ⇒ Object

return a formatted string of the correct length



525
526
527
528
529
530
531
# File 'lib/formatr.rb', line 525

def formatString (var_value, var_name, aBinding)
  result = var_value[0,@len]
  if (! @filled)
    return result
  end
  return changeVarValue(var_value, var_name, aBinding)
end

#setVarValue(var_name, var_value, aBinding) ⇒ Object

Move the call to eval into one place we shouldn’t have to



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/formatr.rb', line 508

def setVarValue (var_name, var_value, aBinding)
  if var_value.nil? 
    var_value = ''
  end
  var_value.gsub!(/^\s+/,'')
  if (aBinding.class != binding.class) 
    aBinding[var_name] = var_value
  else
    escaped_var_value = 
      var_value.gsub(/(\\*)'/) { |m| $1.length % 2 == 0 ? $1 + "\\'" : m }
    to_eval = "#{var_name} = '#{escaped_var_value}'";
    #puts "going to eval '#{to_eval}'"
    eval(to_eval, aBinding)
  end
end