array_hasher 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/README.md +51 -12
- data/examples/csv.rb +6 -1
- data/lib/array_hasher.rb +5 -1
- data/lib/array_hasher/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a911e20523c7d6e32acd5b9b7966c42fac7a4162
|
4
|
+
data.tar.gz: 0d368a949b832854f4fcf251d352e6f74bd03985
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6569caa11bc8414f3e37cca4fcbf28a6322de6c5da967a7ea6e45715213fe8aee9a45540c873e0cf4e0bed79729a88e7e2c5b73e8a901d3790cad009c9ffa710
|
7
|
+
data.tar.gz: 7fbb2f6f968265a22ea30ebe188fbd5237d19bb353248851352aa8eeb8bb5c73ef425584f8e3ebcaec60211d7155c43bd64eac5a6dc1952fbacff8441978b832
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
ArrayHasher
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/array_hasher)
|
4
4
|
|
5
|
-
[](https://travis-ci.org/xiejiangzhi/array_hasher)
|
6
6
|
|
7
7
|
Format Array data to a hash with your definition. it also can parse the CSV file with definition of title.
|
8
8
|
|
@@ -27,32 +27,49 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
### Format array
|
29
29
|
|
30
|
+
New a formatter
|
31
|
+
|
30
32
|
```
|
31
33
|
require 'array_hasher'
|
32
34
|
|
33
35
|
f = ArrayHasher.new_formatter([
|
34
|
-
|
36
|
+
# [hash_key, data_type]
|
37
|
+
[:a, :int],
|
38
|
+
[:b, :float],
|
39
|
+
[:c, proc {|v| v.split(',') }],
|
40
|
+
[:d, nil, range: 3..-1]
|
35
41
|
])
|
42
|
+
```
|
43
|
+
|
44
|
+
Array to hash by formatter
|
45
|
+
|
46
|
+
```
|
36
47
|
f.parse(['number: 123', '$ 123.1', 'a,b,c', 'd1', 'd2', 'd3'])
|
37
48
|
# => {a: 123, b: 123.1, c: ['a', 'b', 'c'], d: ['d1', 'd2', 'd3']}
|
49
|
+
```
|
50
|
+
|
51
|
+
Define your data type
|
38
52
|
|
53
|
+
```
|
39
54
|
f.define_type(:my_arr) {|v| v.split(',').map(&:to_i) }
|
40
55
|
f.cols[2] = [:c, :my_arr]
|
41
56
|
f.parse(['number: 123', '$ 123.1', '1,2,3', 'd1', 'd2', 'd3'])
|
42
57
|
# => {a: 123, b: 123.1, c: [1, 2, 3], d: ['d1', 'd2', 'd3']}
|
43
58
|
```
|
44
59
|
|
45
|
-
###
|
60
|
+
### Format CSV
|
46
61
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
```
|
62
|
+
For a CSV file, add a defination to csv first line, `hash_key:data_type:options`
|
63
|
+
If a column's `hash_key` is empty, we will ignore this column
|
64
|
+
If a column's `data_type` is empty, :string will be use.
|
65
|
+
`options` is a JSON of hash, it is optional.
|
52
66
|
|
53
|
-
|
67
|
+
For examples
|
54
68
|
|
55
|
-
|
69
|
+
* `name`: equal to `name:string`
|
70
|
+
* ` `, `:`, `:string`: this column will be ignore
|
71
|
+
* `tags::{"range": [2,3]}`: we'll put `line[2...(2+3)]` to the `tags` key
|
72
|
+
* `name:undefined_type`: if you give a undefined type, and you didn't define it in your code, its type is `string`
|
56
73
|
|
57
74
|
```
|
58
75
|
name:bookname,price:float,"tags::{""range"": [2, 3]}",,
|
@@ -62,7 +79,11 @@ My book,USD 4.3,C,123,
|
|
62
79
|
Your book,1.2,Hehe,Haha,666
|
63
80
|
```
|
64
81
|
|
82
|
+
Define our data type and parser
|
83
|
+
|
65
84
|
```
|
85
|
+
# `bookname` type was used in that CSV file
|
86
|
+
# We can define this type, it will tell parser how to parse data of bookname
|
66
87
|
ext_types = {bookname: proc {|v| "<#{v}>" }}
|
67
88
|
ArrayHasher.csv_each('path/to/test.csv', ext_types) do |line|
|
68
89
|
puts line
|
@@ -72,7 +93,25 @@ end
|
|
72
93
|
# {:name=>"<World>", :price=>3.2, :tags=>["B", "C", "What’s this?"]}
|
73
94
|
# {:name=>"<My book>", :price=>4.3, :tags=>["C", "123", nil]}
|
74
95
|
# {:name=>"<Your book>", :price=>1.2, :tags=>["Hehe", "Haha", "666"]}
|
96
|
+
|
97
|
+
ArrayHasher.csv_each('path/to/test.csv', ext_types) # <Enumerator: xxx>
|
98
|
+
```
|
99
|
+
|
100
|
+
### Put multiple columns to one key.
|
101
|
+
|
75
102
|
```
|
103
|
+
# We can append a `range` option as third arguments
|
104
|
+
# `range` also can be used in CSV title
|
105
|
+
format = ArrayHasher.parse_formatter([
|
106
|
+
'name:string',
|
107
|
+
'price:float',
|
108
|
+
'attrs:arr:{"range": [2, 100]}'
|
109
|
+
])
|
110
|
+
# => [[:name, :string], [:price, :float], [:attrs, :arr, range: 2..-1]]
|
111
|
+
|
112
|
+
ArrayHasher.new_formatter(format)
|
113
|
+
```
|
114
|
+
|
76
115
|
|
77
116
|
### Examples
|
78
117
|
|
@@ -84,7 +123,7 @@ See [Here](./examples)
|
|
84
123
|
* `float` # convert string to float
|
85
124
|
* `string`: # to_s
|
86
125
|
* `time` # Time.parse(string)
|
87
|
-
* `Proc` # format the value with your proc
|
126
|
+
* `Proc` # format the value with your proc. we can define a Proc in our code only.
|
88
127
|
|
89
128
|
|
90
129
|
## Development
|
data/examples/csv.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'array_hasher'
|
3
3
|
|
4
|
+
puts "Undefined data type 'bookname'"
|
5
|
+
ArrayHasher.csv_each(File.expand_path('../test.csv', __FILE__)) do |line|
|
6
|
+
puts line
|
7
|
+
end
|
8
|
+
|
9
|
+
puts "\nDefined 'bookname' wrap with <>"
|
4
10
|
ext_types = {bookname: proc {|v| "<#{v}>" }}
|
5
11
|
ArrayHasher.csv_each(File.expand_path('../test.csv', __FILE__), ext_types) do |line|
|
6
12
|
puts line
|
7
13
|
end
|
8
14
|
|
9
|
-
|
data/lib/array_hasher.rb
CHANGED
@@ -27,7 +27,11 @@ module ArrayHasher
|
|
27
27
|
csv = CSV.open(path)
|
28
28
|
formatter = new_formatter(parse_format(csv.gets))
|
29
29
|
formatter.types.merge!(ext_types)
|
30
|
-
|
30
|
+
if block
|
31
|
+
csv.each { |line| block.call(formatter.parse(line)) }
|
32
|
+
else
|
33
|
+
Enumerator.new { |y| csv.each { |line| y << formatter.parse(line) } }
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
data/lib/array_hasher/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_hasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jiangzhi.xie
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|