fileconv 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a11710da18bbef2f204f676c21b5827f01c0a84af1aa889353e9ef44de5043d5
4
- data.tar.gz: c8c62864940a00b078e76c63459646aad1898a544569df233f49bde1d2ec9445
3
+ metadata.gz: 1b8f07c030f514fbed40b42a39c737579023a907f7e636d3fd37714017e80bc5
4
+ data.tar.gz: d0115ed9453377e2cda186096c55380031550906bded00c8e5fe86422e005d72
5
5
  SHA512:
6
- metadata.gz: a7ed3cbcd55d7a35fab31ffa3e372ed5a80ae207a50ae1c0076cc78c7007ad2919a19dfa39e5759a49d4bece4bcce035522280cb86bcef8ba0cd7e06eb3d6090
7
- data.tar.gz: 3da1ed9b8b478a21cce27c4692e72e77010a7628636ec1881a59e4b2f0bf462bff3cbf796095e08c9a33a1db2cf64d2fe0b412a63cd54aaa6cb36e8f645e6f54
6
+ metadata.gz: 141302bfc7a4cc4a8b1bcb8fc6f97fb99064f1930621647602bc0f79c6db9fae6d5b13443b7880d31df614ce1dec27f17d70d90a3983d911efb4b8c69b1c5265
7
+ data.tar.gz: d420660694a1ec760285f38470e0b64cf4312b7fadb6edaef0791c63a503b165e150110300572989b75ecffb5d39b545bc5f96b232a5313707feafddafaceb62
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fileconv (0.1.0)
4
+ fileconv (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Fileconv
2
2
 
3
- Extensible multi-file convertor. Simple text file, CSV file, JSON file binary file and so on.
3
+ Extensible multi-file convertor. Simple text file, CSV file, JSON file, binary file and so on.
4
4
 
5
5
  `fileconv` gem is a simple to use and extensible library to convert multi-file format. You can extend your class with `MetaConvertor` and convert files into various format.
6
6
 
@@ -25,7 +25,7 @@ Or install it yourself as:
25
25
  You have to do a few things to build a Convertor with `fileconv` gem.
26
26
 
27
27
  - include `MetaConvertor`(e.g. `Fileconv::Line`) into your object,
28
- - add several hooks(e.g. `input_ext`) if you need.
28
+ - add some hooks(e.g. `input_ext`) if you need.
29
29
 
30
30
  Lets start with a simple example. It will be a convertor for simple text files.
31
31
 
@@ -89,22 +89,16 @@ the convertor convert it into:
89
89
  3: 333
90
90
  ```
91
91
 
92
- ### Variables
93
-
94
- |variable|scope|descripton|
95
- |---|---|---|
96
- |acc|file|accumulator for a file|
97
- |@meta|convertor|meta data for the convertor|
98
- |@opts|convertor|options for the convertor(`#conv` can receive options.)|
99
-
100
92
  ### Convertor Hooks
101
93
 
94
+ You can overwrite convertor hooks if you need. The default action is that a converter copies all files in the current directory to an “output” directory. The convertor make the "output" directory in the current directory if it doesn't exist.
95
+
102
96
  |hook|default|description|
103
97
  |---|---|---|
104
98
  |input_dir|"."(current directory)|input Directory|
105
99
  |input_ext|`nil` (all files)|input extension|
106
100
  |output_dir|"output"|output directory|
107
- |input_files|`nil` (use `input_dir`)|input files(array of file names)|
101
+ |input_files(files)|`files`|input files|
108
102
  |init_conv|`nil`|init convertor hook|
109
103
  |init_acc(acc)|`nil`|init accumulator hook|
110
104
  |read_file(filename, acc)|`nil` (use default reader)|read file hook|
@@ -114,10 +108,28 @@ the convertor convert it into:
114
108
  |result_filename|"result.txt"|result filename|
115
109
  |conv_result|`nil`|conversion result|
116
110
 
111
+ Input files is selected by `input_dir` and `input_ext`. you can overwrite it with `#input_files` hooks.
112
+
113
+ The most commonly used hooks are:
114
+
115
+ - `#input_ext`
116
+ - `#convert_line`
117
+ - `#convert_file`
118
+ - `#conv_result`
119
+
120
+ ### Convertor Variables
121
+
122
+ You can use several convertor variables. Convertor variables are `Hash` type and have a specific scope. `acc` has single file scope, which means that it is initialized for each file. `@meta` and `@opts` is valid in a convertor. `@opts` variable is set by `#conv` method that takes user options. you can use `@meta` variable for any purpose.
123
+
124
+ |variable|scope|descripton|
125
+ |---|---|---|
126
+ |acc|file|accumulator for a file|
127
+ |@meta|convertor|meta data for the convertor|
128
+ |@opts|convertor|options for the convertor|
117
129
 
118
- ### Default MetaConvertor
130
+ ### Default MetaConvertors
119
131
 
120
- `fileconv` gem have several default MetaConvertor:
132
+ `fileconv` gem have several default MetaConvertors. MetaConverters are mainly intended to be included by a converter.
121
133
 
122
134
  |MetaConvertor|mode|description|
123
135
  |---|---|---|
@@ -127,21 +139,21 @@ the convertor convert it into:
127
139
  |File|File|`File` convertor|
128
140
  |JSON|File|JSON convertor|
129
141
 
130
- A convertor(includes MetaConvertor) can be divided into two modes.
142
+ Convertors(includes MetaConvertors) can be divided into two modes.
131
143
 
132
144
  - Line Mode
133
- - `#convert_line` hooks is called
145
+ - `#convert_line` hooks is called.
134
146
  - e.g. `Line`, `CSV`
135
147
  - File Mode
136
- - `#convert_line` hooks is not called
148
+ - `#convert_line` hooks is not called.
137
149
  - e.g. `Data`, `File`, `JSON`
138
150
 
139
- Let's see a JSON example.
151
+ Let's see a JSON MetaConvertor usage.
140
152
 
141
153
  ```ruby
142
154
  require 'fileconv'
143
155
 
144
- class ModifyJSON
156
+ class ModifyJSONConvertor
145
157
  include Fileconv::JSON
146
158
 
147
159
  def input_ext
@@ -156,7 +168,7 @@ class ModifyJSON
156
168
  end
157
169
  end
158
170
 
159
- ModifyJSON.new.conv
171
+ ModifyJSONConvertor.new.conv
160
172
  ```
161
173
 
162
174
  original file (`address.json`) :
@@ -203,10 +215,12 @@ module Fileconv
203
215
  end
204
216
  ```
205
217
 
206
- MetaConvertor can use below hooks:
218
+ MetaConvertors can use below hooks:
207
219
 
208
220
  - pre_init_conv
209
221
  - post_init_conv
222
+ - pre_input_files
223
+ - post_input_files
210
224
  - pre_init_acc
211
225
  - post_init_acc
212
226
  - pre_convert_file
@@ -6,7 +6,7 @@ class InputFiles
6
6
  "txt"
7
7
  end
8
8
 
9
- def input_files
9
+ def input_files(files)
10
10
  ["test1.txt", "test3.txt"]
11
11
  end
12
12
 
data/lib/fileconv/base.rb CHANGED
@@ -1,46 +1,56 @@
1
1
  module Fileconv
2
2
  module MetaConvertor
3
- # Pre hook for {Convertor#init_conv}
3
+ # Pre-hook for {Convertor#init_conv}
4
4
  def pre_init_conv
5
5
  end
6
6
 
7
- # Post hook for {Convertor#init_conv}
7
+ # Post-hook for {Convertor#init_conv}
8
8
  def post_init_conv
9
9
  end
10
10
 
11
- # Pre hook for {Convertor#init_acc}
11
+ # Pre-hook for {Convertor#input_files}
12
+ def pre_input_files(files)
13
+ files
14
+ end
15
+
16
+ # Post-hook for {Convertor#input_files}
17
+ def post_input_files(files)
18
+ files
19
+ end
20
+
21
+ # Pre-hook for {Convertor#init_acc}
12
22
  def pre_init_acc(acc)
13
23
  end
14
24
 
15
- # Post hook for {Convertor#init_acc}
25
+ # Post-hook for {Convertor#init_acc}
16
26
  def post_init_acc(acc)
17
27
  end
18
28
 
19
- # Pre hook for {Convertor#convert_file}
29
+ # Pre-hook for {Convertor#convert_file}
20
30
  def pre_convert_file(file, acc)
21
31
  file
22
32
  end
23
33
 
24
- # Pre hook for {Convertor#convert_line}
34
+ # Pre-hook for {Convertor#convert_line}
25
35
  def pre_convert_line(line, acc)
26
36
  line
27
37
  end
28
38
 
29
- # Post hook for {Convertor#convert_line}
39
+ # Post-hook for {Convertor#convert_line}
30
40
  def post_convert_line(line, acc)
31
41
  line
32
42
  end
33
43
 
34
- # Pre hook for {Convertor#convert_file}
44
+ # Pre-hook for {Convertor#convert_file}
35
45
  def post_convert_file(file, acc)
36
46
  file
37
47
  end
38
48
 
39
- # Pre hook for {Convertor#conv_result}
49
+ # Pre-hook for {Convertor#conv_result}
40
50
  def pre_conv_result
41
51
  end
42
52
 
43
- # Post hook for {Convertor#conv_result}
53
+ # Post-hook for {Convertor#conv_result}
44
54
  def post_conv_result(result)
45
55
  result
46
56
  end
@@ -67,10 +77,11 @@ module Fileconv
67
77
  end
68
78
 
69
79
  # Input files
70
- # @return [Array<String>,nil]
71
- # @note ignore {#input_dir} if this method return non-nil
72
- # @note select files by {#input_dir} and {#input_ext} if this method return nil
73
- def input_files
80
+ # @param [Array<string>] files input files
81
+ # @return [Array<String>]
82
+ # @note you can overwrite default input files
83
+ def input_files(files)
84
+ files
74
85
  end
75
86
 
76
87
  # Initialize the convertor
@@ -151,28 +162,28 @@ module Fileconv
151
162
  raw = ::File.read(filename, @opts[:read_file_opts]) unless raw
152
163
  end
153
164
 
154
- pre_converted_file = pre_convert_file(raw, acc)
165
+ converted_file = pre_convert_file(raw, acc)
155
166
  if @opts[:line_mode]
156
167
  # Line mode
157
- pre_converted_file = process_line(pre_converted_file, acc)
168
+ converted_file = process_line(converted_file, acc)
158
169
  end
159
170
 
160
- converted_file = convert_file(pre_converted_file, acc)
161
- post_converted_file = post_convert_file(converted_file, acc)
162
- if post_converted_file
171
+ converted_file = convert_file(converted_file, acc)
172
+ converted_file = post_convert_file(converted_file, acc)
173
+ if converted_file
163
174
  out = output_filename(::File.basename(filename), acc)
164
175
  Dir.mkdir output_dir if !Dir.exists? output_dir
165
- ::File.write(::File.join(output_dir, out), post_converted_file, @opts[:read_file_opts])
176
+ ::File.write(::File.join(output_dir, out), converted_file, @opts[:read_file_opts])
166
177
  end
167
178
  end
168
179
 
169
180
  def process_line(raw_lines, acc)
170
181
  lines = []
171
182
  raw_lines.each do |line|
172
- pre_converted_line = pre_convert_line(line, acc)
173
- converted_line = convert_line(pre_converted_line, acc)
174
- post_converted_line = post_convert_line(converted_line, acc)
175
- lines.push *post_converted_line
183
+ converted_line = pre_convert_line(line, acc)
184
+ converted_line = convert_line(converted_line, acc)
185
+ converted_line = post_convert_line(converted_line, acc)
186
+ lines.push *converted_line
176
187
  end
177
188
  lines
178
189
  end
@@ -188,11 +199,11 @@ module Fileconv
188
199
  init_conv()
189
200
  post_init_conv()
190
201
 
191
- files = input_files()
192
- unless files
193
- glob = input_ext ? "*." + input_ext : "*"
194
- files = Dir.glob(::File.join(input_dir, glob))
195
- end
202
+ glob = input_ext ? "*." + input_ext : "*"
203
+ files = Dir.glob(::File.join(input_dir, glob))
204
+ files = pre_input_files(files)
205
+ files = input_files(files)
206
+ files = post_input_files(files)
196
207
 
197
208
  files.each do |filename|
198
209
  process_file(filename)
@@ -1,3 +1,3 @@
1
1
  module Fileconv
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fileconv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hinastory
@@ -55,29 +55,22 @@ files:
55
55
  - bin/setup
56
56
  - example/csv/add_column.rb
57
57
  - example/csv/add_header.rb
58
- - example/csv/output/test2.txt
59
58
  - example/csv/test.csv
60
59
  - example/csv/test2.txt
61
60
  - example/csv/with_header.rb
62
61
  - example/data/list_file.rb
63
62
  - example/data/output/result.txt
64
63
  - example/data/test.data
65
- - example/file/output/test.data
66
64
  - example/file/read_bytes.rb
67
65
  - example/file/test.data
68
66
  - example/json/address.json
69
67
  - example/json/modify_json.rb
70
- - example/json/output/address.json
71
68
  - example/json/pretty_json.rb
72
69
  - example/line/add_lineno.rb
73
70
  - example/line/count_lines.rb
74
71
  - example/line/filter_lines.rb
75
72
  - example/line/input_files.rb
76
73
  - example/line/modify_new_line.rb
77
- - example/line/output/test1.txt
78
- - example/line/output/test2.txt
79
- - example/line/output/test3.txt
80
- - example/line/output/test4.txt
81
74
  - example/line/sort_lines.rb
82
75
  - example/line/test1.txt
83
76
  - example/line/test2.txt
@@ -85,7 +78,6 @@ files:
85
78
  - example/line/test4.txt
86
79
  - example/line/uniq_line.rb
87
80
  - example/meta_convertor/csv2json.rb
88
- - example/meta_convertor/output/test.csv
89
81
  - example/meta_convertor/test.csv
90
82
  - fileconv.gemspec
91
83
  - lib/fileconv.rb
@@ -1,4 +0,0 @@
1
- Name,Age
2
- Taro,20
3
- Hanako,20
4
- Hideki,20
@@ -1,7 +0,0 @@
1
- abcde
2
- fghij
3
- klmno
4
- pqrst
5
- uvwxy
6
- z1234
7
- 56789
@@ -1 +0,0 @@
1
- [{"name":"Mike","Age":"21","country":"USA"},{"name":"Jon","Age":"33","country":"USA"}]
@@ -1,5 +0,0 @@
1
- 111
2
- aaa
3
- bbb
4
- ccc
5
- ddd
@@ -1,7 +0,0 @@
1
- 126
2
- 2323
3
- aaaa
4
- aaaa
5
- ddds
6
- pppppp
7
- rrrr
@@ -1,10 +0,0 @@
1
- 2281
2
- 6656
3
- ddd
4
- ggg
5
- ggg
6
- kkk
7
- sssgg
8
- sssgg
9
- sssgg
10
- ttt
@@ -1,5 +0,0 @@
1
- 111
2
- aaa
3
- bbb
4
- ccc
5
- ddd
@@ -1,17 +0,0 @@
1
- [
2
- {
3
- "Name": "Taro",
4
- "Age": "22",
5
- "Check": "true"
6
- },
7
- {
8
- "Name": "Hanako",
9
- "Age": "18",
10
- "Check": "false"
11
- },
12
- {
13
- "Name": "Hideki",
14
- "Age": "6",
15
- "Check": "false"
16
- }
17
- ]