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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a099d03a2435d188931e843e69866730ef6344a
4
- data.tar.gz: 26827471d19e23d7a1b897ea50162ecba67e438d
3
+ metadata.gz: 57ef43477b818568a4b417b7dc080fa9ff34f2b6
4
+ data.tar.gz: efefefbd9ec732ef479c619c538ef3588d7c7cb0
5
5
  SHA512:
6
- metadata.gz: 048fcbab9f4a789b94f590b8a462b9655b361da82a73ca2e70f732c422cc919d10a40f95c2e7573b7dd6aa420ae325c8ef43c4501e8eee3c338fc8c843048e44
7
- data.tar.gz: d204980813037d59a7047d9d9a968e1affca8f1c593dabd944764b7a577b397947f25d727a47b8c5bed0c86e18898e43dd3ababf17f66281c7e029738bc3d189
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 to convert data between different formats.
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
- filter = :limit
21
- filter = {limit: {size: 2}}
22
- filter = [{limit: {size: 12}}, {count: {size: 4}}]
23
- convert(:item_iterator, :table_data, iter, table_iterator: {filter: filter})
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.rb) for more information,
27
- also you can check [test examples](https://github.com/halida/data_list_converter/blob/master/test/data_list_converter_test.rb).
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
- - **multi_sheet_data** like: `{'sheet1' => [columns, row, row, ...], 'sheet2' => [columns, row, row, ...]}`
38
- - **multi_sheet_iterator** like: `{'sheet1' => table_iterator1, 'sheet2' => table_iterator2}`
39
- - **records** ActiveRecord records, usage: `DataListConverter.convert(:records, :item_data, query, item_iterator: {columns: [:name, :age]})`
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
- :records, :item_iterator, lambda { |records, options|
55
- columns = options[:columns]
56
- lambda { |&block|
57
- records.find_each do |record|
58
- item = columns.map do |column|
59
- [column.first.to_sym, record.send(column[1])]
60
- end.to_h
61
- block.call(item)
62
- end
63
- }
64
- })
65
-
66
- DataListConverter.register_filter(
67
- :item_iterator, :limit, lambda { |proc, options|
68
- limit_size = options[:size] || 10
69
- lambda { |&block|
70
- limit = 0
71
- proc.call do |item|
72
- block.call(item)
73
- limit += 1
74
- break if limit >= limit_size
75
- end
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
@@ -25,9 +25,9 @@ end
25
25
 
26
26
  task :build do
27
27
  sh 'rm -f *.gem'
28
- sh 'gem build data_list_converter.gemspec'
28
+ sh 'bundle exec gem build data_list_converter.gemspec'
29
29
  end
30
30
 
31
31
  task :upload do
32
- sh 'gem push *.gem'
32
+ sh 'bundle exec gem push *.gem'
33
33
  end
data/changelog.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 0.3
2
+
3
+ Refactor.
4
+
5
+ - 0.3.1 sheet name should as symbol
6
+ - 0.3.2 support xlsx cell format
7
+
1
8
  ## 0.2
2
9
 
3
10
  Add xlsx_file data type.
@@ -5,3 +12,4 @@ Add xlsx_file data type.
5
12
  ## 0.1
6
13
 
7
14
  First version.
15
+
@@ -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 = "0.3.2"
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"]
@@ -63,6 +63,7 @@ class DataListConverter
63
63
  book.worksheets.map do |sheet|
64
64
  iterator = lambda { |&block|
65
65
  sheet.each do |row|
66
+ next unless row
66
67
  block.call(row.cells.map(&:value))
67
68
  end
68
69
  }
@@ -0,0 +1,3 @@
1
+ class DataListConverter
2
+ VERSION = "0.3.3".freeze
3
+ end
@@ -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
@@ -1,3 +1,7 @@
1
+ require 'data_list_converter/types/csv_file'
2
+ require 'data_list_converter/types/xls_file'
3
+ require 'data_list_converter/types/xlsx_file'
4
+
1
5
  describe DataListConverter do
2
6
  before :all do
3
7
  @c = DataListConverter
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.2
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 00:00:00.000000000 Z
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