Class: CsvGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_generator.rb,
lib/csv_generator/version.rb

Defined Under Namespace

Classes: RowGenerator

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, options = {}) ⇒ CsvGenerator

Returns a new instance of CsvGenerator.



20
21
22
23
# File 'lib/csv_generator.rb', line 20

def initialize(io, options = {})
  @io = io
  @options = options
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



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

def io
  @io
end

Class Method Details

.open(path, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/csv_generator.rb', line 7

def open(path, options = {})
  mode = options[:mode] || 'w'
  permission = options[:permission] || 0644

  File.open(path, mode, permission) do |io|
    generator = new(io, options)
    yield generator

    generator
  end
end

Instance Method Details

#<<(row_values) ⇒ Object



35
36
37
# File 'lib/csv_generator.rb', line 35

def <<(row_values)
  io.write row_generator.generate(row_values)
end

#generate(enumerable) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/csv_generator.rb', line 25

def generate(enumerable)
  enumerable.each do |row_instance|
    if block_given?
      self << (yield row_instance)
    else
      self << row_instance
    end
  end
end