Class: Masticate::Cook

Inherits:
Base
  • Object
show all
Defined in:
lib/masticate/cook.rb

Instance Attribute Summary

Attributes inherited from Base

#csv_options, #filename, #input, #input_count, #output, #output_count

Instance Method Summary collapse

Methods inherited from Base

#emit, #execute, #get, #standard_options, #with_input

Constructor Details

#initialize(filename) ⇒ Cook

Returns a new instance of Cook.



10
11
12
# File 'lib/masticate/cook.rb', line 10

def initialize(filename)
  @filename = filename
end

Instance Method Details

#cook(opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/masticate/cook.rb', line 14

def cook(opts)
  standard_options(opts)

  recipefile = opts[:recipe] or raise "missing recipe for cook"
  recipe = File.read(recipefile).lines
  standard_options(opts)

  # ignore blank lines in recipe file
  steps = recipe.grep(/\S/).map do |step|
    argv = Shellwords.split(step)
    masticator = Masticate::MyOptionParser.new
    command, options = masticator.parse(argv)
    masticator.prepare(command, options)
  end

  @output_count = 0
  headers = nil
  with_input do |input|
    while line = get
      row = CSV.parse_line(line, csv_options)

      steps.each do |step|
        if row
          if step.class == Masticate::Mender
            row = step.crunch(row, line, csv_options)
          else
            row = step.crunch(row)
          end
        end
      end

      emit(row) if row
    end
  end
  more_rows = []
  steps.each do |step|
    if more_rows.any?
      more_rows = more_rows.map {|row| step.crunch(row)}
    else
      step.crunch(nil) {|row| more_rows << row}
    end
  end
  more_rows.each {|row| emit(row)}

  @output.close if opts[:output]

  {
    :input_count => @input_count,
    :output_count => @output_count
  }
end