Class: Daru::IO::Exporters::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/exporters/base.rb

Overview

Base Exporter Class that contains generic helper methods, to be used by other Exporters via inheritence

Direct Known Subclasses

Excel, JSON

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(dataframe) ⇒ Base

Checks whether the first argument given to any Daru::IO::<Exporter> module is a Daru::DataFrame. Raises an error when it's not a Daru::DataFrame.

Examples:

Stores the dataframe

df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])
Daru::IO::Exporters::Base.new(df)

#=> #<Daru::IO::Exporters::Base:0x007f899081af08 @dataframe=#<Daru::DataFrame(2x2)>
#      a   b
#  0   1   3
#  1   2   4>

Raises error when not a DataFrame

Daru::IO::Exporters::Base.new(nil)

#=> ArgumentError: Expected first argument to be a Daru::DataFrame, received NilClass instead

Parameters:



27
28
29
30
31
32
33
34
# File 'lib/daru/io/exporters/base.rb', line 27

def initialize(dataframe)
  unless dataframe.is_a?(Daru::DataFrame)
    raise ArgumentError,
      'Expected first argument to be a Daru::DataFrame, '\
      "received #{dataframe.class} instead."
  end
  @dataframe = dataframe
end

Instance Method Details

#to_sObject

Exports an Exporter instance to a file-writable String.

Examples:

Getting a file-writable string from Avro Exporter instance


instance = Daru::IO::Exporters::Format.new(opts)
instance.to_s #! same as df.to_format_string(opts)

Returns:

  • A file-writable String



44
45
46
47
48
49
50
# File 'lib/daru/io/exporters/base.rb', line 44

def to_s
  tempfile = Tempfile.new('tempfile')
  path     = tempfile.path
  write(path)

  File.read(path)
end