Class: FormatR::FormatReader

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

Overview

This class takes in a format and instead of writing out the values variables under the given format will read in formatted text and give the values of variables as specified in the given format.

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ FormatReader

Make a FormatReader given a format



1341
1342
1343
1344
# File 'lib/formatr.rb', line 1341

def initialize (format) 
  @pictures = format.getPictureLines()
  @var_values = Hash.new
end

Instance Method Details

#readFormat(output) ⇒ Object

Given the output from a format return a hash with the values of the variables given in the input mapped to the variables in the format.



1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/formatr.rb', line 1349

def readFormat (output) 
  @var_values = Hash.new
  output_line = 0
  while (output_line < output.length)
    @pictures.each_index do |i|
      repeat = true
      while (repeat)
        found_match = setLine( @pictures[i], output[output_line] )
        repeat = false #default to stopping
        if (found_match)
          output_line += 1
        end
        #we may need to repeat if it's a ~~ line
        if (@pictures[i].repeat() && found_match)
          repeat = true
        end
      end #while
    end
    if block_given?
      yield @var_values
      @var_values = Hash.new
    else
      return @var_values
    end
  end
end