Sha256: c0722a35c247425e5511925e004d98165076285ed33967857159492259a0e30d

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

class Masticate::Base
  attr_reader :filename
  attr_reader :input, :output
  attr_reader :input_count, :output_count
  attr_reader :csv_options

  def initialize(args)
    case args
    when String, nil
      @filename = args
    when Hash
      configure(args)
    else
      raise "invalid initialization: #{args}"
    end
  end

  def with_input
    @input = @filename ? open(@filename) : $stdin
    @input_count = 0
    result = yield @input
    @input.close if @filename
    result
  end

  def get
    line = @input.gets
    @input_count += 1
    line && line.chomp
  end

  def emit(line)
    @output_count ||= 0
    @output_count += 1
    begin
      case line
      when Array
        @output.puts line.to_csv
      else
        @output.puts line
      end
    rescue Errno::EPIPE
      # output was closed, e.g. ran piped into `head`
      # silently ignore this condition, it's not fatal and doesn't need a warning
    end
  end

  def standard_options(opts)
    @output = opts[:output] ? File.open(opts[:output], "w") : $stdout
    @csv_options = {}
    @csv_options[:col_sep] = opts[:col_sep] if opts[:col_sep]
    if opts[:col_sep]
      @csv_options[:quote_char] = opts[:quote_char] || "\0"
    end
  end

  def execute(opts)
    configure(opts)

    @output_count = 0
    with_input do |input|
      while line = get
        row = CSV.parse_line(line, csv_options)
        output = crunch(row)
        emit(output) if output
      end
    end
    crunch(nil) {|row| emit(row)}
    @output.close if opts[:output]

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
masticate-0.2.3 lib/masticate/base.rb
masticate-0.2.2 lib/masticate/base.rb