csv-query 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/csv/query/version.rb +1 -1
- data/lib/csv/query.rb +37 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e2eb91c43cde7f52f32a6cb81ff9bfbe1db93d78a5e40ee0faecedec42c4e0d
|
4
|
+
data.tar.gz: a5d750c90a8a2ca068a0ee0767aa16855532a8bbb81ff2d956e1c0d0f8602656
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e28babdaf65665beaa4d73fec25c86403811a3fa5a0d310033cfe775ad92073132347c4db40d4234daba0d51d5706d00ceb81091c35790c090cdabedd52b4a2
|
7
|
+
data.tar.gz: 9fda521cd933cd080e4fdb50672c5132f7014225c37da0eb78bf4722082722799642aff4d1fc09020d37a8876272d7d043237faccf741aeddd1de7d0d87e396e
|
data/Gemfile.lock
CHANGED
data/lib/csv/query/version.rb
CHANGED
data/lib/csv/query.rb
CHANGED
@@ -3,7 +3,6 @@ require 'csv'
|
|
3
3
|
require 'sqlite3'
|
4
4
|
require 'active_record'
|
5
5
|
require 'optparse'
|
6
|
-
require 'pp'
|
7
6
|
require 'charlock_holmes/string'
|
8
7
|
require 'terminal-table'
|
9
8
|
|
@@ -11,15 +10,20 @@ module Csv
|
|
11
10
|
module Query
|
12
11
|
# Your code goes here...
|
13
12
|
def self.run!
|
14
|
-
file_path, json =
|
15
|
-
ARGV.getopts(nil, 'file:', 'json').values
|
13
|
+
file_path, json, sync =
|
14
|
+
ARGV.getopts(nil, 'file:', 'json', 'sync').values
|
16
15
|
|
17
|
-
InMemoryAR.new(file_path, json).run!
|
16
|
+
InMemoryAR.new(file_path, json, sync).run!
|
18
17
|
end
|
19
18
|
|
20
19
|
class InMemoryAR
|
21
20
|
# temporary AR obj
|
22
21
|
class Record < ActiveRecord::Base
|
22
|
+
def to_h
|
23
|
+
h = self.attributes
|
24
|
+
h.delete("id")
|
25
|
+
h
|
26
|
+
end
|
23
27
|
end
|
24
28
|
|
25
29
|
# `LP(iphone)`みたいに
|
@@ -31,10 +35,14 @@ module Csv
|
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
def
|
38
|
+
def encoding
|
35
39
|
contents = File.read(file_path)
|
36
40
|
detection = contents.detect_encoding
|
37
|
-
|
41
|
+
detection[:encoding]
|
42
|
+
end
|
43
|
+
|
44
|
+
def csv
|
45
|
+
case encoding
|
38
46
|
when "Shift_JIS"
|
39
47
|
CSV.read(file_path, encoding: "SJIS:UTF-8", headers: true, header_converters: header_converter)
|
40
48
|
when "UTF-8"
|
@@ -48,7 +56,8 @@ module Csv
|
|
48
56
|
|
49
57
|
attr_reader :file_path
|
50
58
|
|
51
|
-
def initialize(file_path, json)
|
59
|
+
def initialize(file_path, json, sync)
|
60
|
+
@sync = sync
|
52
61
|
@json = json
|
53
62
|
@file_path = file_path
|
54
63
|
|
@@ -73,18 +82,35 @@ module Csv
|
|
73
82
|
@json
|
74
83
|
end
|
75
84
|
|
85
|
+
def sync?
|
86
|
+
@sync
|
87
|
+
end
|
88
|
+
|
76
89
|
def run!
|
77
90
|
csv.each do |row|
|
78
91
|
Record.create!(row.to_h)
|
79
92
|
end
|
80
93
|
|
81
|
-
records =
|
82
|
-
|
94
|
+
records = InMemoryAR::Record.all
|
95
|
+
|
96
|
+
export_as_csv(records) if sync?
|
97
|
+
|
98
|
+
render(records)
|
99
|
+
end
|
100
|
+
|
101
|
+
def export_as_csv records
|
102
|
+
CSV.open(file_path, "wb", encoding: encoding, headers: csv_headers, write_headers: true, force_quotes: true) do |csv|
|
103
|
+
records.each do |record|
|
104
|
+
csv << record.to_h.values
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
83
108
|
|
109
|
+
def render records
|
84
110
|
if json_format?
|
85
|
-
puts records.map { |e| e.
|
111
|
+
puts records.map { |e| e.to_h }.to_json
|
86
112
|
else
|
87
|
-
rows = records.map { |e| e.
|
113
|
+
rows = records.map { |e| e.to_h.values }
|
88
114
|
puts Terminal::Table.new :headings => csv_headers, :rows => rows
|
89
115
|
end
|
90
116
|
end
|