Module: CsvRecord

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

Defined Under Namespace

Modules: Version Classes: Base

Constant Summary collapse

VERSION =
[Version::MAJOR,
Version::MINOR,
Version::PATCH].join('.')

Class Method Summary collapse

Class Method Details



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

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



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

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



117
118
119
# File 'lib/csvrecord/base.rb', line 117

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

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



106
107
108
109
110
111
# File 'lib/csvrecord/base.rb', line 106

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

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

.read(path, sep: nil) ⇒ Object



99
100
101
102
103
104
# File 'lib/csvrecord/base.rb', line 99

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

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

.rootObject



24
25
26
# File 'lib/csvrecord/version.rb', line 24

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

.versionObject



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

def self.version
  VERSION
end