windows_csv 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -2
- data/Gemfile.lock +0 -8
- data/VERSION +1 -1
- data/lib/windows_csv.rb +22 -8
- data/spec/windows_csv_spec.rb +13 -5
- data/windows_csv.gemspec +1 -4
- metadata +2 -18
data/Gemfile
CHANGED
@@ -3,8 +3,6 @@ source "http://rubygems.org"
|
|
3
3
|
# Example:
|
4
4
|
# gem "activesupport", ">= 2.3.5"
|
5
5
|
|
6
|
-
gem "csv_lazy", :path => "/Users/kaspernj/Dev/Ruby/csv_lazy"
|
7
|
-
|
8
6
|
# Add dependencies to develop your gem here.
|
9
7
|
# Include everything needed to run rake, tests, features, etc.
|
10
8
|
group :development do
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
PATH
|
2
|
-
remote: /Users/kaspernj/Dev/Ruby/csv_lazy
|
3
|
-
specs:
|
4
|
-
csv_lazy (0.0.8)
|
5
|
-
string-strtr
|
6
|
-
|
7
1
|
GEM
|
8
2
|
remote: http://rubygems.org/
|
9
3
|
specs:
|
@@ -26,14 +20,12 @@ GEM
|
|
26
20
|
rspec-expectations (2.8.0)
|
27
21
|
diff-lcs (~> 1.1.2)
|
28
22
|
rspec-mocks (2.8.0)
|
29
|
-
string-strtr (0.0.3)
|
30
23
|
|
31
24
|
PLATFORMS
|
32
25
|
ruby
|
33
26
|
|
34
27
|
DEPENDENCIES
|
35
28
|
bundler (>= 1.0.0)
|
36
|
-
csv_lazy!
|
37
29
|
jeweler (~> 1.8.4)
|
38
30
|
rdoc (~> 3.12)
|
39
31
|
rspec (~> 2.8.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
data/lib/windows_csv.rb
CHANGED
@@ -5,22 +5,32 @@ class WindowsCsv
|
|
5
5
|
BOM = "\377\376".force_encoding(Encoding::UTF_16LE) # Byte Order Mark
|
6
6
|
COL_SEP = "\t"
|
7
7
|
QUOTE_CHAR = "\""
|
8
|
+
ROW_SEP = "\r\n"
|
9
|
+
ARGS = {:col_sep => COL_SEP, :quote_char => QUOTE_CHAR, :row_sep => ROW_SEP}
|
8
10
|
|
9
11
|
def self.foreach(path, args = {})
|
10
|
-
require "
|
11
|
-
require "csv_lazy"
|
12
|
+
require "csv"
|
12
13
|
|
13
14
|
File.open(path, "rb:bom|utf-16le") do |fp|
|
14
|
-
csv_args =
|
15
|
+
csv_args = ARGS.clone
|
15
16
|
csv_args.merge!(args[:csv_args]) if args[:csv_args]
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
CSV.foreach(fp, csv_args) do |row|
|
19
|
+
if csv_args[:headers]
|
20
|
+
real = {}
|
21
|
+
else
|
22
|
+
real = []
|
23
|
+
end
|
24
|
+
|
19
25
|
row.each do |col|
|
20
|
-
|
26
|
+
if csv_args[:headers]
|
27
|
+
real[col[0].to_sym] = WindowsCsv.unescape(col[1])
|
28
|
+
else
|
29
|
+
real << WindowsCsv.unescape(col)
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
|
-
yield
|
33
|
+
yield real
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
@@ -58,7 +68,7 @@ class WindowsCsv
|
|
58
68
|
end
|
59
69
|
end
|
60
70
|
|
61
|
-
@args[:io].puts CSV.generate_line(encoded,
|
71
|
+
@args[:io].puts CSV.generate_line(encoded, ARGS).encode(Encoding::UTF_16LE)
|
62
72
|
|
63
73
|
return nil
|
64
74
|
end
|
@@ -66,4 +76,8 @@ class WindowsCsv
|
|
66
76
|
def self.escape(str)
|
67
77
|
return str.to_s.gsub("\n", "\\r\\n")
|
68
78
|
end
|
79
|
+
|
80
|
+
def self.unescape(str)
|
81
|
+
return str.to_s.gsub("\\r\\n", "\r\n")
|
82
|
+
end
|
69
83
|
end
|
data/spec/windows_csv_spec.rb
CHANGED
@@ -13,15 +13,23 @@ describe "WindowsCsv" do
|
|
13
13
|
WindowsCsv.new(:path => tmpfile) do |csv|
|
14
14
|
csv << ["Name1", "Name2", "Encoding", "Date", "Time", "DateTime", "MultiLine"]
|
15
15
|
csv << ["Kasper", "Christina", "æøå", Date.new, Time.new, DateTime.new, "Multi\nLine"]
|
16
|
+
csv << ["Thomas", "Nikolaj", "læp", nil, nil, nil, nil]
|
16
17
|
end
|
17
18
|
|
18
|
-
|
19
|
+
puts "Path: #{tmpfile}"
|
19
20
|
|
21
|
+
count = 0
|
20
22
|
WindowsCsv.foreach(tmpfile, :csv_args => {:headers => true}) do |row|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
count += 1
|
24
|
+
#puts "Row: #{row}"
|
25
|
+
|
26
|
+
if count == 1
|
27
|
+
row[:Name2].should eql("Christina")
|
28
|
+
row[:Encoding].should eql("æøå")
|
29
|
+
row[:MultiLine].should eql("Multi\r\nLine")
|
30
|
+
end
|
25
31
|
end
|
32
|
+
|
33
|
+
count.should eql(2)
|
26
34
|
end
|
27
35
|
end
|
data/windows_csv.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "windows_csv"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["kaspernj"]
|
@@ -40,20 +40,17 @@ Gem::Specification.new do |s|
|
|
40
40
|
s.specification_version = 3
|
41
41
|
|
42
42
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
-
s.add_runtime_dependency(%q<csv_lazy>, [">= 0"])
|
44
43
|
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
45
44
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
46
45
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
47
46
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
48
47
|
else
|
49
|
-
s.add_dependency(%q<csv_lazy>, [">= 0"])
|
50
48
|
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
51
49
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
52
50
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
53
51
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
54
52
|
end
|
55
53
|
else
|
56
|
-
s.add_dependency(%q<csv_lazy>, [">= 0"])
|
57
54
|
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
58
55
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
59
56
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: windows_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- kaspernj
|
@@ -11,22 +11,6 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2013-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
none: false
|
17
|
-
requirements:
|
18
|
-
- - ! '>='
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '0'
|
21
|
-
name: csv_lazy
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
requirement: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
version_requirements: !ruby/object:Gem::Requirement
|
32
16
|
none: false
|
@@ -125,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
109
|
- !ruby/object:Gem::Version
|
126
110
|
segments:
|
127
111
|
- 0
|
128
|
-
hash:
|
112
|
+
hash: -649786811259003594
|
129
113
|
version: '0'
|
130
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
115
|
none: false
|