Class: Daru::IO::Importers::Base

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

Overview

Base Importer Class that contains generic helper methods, to be used by other Importers via inheritence

Direct Known Subclasses

ActiveRecord, Excel, Excelx, HTML, JSON, Plaintext, Redis

Class Method Summary collapse

Methods inherited from Base

#optional_gem

Class Method Details

.from(relation) ⇒ Object

Adds the from class method to all inheriting children Importer classes, which calls corresponding Importer's initialize and instance method from.



50
51
52
# File 'lib/daru/io/importers/base.rb', line 50

def self.from(relation)
  new.from(relation)
end

.guess_parse(keys, vals) ⇒ Object

Guesses the Daru::DataFrame from the parsed set of key-value pairs.

Examples:

When key-value pairs contains values that is Array of Hashes

Daru::IO::Importers::Base.guess_parse([:a], [[{ x: 1, y: 2 },{ x: 3, y: 4 }]])

#=> #<Daru::DataFrame(2x2)>
#      x   y
#  0   1   2
#  1   3   4

When key-value pairs contains values that is Arrays

Daru::IO::Importers::Base.guess_parse([:x, :y], [[1,3], [2,4]])

#=> #<Daru::DataFrame(2x2)>
#      x   y
#  0   1   2
#  1   3   4

When key-value pairs contains Array of keys contain value Hashes

Daru::IO::Importers::Base.guess_parse([:a, :b], [{ x: 1, y: 2 }, { x: 3, y: 4 }])

#=> #<Daru::DataFrame(2x2)>
#      x   y
#  a   1   2
#  b   3   4

Parameters:

  • keys (Array)

    A set of keys from given key-value pairs

  • vals (Array)

    A set of values from given key-value pairs



37
38
39
40
41
42
43
44
45
46
# File 'lib/daru/io/importers/base.rb', line 37

def self.guess_parse(keys, vals)
  case vals.first
  when Array
    case vals.first.first
    when Hash then Daru::DataFrame.new(vals.flatten)
    else Daru::DataFrame.rows(vals.transpose, order: keys)
    end
  when Hash then Daru::DataFrame.new(vals.flatten, index: keys)
  end
end

.read(path) ⇒ Object

Adds the read class method to all inheriting children Importer classes, which calls corresponding Importer's initialize and instance method read.



56
57
58
# File 'lib/daru/io/importers/base.rb', line 56

def self.read(path)
  new.read(path)
end