Class: FlexibleCsv
- Inherits:
-
Object
- Object
- FlexibleCsv
- Defined in:
- lib/flexible_csv.rb
Defined Under Namespace
Classes: Parser
Instance Attribute Summary collapse
-
#column_hash ⇒ Object
readonly
a Hash where the key is the method name, and the value is the array of possible headers.
Instance Method Summary collapse
-
#clean_up(str) ⇒ Object
Developed by Chris Powers, Killswitch Collective on 02/17/2009.
-
#column(id, *args) ⇒ Object
Developed by Chris Powers, Killswitch Collective on 02/17/2009.
-
#initialize {|_self| ... } ⇒ FlexibleCsv
constructor
A new instance of FlexibleCsv.
-
#parse(data) ⇒ Object
Developed by Chris Powers, Killswitch Collective on 02/17/2009.
Constructor Details
#initialize {|_self| ... } ⇒ FlexibleCsv
Returns a new instance of FlexibleCsv.
8 9 10 11 |
# File 'lib/flexible_csv.rb', line 8 def initialize @column_hash = {} yield self end |
Instance Attribute Details
#column_hash ⇒ Object (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 |