Module: CsvRecord

Defined in:
lib/csvrecord/base.rb,
lib/csvrecord/version.rb

Defined Under Namespace

Classes: Base

Constant Summary collapse

MAJOR =

todo: namespace inside version or something - why? why not??

0
MINOR =
4
PATCH =
2
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Class Method Summary collapse

Class Method Details



16
17
18
# File 'lib/csvrecord/version.rb', line 16

def self.banner
  "csvrecord/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.build_class(headers) ⇒ Object

“magic” lazy auto-build schema from headers versions



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/csvrecord/base.rb', line 82

def self.build_class( headers )   ## check: find a better name - why? why not?

  ## (auto-)build record class from an array of headers

  ##   add fields (all types will be string for now)

  clazz = Class.new( Base )
  headers.each do |header|
    ## downcase and remove all non-ascii chars etc.

    ##  todo/fix: remove all non-ascii chars!!!

    ##  todo: check if header starts with a number too!!

    name = header.downcase.gsub( ' ', '_' )
    name = name.to_sym   ## symbol-ify

    clazz.field( name )
  end
  clazz
end

.define(&block) ⇒ Object

alternative class (record) builder



115
116
117
# File 'lib/csvrecord/base.rb', line 115

def self.define( &block )   ## check: rename super_class to base - why? why not?

  Record.define( Base, &block )
end

.foreach(path, sep: Csv.config.sep, &block) ⇒ Object



104
105
106
107
108
109
# File 'lib/csvrecord/base.rb', line 104

def self.foreach( path, sep: Csv.config.sep, &block )
  headers = CsvReader.header( path, sep: sep )

  clazz = build_class( headers )
  clazz.foreach( path, sep: sep, &block )
end

.read(path, sep: Csv.config.sep) ⇒ Object



97
98
99
100
101
102
# File 'lib/csvrecord/base.rb', line 97

def self.read( path, sep: Csv.config.sep )
  headers = CsvReader.header( path, sep: sep )

  clazz = build_class( headers )
  clazz.read( path, sep: sep )
end

.rootObject



20
21
22
# File 'lib/csvrecord/version.rb', line 20

def self.root
  File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
end

.versionObject



12
13
14
# File 'lib/csvrecord/version.rb', line 12

def self.version
  VERSION
end