metacrunch-file 1.4.0 → 1.5.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: 1a64cc33b8badb7d4e6b7c29267501db505edbb081ed55be3c4bdf269109eac8
4
- data.tar.gz: 0bb18d835fb8e812ff04aded3b8bfc3d90f8daae606704c49d2dbfe502cdaafd
3
+ metadata.gz: 873e28ee7fabd11484426b0282b9857025b95452ec7cabffabf98a163326f8e5
4
+ data.tar.gz: 46f10b360760fa3259d46a1578ee62bffb9134511d4fd76ca0247d12b91f4866
5
5
  SHA512:
6
- metadata.gz: a28ff6fcb40f286673011ee27de86a15d579bdc213b379eb77781eddbd6efa5613e3d68bd16e1d016d61f9d786b143c35e2b276961affbad21d71707060c65a4
7
- data.tar.gz: e58ec6254b11f956c4a45a0ef68477cee7b9bb2e79ce5fd283f5a3e0450e0e4b49c5c53796f7e6234ad9742d46fd42e19fed6bd2be231847b8de6a82cb188d57
6
+ metadata.gz: 28f0edd5914fac3b1611943e8d8bbdbb9c2c33a92c4fd45cd3b579a61cbf7eaf2c6db489878ff878d31ceeb75262ff76f95fedf4739e9e90e13cf892e2cf0de8
7
+ data.tar.gz: 62aec554a36543043909c814d1331dc3951997be24fcc9c6a4839b10d454c7f4287160aaf99c80a7bed33d2a495e8bce676fe17e29c26ee03e7ce8774be40940
data/Readme.md CHANGED
@@ -17,7 +17,7 @@ Installation
17
17
  Include the gem in your `Gemfile`
18
18
 
19
19
  ```ruby
20
- gem "metacrunch-file", "~> 1.4.0"
20
+ gem "metacrunch-file", "~> 1.5.0"
21
21
  ```
22
22
 
23
23
  and run `$ bundle install` to install it.
@@ -98,6 +98,25 @@ source Metacrunch::File::CSVSource.new("my.csv" [, OPTIONS])
98
98
  * `quote_char`: Quotation character. Defaults to `"`.
99
99
  * `file_encoding`: Set the file encoding. Defaults to `utf-8`.
100
100
 
101
+ ## `Metacrunch::File::CSVDestination`
102
+
103
+ This class provides a metacrunch `desination` for writing CSV files. Because [smarter_csv](https://github.com/tilo/smarter_csv) can only be used to read CSV, this class uses Ruby's [build in CSV feature](https://ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV.html) under the hood.
104
+
105
+ ```ruby
106
+ # my_job.metacrunch
107
+
108
+ destination Metacrunch::File::CSVDestination.new(
109
+ "result.csv", # filename
110
+ ["Header 1", "Header 2", ...], # headers
111
+ [, OPTIONS]
112
+ )
113
+ ```
114
+
115
+ **Options**
116
+
117
+ * `override_existing_file`: Overrides an existing file if set to `true`. If set to `false` an error is raised if the file already exists. Defaults to `false`.
118
+ * `csv_options`: Set options for CSV generation as `col_sep`. Full list is [here](https://ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV.html#class-CSV-label-Options).
119
+
101
120
  ## `Metacrunch::File::XLSXDestination`
102
121
 
103
122
  This class provides a metacrunch `destination` implementation to create simple Excel (xlsx) files.
@@ -10,5 +10,6 @@ module Metacrunch
10
10
  require_relative "file/file_destination"
11
11
  require_relative "file/destination"
12
12
  require_relative "file/xlsx_destination"
13
+ require_relative "file/csv_destination"
13
14
  end
14
15
  end
@@ -0,0 +1,49 @@
1
+ require "metacrunch/file"
2
+
3
+ module Metacrunch
4
+ class File::CSVDestination
5
+
6
+ DEFAULT_OPTIONS = {
7
+ override_existing_file: false,
8
+ csv_options: {}
9
+ }
10
+
11
+ def initialize(filename, headers, options = {})
12
+ @filename = ::File.expand_path(filename)
13
+ @headers = headers
14
+ @options = DEFAULT_OPTIONS.deep_merge(options)
15
+
16
+ if ::File.exists?(@filename) && @options[:override_existing_file] == false
17
+ raise "File `#{@filename}` exists but `override_existing_file` option was set to `false`"
18
+ end
19
+
20
+ @file = ::File.open(@filename, 'wb+')
21
+
22
+ if @headers.present?
23
+ raise ArgumentError, "Headers must be an Array" unless @headers.is_a?(Array)
24
+ csv_str = CSV.generate_line(@headers, **@options[:csv_options])
25
+ @file.write(csv_str)
26
+ end
27
+ end
28
+
29
+ def write(data)
30
+ return if data.blank?
31
+ raise ArgumentError, "Data must be an Array" unless data.is_a?(Array)
32
+
33
+ if data.first.is_a?(Array)
34
+ data.each do |d|
35
+ csv_str = CSV.generate_line(d, **@options[:csv_options])
36
+ @file.write(csv_str)
37
+ end
38
+ else
39
+ csv_str = CSV.generate_line(data, **@options[:csv_options])
40
+ @file.write(csv_str)
41
+ end
42
+ end
43
+
44
+ def close
45
+ @file.close if @file
46
+ end
47
+
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  module Metacrunch
2
2
  module File
3
- VERSION = "1.4.0"
3
+ VERSION = "1.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metacrunch-file
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - René Sprotte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2021-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -67,6 +67,7 @@ files:
67
67
  - Readme.md
68
68
  - bin/console
69
69
  - lib/metacrunch/file.rb
70
+ - lib/metacrunch/file/csv_destination.rb
70
71
  - lib/metacrunch/file/csv_source.rb
71
72
  - lib/metacrunch/file/destination.rb
72
73
  - lib/metacrunch/file/entry.rb
@@ -95,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
98
  requirements: []
98
- rubygems_version: 3.1.4
99
+ rubygems_version: 3.2.22
99
100
  signing_key:
100
101
  specification_version: 4
101
102
  summary: File package for the metacrunch ETL toolkit.