Class: FlexibleCsv

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

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ FlexibleCsv

Returns a new instance of FlexibleCsv.

Yields:

  • (_self)

Yield Parameters:

  • _self (FlexibleCsv)

    the object that the method was called on



8
9
10
11
# File 'lib/flexible_csv.rb', line 8

def initialize
  @column_hash = {}
  yield self
end

Instance Attribute Details

#column_hashObject (readonly)

a Hash where the key is the method name, and the value is the array of possible headers



6
7
8
# File 'lib/flexible_csv.rb', line 6

def column_hash
  @column_hash
end

Instance Method Details

#clean_up(str) ⇒ Object

Developed by Chris Powers, Killswitch Collective on 02/17/2009

Description: Helper method to remove whitespace, capitalization and punctuation

Arguments: a string

Returns: a formatted string

Syntax: clean_up “My Column!” #=> ‘mycolumn’



49
50
51
# File 'lib/flexible_csv.rb', line 49

def clean_up(str)
  str.to_s.downcase.gsub(/[^a-z0-9]/, '')
end

#column(id, *args) ⇒ Object

Developed by Chris Powers, Killswitch Collective on 02/17/2009

Description:

Arguments: The hash key / method name and any number of possible header labels as strings

Returns: nil

Syntax: csv.column :name, “Full Name”, “Name”, “Client Name”



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

def column(id, *args)
  @column_hash[id] = args.map {|item| clean_up(item) }
  nil
end

#parse(data) ⇒ Object

Developed by Chris Powers, Killswitch Collective on 02/17/2009

Description: Parses a CSV data source, either a String or an IO object

Arguments: CSV formatted String or IO of CSV file

Returns: Parser object

Syntax: @parser = @flexible_csv.parse(File.open(‘path/to/file.csv’))



22
23
24
# File 'lib/flexible_csv.rb', line 22

def parse(data)
  Parser.new(data, self)
end