Method: Avro::DataFile.open

Defined in:
lib/avro/data_file.rb

.open(file_path, mode = 'r', schema = nil, codec = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/avro/data_file.rb', line 33

def self.open(file_path, mode='r', schema=nil, codec=nil)
  schema = Avro::Schema.parse(schema) if schema
  case mode
  when 'w'
    unless schema
      raise DataFileError, "Writing an Avro file requires a schema."
    end
    io = open_writer(File.open(file_path, 'wb'), schema, codec)
  when 'r'
    io = open_reader(File.open(file_path, 'rb'), schema)
  else
    raise DataFileError, "Only modes 'r' and 'w' allowed. You gave #{mode.inspect}."
  end

  yield io if block_given?
  io
ensure
  io.close if block_given? && io
end