Module: SimpleCsv

Defined in:
lib/simple_csv.rb,
lib/simple_csv/base.rb,
lib/simple_csv/reader.rb,
lib/simple_csv/writer.rb,
lib/simple_csv/version.rb,
lib/simple_csv/settings.rb,
lib/simple_csv/transformer.rb

Defined Under Namespace

Classes: Base, HeadersNotSet, NotEnoughHeaders, Reader, RowNotComplete, Settings, Transformer, UnparseableCsv, Writer

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Class Method Summary collapse

Class Method Details

.converters_initialized?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/simple_csv.rb', line 54

def self.converters_initialized?
  @converters_initialized
end

.csv_manually_set_headers!Object

Raises:



27
28
29
30
31
# File 'lib/simple_csv.rb', line 27

def self.csv_manually_set_headers!
  raise HeadersNotSet,
        ['CSV does not contain headers',
         'please add headers in them manually or in the file'].join(', ')
end

.csv_not_enough_headers!Object

Raises:



23
24
25
# File 'lib/simple_csv.rb', line 23

def self.csv_not_enough_headers!
  raise NotEnoughHeaders, 'Not enough headers defined!'
end

.generate(path, **options, &block) ⇒ Object



38
39
40
41
# File 'lib/simple_csv.rb', line 38

def self.generate(path, **options, &block)
  initialize_converters unless converters_initialized?
  Writer.new path, options, &block
end

.initialize_convertersObject



48
49
50
51
52
# File 'lib/simple_csv.rb', line 48

def self.initialize_converters
  CSV::Converters[:blank_to_nil] = ->(f) { f && f.empty? ? nil : f }
  CSV::Converters[:null_to_nil] = ->(f) { f && f == 'NULL' ? nil : f }
  @converters_initialized = true
end

.read(path, **options, &block) ⇒ Object



33
34
35
36
# File 'lib/simple_csv.rb', line 33

def self.read(path, **options, &block)
  initialize_converters unless converters_initialized?
  Reader.new path, options, &block
end

.row_not_complete!(mtd, value) ⇒ Object

Raises:



18
19
20
21
# File 'lib/simple_csv.rb', line 18

def self.row_not_complete!(mtd, value)
  raise RowNotComplete,
        "Row not complete! #{mtd} called twice with value #{value}"
end

.transform(path, **options, &block) ⇒ Object



43
44
45
46
# File 'lib/simple_csv.rb', line 43

def self.transform(path, **options, &block)
  initialize_converters unless converters_initialized?
  Transformer.new path, options, &block
end