webget_spreadsheet_import_export_helpers 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,132 @@
1
+ # = SpreadsheetImportExportHelpers
2
+ #
3
+ # Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
4
+ # Copyright:: Copyright (c) 2006-2009 Joel Parker Henderson
5
+ # License:: CreativeCommons License, Non-commercial Share Alike
6
+ # License:: LGPL, GNU Lesser General Public License
7
+ #
8
+ #
9
+ ##
10
+
11
+ module SpreadsheetImportExportHelpers
12
+
13
+ # Parse a string to a date, with some cleanup
14
+ def import_date(s)
15
+ return nil if s==nil or s.strip==''
16
+ return Date.parse(s)
17
+ end
18
+
19
+
20
+ # There's a typical import use case where a column is
21
+ # a boolean, in a variety of formats like "X" for on,
22
+ # "Y" for yes, "T" for true, etc.
23
+ #
24
+ # @return a boolean
25
+ #
26
+ # We use these rules:
27
+ # - x y yes t true on + => true
28
+ # - blank n no f false off - => false
29
+ # - anything else => nil
30
+ #
31
+ # ==Examples
32
+ # import_flag('Yes') => true
33
+ # import_flag('No') => false
34
+ # import_flag('X') => true
35
+ # import_flag('') => false
36
+ # import_flag('Hello') => nil
37
+
38
+ def import_flag(s)
39
+ case s.strip.downcase
40
+ when 'x','y','yes','t','true','on','+'
41
+ return true,''
42
+ when '','n','no','f','false','off','-'
43
+ return false
44
+ else
45
+ return nil
46
+ end
47
+ end
48
+
49
+
50
+ # There's a typical import use case where a column
51
+ # can contain two different data types:
52
+ # - a boolean, in a variety of formats (see _import_flag_)
53
+ # - a note, in plain text
54
+ #
55
+ # We need to separate these two.
56
+ #
57
+ # This method returns two items: a boolean and a note.
58
+ #
59
+ # We use these rules:
60
+ # - x y yes t true on + => [true, '']
61
+ # - blank n no f false off - => [false, '']
62
+ # - anything else => nil, note
63
+ #
64
+ # ==Example
65
+ # import_flag_or_note('Yes') => [true,'']
66
+ # import_flag_or_note('No') => [false,'']
67
+ # import_flag_or_note('X') => [true,'']
68
+ # import_flag_or_note('') => [false,'']
69
+ # import_flag_or_note('Hello') => [nil,'Hello']
70
+
71
+ def import_flag_or_note(s)
72
+ flag = import_flag(s)
73
+ return flag ? [flag, ''] : [nil, s]
74
+ end
75
+
76
+
77
+ # There's a common import use case where a column can be of two types:
78
+ # - a date, in a variety of format like "Jan 1, 2008", "1/1/2008", etc.
79
+ # - a note, in plain text
80
+ #
81
+ # This method returns two items: a date (Date class) and a note (String class).
82
+ #
83
+ # ==Example
84
+ # import_date_or_note('1/1/2007') => [Date(2007,01,01),'']
85
+ # import_date_or_note('hello') => [nil,'hello']
86
+
87
+ def import_date_or_note(s)
88
+ begin
89
+ d=import_date(s)
90
+ rescue
91
+ d=nil
92
+ end
93
+ return d ? [d,''] : [nil,s]
94
+ end
95
+
96
+
97
+ # Corresponds to import_flag_or_note
98
+ #
99
+ # ==Return
100
+ # - flag==true => 'X'
101
+ # - flag==false => ''
102
+ # - otherwise return note
103
+ #
104
+ # ==Example
105
+ # t=true
106
+ # f=false
107
+ # export_flag_or_note(t,'hello') => 'X'
108
+ # export_flag_or_note(f,'hello') => ''
109
+ # export_flag_or_note(nil,'hello') => 'hello'
110
+
111
+ def export_flag_or_note(flag,note=nil)
112
+ return flag==true ? 'X' : flag==false ? '' : note
113
+ end
114
+
115
+
116
+ # Corresponds to import_date_or_note
117
+ #
118
+ # ==Return
119
+ # - date exists => date to yyyy-mm-dd format
120
+ # - otherwise return note
121
+ #
122
+ # ==Example
123
+ # d=Date.parse('1/1/2007')
124
+ # export_date_or_note(d,'hello') => '2007-01-01'
125
+ # export_date_or_note(nil,'hello') => 'hello'
126
+
127
+ def export_date_or_note(date,note=nil)
128
+ return date ? date.strftime("%Y-%m-%d") : note
129
+ end
130
+
131
+
132
+ end
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+ require 'webget_spreadsheet_import_export_helpers'
4
+
5
+ class SpreadsheetImportExportHelpersTest < Test::Unit::TestCase
6
+
7
+ include SpreadsheetImportExportHelpers
8
+
9
+ def test_import_flag
10
+ assert_equal(true, import_flag('X'))
11
+ assert_equal(false, import_flag(''))
12
+ assert_equal(true, import_flag('yes'))
13
+ assert_equal(false, import_flag('no'))
14
+ assert_equal(true, import_flag('+'))
15
+ assert_equal(false, import_flag('-'))
16
+ assert_equal(nil, import_flag('hello'))
17
+ end
18
+
19
+ def test_import_flag_or_note
20
+ assert_equal([true,''], import_flag_or_note('X'))
21
+ assert_equal([false,''], import_flag_or_note(''))
22
+ assert_equal([true,''], import_flag_or_note('yes'))
23
+ assert_equal([false,''], import_flag_or_note('no'))
24
+ assert_equal([true,''], import_flag_or_note('+'))
25
+ assert_equal([false,''], import_flag_or_note('-'))
26
+ assert_equal([nil,'hello'], import_flag_or_note('hello'))
27
+ end
28
+
29
+ def test_import_date_or_note
30
+ d=Date.parse('2007/12/31')
31
+ assert_equal([d,''], import_date_or_note('2007/12/31'))
32
+ assert_equal([nil,'hello'], import_date_or_note('hello'))
33
+ end
34
+
35
+ def test_export_flag_or_note
36
+ assert_equal('X', export_flag_or_note(true,'hello'))
37
+ assert_equal('', export_flag_or_note(false,'hello'))
38
+ assert_equal('hello', export_flag_or_note(nil,'hello'))
39
+ end
40
+
41
+ def test_export_date_or_note
42
+ d=Date.parse('2007/12/31')
43
+ assert_equal('2007-12-31', export_date_or_note(d,'hello'))
44
+ assert_equal('hello', export_date_or_note(nil,'hello'))
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webget_spreadsheet_import_export_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - WebGet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDvDCCAyWgAwIBAgIJAIlSqEkDQaZIMA0GCSqGSIb3DQEBBQUAMIGbMQswCQYD
14
+ VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQG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0G
30
+ CSqGSIb3DQEBBQUAA4GBADzVXlwuff0/w3yK4LflGKKhtC3oChIrwmSyP6tk628N
31
+ BHokpc4Kz63xSXqzYTnBS7rFBwlYThtNalQeWmoUjGh3Z0ZR0JlhU0ln8899LuJ3
32
+ DXnLFY0cVuBnNDMOOFl8vk1qIcZjcTovhzgcixpG6Uk5qmUsKHRLQf4oQJx7TfLK
33
+ -----END CERTIFICATE-----
34
+
35
+ date: 2010-01-28 00:00:00 -08:00
36
+ default_executable:
37
+ dependencies: []
38
+
39
+ description:
40
+ email: webget@webget.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - lib/webget_spreadsheet_import_export_helpers/webget_spreadsheet_import_export_helpers.rb
49
+ has_rdoc: true
50
+ homepage: http://webget.com/
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: "WebGet spreadsheet_import_export_helpers gem: parsers and prettifiers for spreadsheets"
77
+ test_files:
78
+ - test/webget_spreadsheet_import_export_helpers/unit/webget_spreadsheet_import_export_helpers_test.rb
@@ -0,0 +1,2 @@
1
+ N�d� F�²sq����Kk�^D禜
2
+ -�h{�`OEL�-��