dino_utils 0.1.10 → 0.1.11

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: 8872337516c4a023f16c68fa89a08a97cced682e
4
- data.tar.gz: d8364252a4bdc7614c47f32d93d60d7baf9d751f
3
+ metadata.gz: 55c0be58943aed51e6dfd6febd331afe5fe72d20
4
+ data.tar.gz: 2e5b247b3fd9c8afb603082f4fb17958ed337caa
5
5
  SHA512:
6
- metadata.gz: f9a202ee692e138e040f0f6522a9c8f106e2b207f088fa0e543eb78481d9981f8bfea20d66c427c5f372c5f631065abb5772b60c0f8440fc24f19094fe45727d
7
- data.tar.gz: 039ac434917e43bfab694c301b9e826e5e6528d0e60e7c575465e2da81c2944379bf8bc2fe9b0ab5e75deafe5b66b37d38a42d84012edbce70fa8efe78ed0d93
6
+ metadata.gz: 1e776cb5141a8afa54aa33922072c6057eba5198f90c1782ed2ff518cea92a47f57a26c05771794afe71ac093693cedbba6dac5e9d995ef169e48478f4355be9
7
+ data.tar.gz: ce1da0d563c48f6292d6a56b93468af12ad033b7f7ff4b86303811b9a50b85106c76d7cd6bd04be2c8a2c73caa3bdbcac395bfebdeb664dc4228acb5bdaacb3d
@@ -1,3 +1,3 @@
1
1
  module DinoUtils
2
- VERSION = "0.1.10"
2
+ VERSION = "0.1.11"
3
3
  end
data/lib/dino_utils.rb CHANGED
@@ -4,3 +4,5 @@ require_relative 'string_monkeypatch'
4
4
  require_relative 'tn'
5
5
  require_relative 'tn/http'
6
6
  require_relative 'tn/advisory_lock'
7
+ require_relative 'tn/csv_builder'
8
+ require_relative 'tn/temp_file'
@@ -0,0 +1,51 @@
1
+ require 'csv'
2
+
3
+ module TN::CSVBuilder
4
+ module ClassMethods
5
+ def csv_header(attribute, name: attribute)
6
+ @csv_headers ||= {}
7
+ @csv_headers[attribute] = name
8
+ end
9
+
10
+ attr_reader :csv_headers
11
+ end
12
+
13
+ module InstanceMethods
14
+ def csv(io, flush_after_each_row: false)
15
+ csv_header_row ||= CSV::Row.new(csv_headers.keys, csv_headers.values, true)
16
+ csv = CSV.new(io)
17
+ csv << csv_header_row
18
+ csv_data do |row|
19
+ row_data = []
20
+ csv_headers.each do |key, _|
21
+ row_data << row[key]
22
+ end
23
+ row.each do |key, _|
24
+ fail "#{key} not a valid csv header!" unless csv_headers.has_key?(key)
25
+ end
26
+ csv << CSV::Row.new(csv_headers, row_data)
27
+ csv.flush if flush_after_each_row
28
+ end
29
+ csv
30
+ end
31
+
32
+ def generate_csv_tempfile(flush_after_each_row: false)
33
+ file = TN::TempFile.new
34
+ csv(file, flush_after_each_row: flush_after_each_row)
35
+ file.close
36
+ if block_given?
37
+ yield(file)
38
+ end
39
+ file
40
+ end
41
+
42
+ def csv_headers
43
+ self.class.csv_headers
44
+ end
45
+ end
46
+
47
+ def self.included(klass)
48
+ klass.extend(ClassMethods)
49
+ klass.send(:include, InstanceMethods)
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ module TN
2
+ class TempFile
3
+
4
+ def initialize(content:nil, file:nil, name: 'tanga')
5
+ extension = File.extname(name)
6
+ file_name = SecureRandom.hex
7
+ @file = file || ::Tempfile.new([file_name, extension])
8
+ @file.binmode
9
+ if content
10
+ @file.write(content) if content
11
+ @file.close
12
+ end
13
+
14
+ if block_given?
15
+ yield(self)
16
+ done
17
+ end
18
+ end
19
+
20
+ def method_missing(method, *args, &block)
21
+ @file.send(method, *args, &block)
22
+ end
23
+
24
+ def path
25
+ File.absolute_path(@file.path)
26
+ end
27
+
28
+ def done
29
+ @file.close
30
+ @file.unlink
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dino_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Van Dyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-02 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -145,7 +145,9 @@ files:
145
145
  - lib/string_monkeypatch.rb
146
146
  - lib/tn.rb
147
147
  - lib/tn/advisory_lock.rb
148
+ - lib/tn/csv_builder.rb
148
149
  - lib/tn/http.rb
150
+ - lib/tn/temp_file.rb
149
151
  homepage: https://github.com/tanga/dino_utils
150
152
  licenses:
151
153
  - MIT