Sha256: 3ab3778d704851b9cd8db38e397b88588a4857d69091a2f8ad7e13bd22925c9c

Contents?: true

Size: 984 Bytes

Versions: 4

Compression:

Stored size: 984 Bytes

Contents

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

  def initialize(filename)
    @filename = filename
  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 += 1
    begin
      @output.puts line
    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
masticate-0.1.5 lib/masticate/base.rb
masticate-0.1.4 lib/masticate/base.rb
masticate-0.1.3 lib/masticate/base.rb
masticate-0.1.1 lib/masticate/base.rb