Module: SpreadsheetImportExportHelpers
- Defined in:
- lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb
Overview
SpreadsheetImportExportHelpers
- Author
-
Joel Parker Henderson, [email protected]
- Copyright
-
Copyright © 2006-2009 Joel Parker Henderson
- License
-
CreativeCommons License, Non-commercial Share Alike
- License
-
LGPL, GNU Lesser General Public License
Instance Method Summary collapse
-
#export_date_or_note(date, note = nil) ⇒ Object
Corresponds to import_date_or_note.
-
#export_flag_or_note(flag, note = nil) ⇒ Object
Corresponds to import_flag_or_note.
-
#import_date(s) ⇒ Object
Parse a string to a date, with some cleanup.
-
#import_date_or_note(s) ⇒ Object
There’s a common import use case where a column can be of two types: - a date, in a variety of format like “Jan 1, 2008”, “1/1/2008”, etc.
-
#import_flag(s) ⇒ Object
There’s a typical import use case where a column is a boolean, in a variety of formats like “X” for on, “Y” for yes, “T” for true, etc.
-
#import_flag_or_note(s) ⇒ Object
There’s a typical import use case where a column can contain two different data types: - a boolean, in a variety of formats (see import_flag) - a note, in plain text.
Instance Method Details
#export_date_or_note(date, note = nil) ⇒ Object
Corresponds to import_date_or_note
Return
-
date exists => date to yyyy-mm-dd format
-
otherwise return note
Example
d=Date.parse('1/1/2007')
export_date_or_note(d,'hello') => '2007-01-01'
export_date_or_note(nil,'hello') => 'hello'
127 128 129 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 127 def export_date_or_note(date,note=nil) return date ? date.strftime("%Y-%m-%d") : note end |
#export_flag_or_note(flag, note = nil) ⇒ Object
Corresponds to import_flag_or_note
Return
-
flag==true => ‘X’
-
flag==false => ”
-
otherwise return note
Example
t=true
f=false
export_flag_or_note(t,'hello') => 'X'
export_flag_or_note(f,'hello') => ''
export_flag_or_note(nil,'hello') => 'hello'
111 112 113 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 111 def export_flag_or_note(flag,note=nil) return flag==true ? 'X' : flag==false ? '' : note end |
#import_date(s) ⇒ Object
Parse a string to a date, with some cleanup
14 15 16 17 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 14 def import_date(s) return nil if s==nil or s.strip=='' return Date.parse(s) end |
#import_date_or_note(s) ⇒ Object
There’s a common import use case where a column can be of two types:
-
a date, in a variety of format like “Jan 1, 2008”, “1/1/2008”, etc.
-
a note, in plain text
This method returns two items: a date (Date class) and a note (String class).
Example
import_date_or_note('1/1/2007') => [Date(2007,01,01),'']
import_date_or_note('hello') => [nil,'hello']
87 88 89 90 91 92 93 94 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 87 def import_date_or_note(s) begin d=import_date(s) rescue d=nil end return d ? [d,''] : [nil,s] end |
#import_flag(s) ⇒ Object
There’s a typical import use case where a column is a boolean, in a variety of formats like “X” for on, “Y” for yes, “T” for true, etc.
We use these rules:
-
x y yes t true on + => true
-
blank n no f false off - => false
-
anything else => nil
Examples
import_flag('Yes') => true
import_flag('No') => false
import_flag('X') => true
import_flag('') => false
import_flag('Hello') => nil
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 38 def import_flag(s) case s.strip.downcase when 'x','y','yes','t','true','on','+' return true,'' when '','n','no','f','false','off','-' return false else return nil end end |
#import_flag_or_note(s) ⇒ Object
There’s a typical import use case where a column can contain two different data types:
-
a boolean, in a variety of formats (see import_flag)
-
a note, in plain text
We need to separate these two.
This method returns two items: a boolean and a note.
We use these rules:
-
x y yes t true on + => [true, ”]
-
blank n no f false off - => [false, ”]
-
anything else => nil, note
Example
import_flag_or_note('Yes') => [true,'']
import_flag_or_note('No') => [false,'']
import_flag_or_note('X') => [true,'']
import_flag_or_note('') => [false,'']
import_flag_or_note('Hello') => [nil,'Hello']
71 72 73 74 |
# File 'lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb', line 71 def import_flag_or_note(s) flag = import_flag(s) return flag ? [flag, ''] : [nil, s] end |