windows_csv 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.8
@@ -1,13 +1,23 @@
1
1
  #This class was heavily inspired by the great Dipth! https://github.com/dipth
2
2
  class WindowsCsv
3
- ENCODING = Encoding::UTF_8
4
-
5
3
  BOM = "\377\376".force_encoding(Encoding::UTF_16LE) # Byte Order Mark
6
- COL_SEP = "\t"
4
+ COL_SEP = ";"
7
5
  QUOTE_CHAR = "\""
8
6
  ROW_SEP = "\r\n"
9
- ARGS = {:col_sep => COL_SEP, :quote_char => QUOTE_CHAR, :row_sep => ROW_SEP}
10
7
 
8
+ ARGS = {
9
+ :col_sep => COL_SEP,
10
+ :quote_char => QUOTE_CHAR,
11
+ :row_sep => ROW_SEP
12
+ }
13
+
14
+ REPLACES = {
15
+ "\r\n" => "\\r\\n",
16
+ "\r" => "\\r",
17
+ "\n" => "\\n"
18
+ }
19
+
20
+ #Loops through a Windows CSV file with leading BOM, tabs as col-sep, quote char " and row sep \r\n
11
21
  def self.foreach(path, args = {})
12
22
  require "csv"
13
23
 
@@ -43,7 +53,7 @@ class WindowsCsv
43
53
  @args = args
44
54
 
45
55
  if @args[:path]
46
- fp = File.open(@args[:path], "w", :encoding => ENCODING)
56
+ fp = File.open(@args[:path], "w", :encoding => Encoding::UTF_8)
47
57
  @args[:io] = fp
48
58
  end
49
59
 
@@ -68,16 +78,28 @@ class WindowsCsv
68
78
  end
69
79
  end
70
80
 
71
- @args[:io].puts CSV.generate_line(encoded, ARGS).encode(Encoding::UTF_16LE)
81
+ @args[:io].write CSV.generate_line(encoded, ARGS).encode(Encoding::UTF_16LE)
72
82
 
73
83
  return nil
74
84
  end
75
85
 
76
86
  def self.escape(str)
77
- return str.to_s.gsub("\n", "\\r\\n")
87
+ str = str.to_s
88
+
89
+ REPLACES.each do |key, val|
90
+ str = str.gsub(key, val)
91
+ end
92
+
93
+ return str
78
94
  end
79
95
 
80
96
  def self.unescape(str)
81
- return str.to_s.gsub("\\r\\n", "\r\n")
97
+ str = str.to_s
98
+
99
+ REPLACES.each do |key, val|
100
+ str = str.gsub(val, key)
101
+ end
102
+
103
+ return str
82
104
  end
83
105
  end
@@ -26,7 +26,7 @@ describe "WindowsCsv" do
26
26
  if count == 1
27
27
  row[:Name2].should eql("Christina")
28
28
  row[:Encoding].should eql("æøå")
29
- row[:MultiLine].should eql("Multi\r\nLine")
29
+ row[:MultiLine].should eql("Multi\nLine")
30
30
  end
31
31
  end
32
32
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "windows_csv"
8
- s.version = "0.0.6"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["kaspernj"]
12
- s.date = "2013-05-02"
12
+ s.date = "2013-05-03"
13
13
  s.description = "A framework to write Windows CSV files."
14
14
  s.email = "k@spernj.org"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: windows_csv
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.6
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - kaspernj
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -109,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
109
  - !ruby/object:Gem::Version
110
110
  segments:
111
111
  - 0
112
- hash: -649786811259003594
112
+ hash: 3869959267862217963
113
113
  version: '0'
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false