metacrunch-file 1.1.0 → 1.2.0

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: f457f5fcca1eb193efe81c0e7c8f02d73b42bc7e
4
- data.tar.gz: 64f70e6bf5a0d45014853bbae5d09e75456a67c1
3
+ metadata.gz: c5561aee710c9450372e675cd2f528f0024d1b41
4
+ data.tar.gz: 960154bbd9225493784f55b6750ef08bc665c047
5
5
  SHA512:
6
- metadata.gz: 80edcd6aaa76b570371fd14b6bbe59a50df168e2471e265075b5607504ae8270d33a2787dfab48bf038d30d8d8261b1c178d7ec306f127873b2bac1a654f64ac
7
- data.tar.gz: 3ab68b17ab6f7971e286d6177db0c80a4d1b8a83d1d97b707e1ee188e1af55eb2ba1239bc2d47262abc190f7f52503418577e62e5ec6707f4e5d188b13e77197
6
+ metadata.gz: bf579706cde53758b82c5d833be4bc05e064532488c642600624c168f5cfb1eaeecd7cb80bbe006b58161633c4d7f7bdea0be9fb74655370308dc0fc167ebacf
7
+ data.tar.gz: c67526eb2803612c36940220554704c73238e7424168bfce67c9741baa163fee20992e4512e8b4085516c5c61fcb4cd5b2f27a8c850efffad35c8268993d03e3
data/Readme.md CHANGED
@@ -14,7 +14,7 @@ Installation
14
14
  Include the gem in your `Gemfile`
15
15
 
16
16
  ```ruby
17
- gem "metacrunch-file", "~> 1.0.0"
17
+ gem "metacrunch-file", "~> 1.2.0"
18
18
  ```
19
19
 
20
20
  and run `$ bundle install` to install it.
@@ -67,12 +67,26 @@ transformation ->(file_entry) do
67
67
  end
68
68
  ```
69
69
 
70
+ ### `Metacrunch::File::Destination`
71
+
72
+ This class provides a metacrunch `destination` to write data to a file. Every data that gets passed to the destination is appended to the given file. If the data is an `Array` every element of that array is appended to the file. Non existing files will be created automatically.
73
+
74
+ ```ruby
75
+ # my_job.metacrunch
76
+
77
+ source Metacrunch::File::Destination.new("/tmp/my-data.txt" [, OPTIONS])
78
+ ```
79
+
80
+ **Options**
81
+
82
+ * `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`.
83
+ *
70
84
 
71
85
  ### `Metacrunch::File::XLSXDestination`
72
86
 
73
87
  This class provides a metacrunch `destination` implementation to create simple Excel (xlsx) files.
74
88
 
75
- To use this destination a transformation is required to format the data in a proper array that can be feet into the destination. When defining the destination you must provide an array of column names. Each data row passed to the destination must be an array of the same size as the column array.
89
+ To use this destination a transformation is required to format the data in a proper array that can be passed to the destination. When defining the destination you must provide an array of column names. Each data row passed to the destination must be an array of the same size as the column array.
76
90
 
77
91
  ```ruby
78
92
  # my_job.metacrunch
@@ -5,6 +5,7 @@ module Metacrunch
5
5
  module File
6
6
  require_relative "file/entry"
7
7
  require_relative "file/source"
8
+ require_relative "file/destination"
8
9
  require_relative "file/xlsx_destination"
9
10
  end
10
11
  end
@@ -0,0 +1,36 @@
1
+ require "metacrunch/file"
2
+
3
+ module Metacrunch
4
+ class File::Destination
5
+
6
+ DEFAULT_OPTIONS = {
7
+ override_existing_file: false
8
+ }
9
+
10
+ def initialize(filename, options = {})
11
+ @filename = ::File.expand_path(filename)
12
+ @options = DEFAULT_OPTIONS.deep_merge(options)
13
+
14
+ if ::File.exists?(@filename) && @options[:override_existing_file] == false
15
+ raise "File `#{@filename}` exists but `override_existing_file` option was set to `false`"
16
+ end
17
+ end
18
+
19
+ def write(data)
20
+ return if data.blank?
21
+
22
+ if data.is_a?(Array)
23
+ ::File.open(@filename, 'ab+') do |file|
24
+ data.each { |row| file.write(row) }
25
+ end
26
+ else
27
+ ::File.open(@filename, 'ab+') { |file| file.write(data) }
28
+ end
29
+ end
30
+
31
+ def close
32
+ # noop
33
+ end
34
+
35
+ end
36
+ end
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Metacrunch
3
3
  module File
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metacrunch-file
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - René Sprotte
@@ -53,6 +53,7 @@ files:
53
53
  - Readme.md
54
54
  - bin/console
55
55
  - lib/metacrunch/file.rb
56
+ - lib/metacrunch/file/destination.rb
56
57
  - lib/metacrunch/file/entry.rb
57
58
  - lib/metacrunch/file/source.rb
58
59
  - lib/metacrunch/file/version.rb