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 +4 -4
- data/lib/dino_utils/version.rb +1 -1
- data/lib/dino_utils.rb +2 -0
- data/lib/tn/csv_builder.rb +51 -0
- data/lib/tn/temp_file.rb +33 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55c0be58943aed51e6dfd6febd331afe5fe72d20
|
4
|
+
data.tar.gz: 2e5b247b3fd9c8afb603082f4fb17958ed337caa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e776cb5141a8afa54aa33922072c6057eba5198f90c1782ed2ff518cea92a47f57a26c05771794afe71ac093693cedbba6dac5e9d995ef169e48478f4355be9
|
7
|
+
data.tar.gz: ce1da0d563c48f6292d6a56b93468af12ad033b7f7ff4b86303811b9a50b85106c76d7cd6bd04be2c8a2c73caa3bdbcac395bfebdeb664dc4228acb5bdaacb3d
|
data/lib/dino_utils/version.rb
CHANGED
data/lib/dino_utils.rb
CHANGED
@@ -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
|
data/lib/tn/temp_file.rb
ADDED
@@ -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.
|
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-
|
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
|