array_hasher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +16 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +6 -0
- data/array_hasher.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/base.rb +9 -0
- data/examples/csv.rb +9 -0
- data/examples/custom_type.rb +8 -0
- data/examples/test.csv +5 -0
- data/lib/array_hasher/formatter.rb +62 -0
- data/lib/array_hasher/version.rb +3 -0
- data/lib/array_hasher.rb +33 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a9c3a8b25d43c841fb71f879622a0a33d286ccb
|
4
|
+
data.tar.gz: b74ea3676aca9c1f182c6ae83293f58a1eda133e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 467f10fac243a6bd90afe015a85e67800ab7889f9a4e9571542c3ab6c53f2fd2da505b622a4fba804c09063fde4065b75c49bb83e6e6a91f8bd32d898f430ea8
|
7
|
+
data.tar.gz: d2c7ecf021d69af28007acaca3238e569579296e9a6e8704c4911bfc138a13a393080c46779db771cfd994ed8b99330f824a1b3fa6cb498737ec01c7ae9f392c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
array_hasher (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.6.0)
|
12
|
+
rspec-core (~> 3.6.0)
|
13
|
+
rspec-expectations (~> 3.6.0)
|
14
|
+
rspec-mocks (~> 3.6.0)
|
15
|
+
rspec-core (3.6.0)
|
16
|
+
rspec-support (~> 3.6.0)
|
17
|
+
rspec-expectations (3.6.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.6.0)
|
20
|
+
rspec-mocks (3.6.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.6.0)
|
23
|
+
rspec-support (3.6.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
array_hasher!
|
30
|
+
bundler (~> 1.16.a)
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.0.pre.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 jiangzhi.xie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# ArrayHasher
|
2
|
+
|
3
|
+
Format Array data to a hash with your definition. it also can parse the CSV file with definition of title.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'array_hasher'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install array_hasher
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Format array
|
25
|
+
|
26
|
+
```
|
27
|
+
require 'array_hasher'
|
28
|
+
|
29
|
+
f = ArrayHasher.new_formatter([
|
30
|
+
[:a, :int], [:b, :float], [:c, proc {|v| v.split(',') }], [:d, nil, range: 3..-1]
|
31
|
+
])
|
32
|
+
f.parse(['number: 123', '$ 123.1', 'a,b,c', 'd1', 'd2', 'd3'])
|
33
|
+
# => {a: 123, b: 123.1, c: ['a', 'b', 'c'], d: ['d1', 'd2', 'd3']}
|
34
|
+
|
35
|
+
f.define_type(:my_arr) {|v| v.split(',').map(&:to_i) }
|
36
|
+
f.cols[2] = [:c, :my_arr]
|
37
|
+
f.parse(['number: 123', '$ 123.1', '1,2,3', 'd1', 'd2', 'd3'])
|
38
|
+
# => {a: 123, b: 123.1, c: [1, 2, 3], d: ['d1', 'd2', 'd3']}
|
39
|
+
```
|
40
|
+
|
41
|
+
### Parse format from a array of string
|
42
|
+
|
43
|
+
```
|
44
|
+
format = ArrayHasher.parse_formatter(['name:string', 'price:float', 'attrs:arr:{range: 2..-1}'])
|
45
|
+
# => [[:name, :string], [:price, :float], [:attrs, :arr, range: 2..-1]]
|
46
|
+
ArrayHasher.new_formatter(format)
|
47
|
+
```
|
48
|
+
|
49
|
+
### Format CSV
|
50
|
+
|
51
|
+
CSV file
|
52
|
+
|
53
|
+
```
|
54
|
+
name:bookname,price:float,"tags::{""range"": [2, 3]}",,
|
55
|
+
Hello,$1.20,A,B,C
|
56
|
+
World,$ 3.20,B,C,What’s this?
|
57
|
+
My book,USD 4.3,C,123,
|
58
|
+
Your book,1.2,Hehe,Haha,666
|
59
|
+
```
|
60
|
+
|
61
|
+
```
|
62
|
+
ext_types = {bookname: proc {|v| "<#{v}>" }}
|
63
|
+
ArrayHasher.csv_each('path/to/test.csv', ext_types) do |line|
|
64
|
+
puts line
|
65
|
+
end
|
66
|
+
|
67
|
+
# {:name=>"<Hello>", :price=>1.2, :tags=>["A", "B", "C"]}
|
68
|
+
# {:name=>"<World>", :price=>3.2, :tags=>["B", "C", "What’s this?"]}
|
69
|
+
# {:name=>"<My book>", :price=>4.3, :tags=>["C", "123", nil]}
|
70
|
+
# {:name=>"<Your book>", :price=>1.2, :tags=>["Hehe", "Haha", "666"]}
|
71
|
+
```
|
72
|
+
|
73
|
+
## Default Types
|
74
|
+
|
75
|
+
* `int` # convert string to int
|
76
|
+
* `float` # convert string to float
|
77
|
+
* `string`: # to_s
|
78
|
+
* `time` # Time.parse(string)
|
79
|
+
* `Proc` # format the value with your proc
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
84
|
+
|
85
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/array_hasher.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "array_hasher/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "array_hasher"
|
8
|
+
spec.version = ArrayHasher::VERSION
|
9
|
+
spec.authors = ["jiangzhi.xie"]
|
10
|
+
spec.email = ["xiejiangzhi@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Convert array to hash}
|
13
|
+
spec.description = %q{Convert array to hash according to your defined formatter}
|
14
|
+
spec.homepage = "https://github.com/xiejiangzhi/array_hasher"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "array_hasher"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/base.rb
ADDED
data/examples/csv.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
f = ArrayHasher.new_formatter([
|
2
|
+
[:a, :int], [:b, :float], [:c, :my_arr], [:d, nil, range: 3..-1]
|
3
|
+
])
|
4
|
+
f.define_type(:my_arr) {|v| v.split(',').map(&:to_i) }
|
5
|
+
# or
|
6
|
+
# f.types[:my_arr] = proc {|v| v.split(',').map(&:to_i) }
|
7
|
+
puts f.parse(['number: 123', '$ 123.1', '1,2,3', 'd1', 'd2', 'd3'])
|
8
|
+
|
data/examples/test.csv
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module ArrayHasher
|
4
|
+
class Formatter
|
5
|
+
attr_accessor :cols, :types
|
6
|
+
|
7
|
+
REGEXP_EMPTY = /\A\s*\z/
|
8
|
+
|
9
|
+
TYPES = {
|
10
|
+
int: Proc.new {|v| v =~ REGEXP_EMPTY ? nil : v.gsub(/\A[^\d]+/, '').to_i },
|
11
|
+
float: Proc.new {|v| v =~ REGEXP_EMPTY ? nil : v.gsub(/\A[^\d]+/, '').to_f },
|
12
|
+
string: Proc.new {|v| v.to_s },
|
13
|
+
time: Proc.new {|v| Time.parse(v) }
|
14
|
+
}
|
15
|
+
|
16
|
+
# cols:
|
17
|
+
# [
|
18
|
+
# [], # ignore this col
|
19
|
+
# [:name, :string],
|
20
|
+
# [:amount, :int],
|
21
|
+
# [:type], # don't change val
|
22
|
+
# [:other, Proc {|v| v.to_i % 2}] # convert val by proc
|
23
|
+
# [:all, nil, range: 0..-1]
|
24
|
+
# ]
|
25
|
+
def initialize(cols)
|
26
|
+
@types = TYPES.clone
|
27
|
+
|
28
|
+
@cols = cols.map do |name, type, opts|
|
29
|
+
[
|
30
|
+
name ? name.to_sym : nil,
|
31
|
+
(type.nil? || type.is_a?(Proc)) ? type : type.to_sym,
|
32
|
+
(opts || {}).each_with_object({}) {|kv, r| r[kv[0].to_sym] = kv[1] }
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def define_type(type, &block)
|
38
|
+
types[type.to_sym] = block
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse(arr)
|
42
|
+
cols.each_with_index.each_with_object({}) do |col_and_index, result|
|
43
|
+
col_opts, index = col_and_index
|
44
|
+
name, type, opts = col_opts
|
45
|
+
opts ||= {}
|
46
|
+
next if name.nil?
|
47
|
+
|
48
|
+
range = opts[:range] || index
|
49
|
+
val = range.is_a?(Array) ? arr.slice(*range) : arr[range]
|
50
|
+
|
51
|
+
result[name] = if type.is_a?(Proc)
|
52
|
+
type.call(val)
|
53
|
+
elsif type && (block = types[type.to_sym])
|
54
|
+
block.call(val)
|
55
|
+
else
|
56
|
+
val
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
data/lib/array_hasher.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "array_hasher/version"
|
2
|
+
|
3
|
+
require "array_hasher/formatter"
|
4
|
+
|
5
|
+
require 'csv'
|
6
|
+
require 'JSON'
|
7
|
+
|
8
|
+
module ArrayHasher
|
9
|
+
class << self
|
10
|
+
def new_formatter(cols)
|
11
|
+
Formatter.new(cols)
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_format(definition)
|
15
|
+
definition.map do |val|
|
16
|
+
name, type, opts = val.to_s.split(':', 3)
|
17
|
+
|
18
|
+
[
|
19
|
+
(name && name.length > 0) ? name.to_sym : nil,
|
20
|
+
(type && type.length > 0) ? type.to_sym : nil,
|
21
|
+
(opts && opts =~ /\A\{.*\}\z/) ? JSON.parse(opts) : {}
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def csv_each(path, ext_types = {}, &block)
|
27
|
+
csv = CSV.open(path)
|
28
|
+
formatter = new_formatter(parse_format(csv.gets))
|
29
|
+
formatter.types.merge!(ext_types)
|
30
|
+
csv.each { |line| block.call(formatter.parse(line)) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array_hasher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jiangzhi.xie
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.16.a
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.16.a
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Convert array to hash according to your defined formatter
|
56
|
+
email:
|
57
|
+
- xiejiangzhi@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- array_hasher.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- examples/base.rb
|
74
|
+
- examples/csv.rb
|
75
|
+
- examples/custom_type.rb
|
76
|
+
- examples/test.csv
|
77
|
+
- lib/array_hasher.rb
|
78
|
+
- lib/array_hasher/formatter.rb
|
79
|
+
- lib/array_hasher/version.rb
|
80
|
+
homepage: https://github.com/xiejiangzhi/array_hasher
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.6.13
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Convert array to hash
|
104
|
+
test_files: []
|