typedcsv 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG +13 -0
- data/README.md +6 -4
- data/lib/typedcsv.rb +13 -0
- data/lib/typedcsv/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 315765b481130f95049fe7549a29fc3e7722fd20
|
4
|
+
data.tar.gz: 070a5e9194663ba62113a2a7a87e6976fd2f86df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce70f197882f1103c71c108f9e97722712ced83c1f82365cd9a4b154f6a8bb032268ff822ec3ada4ec1c39f076b6b46b6388a45721bfded88bdd6dbda8468ea2
|
7
|
+
data.tar.gz: '0280d2e3c7137c80dce8cf30e2c0b680e9f5bd1330575254e35ca942cdace950dc5dca7d0c1a3c46582131a1b179a00736cff9908df3bf934f8a7bbf2f2aa3ce'
|
data/CHANGELOG
ADDED
data/README.md
CHANGED
@@ -3,15 +3,15 @@
|
|
3
3
|
Here's your standard untyped CSV:
|
4
4
|
|
5
5
|
```
|
6
|
-
name,income,created_at,tags
|
7
|
-
Seamus,12301.2,2012-02-21,red;blue
|
6
|
+
name,income,created_at,tags,great
|
7
|
+
Seamus,12301.2,2012-02-21,red;blue,true
|
8
8
|
```
|
9
9
|
|
10
10
|
Now, you and I know that `12301.2` is a number and `2012-02-21` is a date and `red;blue` is a list... so let's just write that into the headers:
|
11
11
|
|
12
12
|
```
|
13
|
-
name,income:number,created_at:date,tags:list
|
14
|
-
Seamus,12301.2,2012-02-21,red;blue
|
13
|
+
name,income:number,created_at:date,tags:list,great:boolean
|
14
|
+
Seamus,12301.2,2012-02-21,red;blue,true
|
15
15
|
```
|
16
16
|
|
17
17
|
Now let's parse it:
|
@@ -21,6 +21,7 @@ Typedcsv.foreach('file.csv', headers: true) do |row|
|
|
21
21
|
row['income'] # will be a Float
|
22
22
|
row['created_at'] # will be a Date
|
23
23
|
row['tags'] # will be an Array
|
24
|
+
row['great'] # will be TrueClass or FalseClass
|
24
25
|
end
|
25
26
|
```
|
26
27
|
|
@@ -33,6 +34,7 @@ This gem provides `Typedcsv.foreach()`, which takes exactly the same arguments a
|
|
33
34
|
* list (must be semicolon-separated)
|
34
35
|
* date (must be ISO8601)
|
35
36
|
* time (must be ISO8601)
|
37
|
+
* boolean (recognizes "true" or "false")
|
36
38
|
|
37
39
|
## Benchmarks
|
38
40
|
|
data/lib/typedcsv.rb
CHANGED
@@ -44,6 +44,9 @@ class Typedcsv
|
|
44
44
|
end
|
45
45
|
|
46
46
|
class Headers
|
47
|
+
TRUE = 'true'
|
48
|
+
FALSE = 'false'
|
49
|
+
EMPTY_STRING = ''
|
47
50
|
attr_reader :raw
|
48
51
|
def initialize(raw)
|
49
52
|
@raw = raw
|
@@ -72,10 +75,20 @@ class Typedcsv
|
|
72
75
|
end
|
73
76
|
private
|
74
77
|
def convert(type, v)
|
78
|
+
if v.nil? or v == EMPTY_STRING
|
79
|
+
return nil
|
80
|
+
end
|
75
81
|
case type
|
76
82
|
when 'text'
|
77
83
|
# defaults to no parsing
|
78
84
|
v
|
85
|
+
when 'boolean'
|
86
|
+
case v
|
87
|
+
when TRUE
|
88
|
+
true
|
89
|
+
when FALSE
|
90
|
+
false
|
91
|
+
end
|
79
92
|
when 'list'
|
80
93
|
CSV.parse_line(v, col_sep: ';')
|
81
94
|
when 'date'
|
data/lib/typedcsv/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typedcsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Seamus Abshere
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|
67
68
|
- LICENSE.txt
|