Class: FlexCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/flex_csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, args = {}) ⇒ FlexCSV

Returns a new instance of FlexCSV.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flex_csv.rb', line 6

def initialize (input, args = {})
  rows = []
  case input.first
    when Hash
      rows << input.first.keys
      input.each { |hash| rows << hash.values }
    when String
      rows << eval(input.first).keys
      input.each { |string| rows << eval(string).values }
    else
      raise ArgumentError
  end
  path = args[:path] || __dir__ + '/temp.csv'
  open = args[:path].nil? ? false : args[:open]

  CSV.open(path, 'w') do |csv|
    rows.each { |row| csv << row}
  end
  `open #{path}` if open

  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/flex_csv.rb', line 4

def path
  @path
end