data_list_converter 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -40
- data/Rakefile +2 -2
- data/changelog.md +8 -0
- data/data_list_converter.gemspec +2 -1
- data/lib/data_list_converter/types/xlsx_file.rb +1 -0
- data/lib/data_list_converter/version.rb +3 -0
- data/lib/data_list_converter.rb +1 -3
- data/test/types_test.rb +4 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57ef43477b818568a4b417b7dc080fa9ff34f2b6
|
4
|
+
data.tar.gz: efefefbd9ec732ef479c619c538ef3588d7c7cb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b840c8d7e3f6b49286168c72e3a6e9a449bcde332a9cfff0b85305d60477d0e0496f074a3acc34f44d3da6514e2542b665eb61f153dfbd292e817f411c45b7c
|
7
|
+
data.tar.gz: bb541e9b0c0f7ed0edde220df745c3fbc9515966a8534e23b90466e15b8dfc100204740f1a91c850d107c458eaf3ba7c5fbe458dff5b72cb23e8164bf295aa17
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Data List Converter
|
2
2
|
|
3
|
-
Data List Converter is a tool
|
3
|
+
Data List Converter is a tool for converting data between different formats.
|
4
4
|
|
5
5
|
Example:
|
6
6
|
|
@@ -8,35 +8,53 @@ Example:
|
|
8
8
|
data = [{name: 'James', age: '22'}, {name: 'Bob', age: '33'}]
|
9
9
|
DataListConverter.convert(:item_data, :table_data, data)
|
10
10
|
# => [["name", "age"], ["James", "22"], ["Bob", "33"]]
|
11
|
-
DataListConverter.convert(:item_data, :csv_file, data, csv_file: {filename: 'result.csv'})
|
12
|
-
DataListConverter.convert(:item_data, :xls_file, data, csv_file: {filename: 'result.csv'})
|
13
11
|
|
12
|
+
require 'data_list_converter/types/csv_file'
|
13
|
+
DataListConverter.convert(:item_data, :csv_file, data, csv_file: {filename: 'result.csv'})
|
14
14
|
DataListConverter.convert(:csv_file, :item_data, {filename: 'result.csv'}) == data
|
15
|
+
|
16
|
+
require 'data_list_converter/types/xls_file'
|
17
|
+
sheets_data = {sheet1: data, sheet2: data}
|
18
|
+
DataListConverter.convert(:multi_sheet_item_data, :xls_file, sheets_data, xls_file: {filename: 'result.xls'})
|
19
|
+
DataListConverter.convert(:xls_file, :multi_sheet_item_data, {filename: 'result.xls'}) == sheets_data
|
15
20
|
```
|
16
21
|
|
17
22
|
You can also add filter to this process:
|
18
23
|
|
19
24
|
```ruby
|
20
|
-
|
21
|
-
filter
|
22
|
-
|
23
|
-
|
25
|
+
filters = [
|
26
|
+
# filter with default options
|
27
|
+
:limit,
|
28
|
+
# filter with options
|
29
|
+
{limit: {size: 2}},
|
30
|
+
# multiple filters
|
31
|
+
[{limit: {size: 12}},
|
32
|
+
{count: {size: 4}}],
|
33
|
+
]
|
34
|
+
filters.map do |filter|
|
35
|
+
data = [{name: "james"}] * 10
|
36
|
+
DataListConverter.convert(:item_data, :table_data, data, table_iterator: {filter: filter})
|
37
|
+
end
|
24
38
|
```
|
25
39
|
|
26
|
-
Please read [the source code](https://github.com/halida/data_list_converter/blob/master/lib/data_list_converter
|
27
|
-
also you can check [test examples](https://github.com/halida/data_list_converter/blob/master/test/
|
40
|
+
Please read [the source code](https://github.com/halida/data_list_converter/blob/master/lib/data_list_converter/) for more information,
|
41
|
+
also you can check [test examples](https://github.com/halida/data_list_converter/blob/master/test/).
|
28
42
|
|
29
43
|
## Data Types
|
30
44
|
|
31
|
-
- **item_data** like: `[{name: 'James', age: '22'}, ...]
|
45
|
+
- **item_data** like: `[{name: 'James', age: '22'}, ...]`, keys should be symbol.
|
32
46
|
- **item_iterator** iterator for item_data, used like: iter.call{|item| out << item}
|
33
47
|
- **table_data** like: `[["name", "age"], ["James", "22"], ["Bob", "33"], ...]`
|
34
48
|
- **table_iterator** iterator for table_data
|
35
49
|
- **csv_file** file in csv format
|
36
|
-
- **xls_file** file in excel format
|
37
|
-
- **
|
38
|
-
- **
|
39
|
-
- **
|
50
|
+
- **xls_file** file in excel format, should install `spreadsheet` gem, and `require 'data_list_converter/types/xls_file'`
|
51
|
+
- **xlsx_file** file in excel xml format, should install `rubyXL` gem, and `require 'data_list_converter/types/xlsx_file'`
|
52
|
+
- **multi_sheet** Contains several data with sheets:
|
53
|
+
- **multi_sheet_table_iterator**: like: `{sheet1: table_iterator1, sheet2: table_iterator2}`
|
54
|
+
- **multi_sheet_table_data**: like: `{sheet1: [['name', 'age'], ...], sheet2: ...}`
|
55
|
+
- **multi_sheet_item_iterator**: like: `{sheet1: item_iterator1, sheet2: item_iterator2}`
|
56
|
+
- **multi_sheet_item_data**: like: `{sheet1: [{name: 'James', age: 32}], sheet2: ...}`
|
57
|
+
- **records** ActiveRecord records, usage: `DataListConverter.convert(:records, :item_data, Users.where(condition), item_iterator: {columns: [:name, :age]})`
|
40
58
|
|
41
59
|
|
42
60
|
## Filters
|
@@ -45,34 +63,34 @@ also you can check [test examples](https://github.com/halida/data_list_converter
|
|
45
63
|
|
46
64
|
**item_iterator count**: count item_iterator items, usage: `DataListConverter.convert(:xls_file, :item_data, {filename: 'result.xls'}, item_iterator: {filter: {count: {size: 10}}})`, it will print current item counts every `size`.
|
47
65
|
|
66
|
+
Please check more [test examples](https://github.com/halida/data_list_converter/blob/master/test/filters_test.rb)
|
67
|
+
|
48
68
|
## Extend
|
49
69
|
|
50
|
-
You can add your data types and filters, example:
|
70
|
+
You can add your own data types and filters, example:
|
51
71
|
|
52
72
|
```ruby
|
53
|
-
DataListConverter.register_converter(
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}
|
77
|
-
})
|
73
|
+
DataListConverter.register_converter(:records, :item_iterator) do |records, options|
|
74
|
+
columns = options[:columns]
|
75
|
+
lambda { |&block|
|
76
|
+
records.find_each do |record|
|
77
|
+
item = columns.map do |column|
|
78
|
+
[column.first.to_sym, record.send(column[1])]
|
79
|
+
end.to_h
|
80
|
+
block.call(item)
|
81
|
+
end
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
DataListConverter.register_filter(:item_iterator, :limit) do |proc, options|
|
86
|
+
limit_size = options[:size] || 10
|
87
|
+
lambda { |&block|
|
88
|
+
limit = 0
|
89
|
+
proc.call do |item|
|
90
|
+
block.call(item)
|
91
|
+
limit += 1
|
92
|
+
break if limit >= limit_size
|
93
|
+
end
|
94
|
+
}
|
95
|
+
end
|
78
96
|
```
|
data/Rakefile
CHANGED
data/changelog.md
CHANGED
data/data_list_converter.gemspec
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "data_list_converter/version"
|
2
3
|
|
3
4
|
Gem::Specification.new do |s|
|
4
5
|
s.name = "data_list_converter"
|
5
|
-
s.version =
|
6
|
+
s.version = DataListConverter::VERSION.dup
|
6
7
|
s.platform = Gem::Platform::RUBY
|
7
8
|
s.authors = ["linjunhalida"]
|
8
9
|
s.email = ["linjunhalida@gmail.com"]
|
data/lib/data_list_converter.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
require 'data_list_converter/base'
|
2
|
+
require 'data_list_converter/version'
|
2
3
|
|
3
4
|
require 'data_list_converter/types/basic'
|
4
|
-
require 'data_list_converter/types/csv_file'
|
5
5
|
require 'data_list_converter/types/records'
|
6
6
|
require 'data_list_converter/types/multi_sheet'
|
7
|
-
require 'data_list_converter/types/xls_file' rescue LoadError
|
8
|
-
require 'data_list_converter/types/xlsx_file' rescue LoadError
|
9
7
|
|
10
8
|
require 'data_list_converter/filters/count'
|
11
9
|
require 'data_list_converter/filters/limit'
|
data/test/types_test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_list_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- linjunhalida
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/data_list_converter/types/records.rb
|
106
106
|
- lib/data_list_converter/types/xls_file.rb
|
107
107
|
- lib/data_list_converter/types/xlsx_file.rb
|
108
|
+
- lib/data_list_converter/version.rb
|
108
109
|
- test/base_test.rb
|
109
110
|
- test/filters_test.rb
|
110
111
|
- test/testdata.rb
|