csv-query 0.1.13 → 0.1.14
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 +4 -4
- data/Gemfile.lock +2 -2
- data/exe/csv-query +1 -1
- data/lib/csv/query.rb +68 -81
- data/lib/csv/query/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d9774746de8aff9bb8e0e6d6affc2fd480cfb6ebc0d69787af335d2877c689f
|
4
|
+
data.tar.gz: 8b168e91f484df3d6ffa87bde8b41ab529244bf3d8548a667c3d2169972fb442
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a5ab7ee5db5280f04d2ded993052f5b5ef0f4cf0ae4387a10b8b3a2a71744054279f142adb8603dbc3fdd52075fae29fad0147d41465af5c3a19ffadc047ba5
|
7
|
+
data.tar.gz: d695a4eea328700a8e6f1857e9e0f4b61612fc15186fd53d484cf7fb0299cf094df4faf5e855d7ebcf53c9f8c4d7ed002c993681a60997074d1d3c9e51bd27ed
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
csv-query (0.1.
|
4
|
+
csv-query (0.1.14)
|
5
5
|
activerecord
|
6
6
|
charlock_holmes
|
7
7
|
sqlite3
|
@@ -48,7 +48,7 @@ GEM
|
|
48
48
|
thread_safe (0.3.6)
|
49
49
|
tzinfo (1.2.5)
|
50
50
|
thread_safe (~> 0.1)
|
51
|
-
unicode-display_width (1.
|
51
|
+
unicode-display_width (1.4.0)
|
52
52
|
|
53
53
|
PLATFORMS
|
54
54
|
ruby
|
data/exe/csv-query
CHANGED
data/lib/csv/query.rb
CHANGED
@@ -8,112 +8,99 @@ require 'terminal-table'
|
|
8
8
|
|
9
9
|
module Csv
|
10
10
|
module Query
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
class CLI
|
12
|
+
# Your code goes here...
|
13
|
+
def self.start
|
14
|
+
file_path, json, sync =
|
15
|
+
ARGV.getopts(nil, 'file:', 'json').values
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
InMemoryAR.new(file_path, json).run!
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
class InMemoryAR
|
21
|
+
# temporary AR obj
|
22
|
+
class Record < ActiveRecord::Base
|
23
|
+
def to_h
|
24
|
+
h = self.attributes
|
25
|
+
h.delete("id")
|
26
|
+
h
|
27
|
+
end
|
26
28
|
end
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
# `LP(iphone)`みたいに
|
31
|
+
# 半角カッコ内にアルファベットだと
|
32
|
+
# Rubyのsyntax的にsetterと勘違いされるので対策
|
33
|
+
def header_converter
|
34
|
+
lambda do |h|
|
35
|
+
h.gsub('(','(').gsub(')', ')')
|
36
|
+
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def encoding
|
40
|
+
contents = File.read(file_path)
|
41
|
+
detection = contents.detect_encoding
|
42
|
+
detection[:encoding]
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
45
|
+
def csv
|
46
|
+
case encoding
|
47
|
+
when "Shift_JIS"
|
48
|
+
CSV.read(file_path, encoding: "SJIS:UTF-8", headers: true, header_converters: header_converter)
|
49
|
+
when "UTF-8"
|
50
|
+
CSV.read(file_path, encoding: "UTF-8:UTF-8", headers: true, header_converters: header_converter)
|
51
|
+
when "ISO-8859-1"
|
52
|
+
CSV.read(file_path, encoding: "ISO8859-1:UTF-8", headers: true, header_converters: header_converter)
|
53
|
+
end
|
52
54
|
end
|
53
|
-
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def csv_headers
|
57
|
+
csv.headers
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
+
attr_reader :file_path
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
@file_path = file_path
|
62
|
+
def initialize(file_path, json)
|
63
|
+
@json = json
|
64
|
+
@file_path = file_path
|
65
65
|
|
66
|
-
|
67
|
-
|
66
|
+
migration(csv_headers)
|
67
|
+
end
|
68
68
|
|
69
|
-
|
70
|
-
|
69
|
+
def migration csv_headers
|
70
|
+
ActiveRecord::Migration.verbose = false
|
71
71
|
|
72
|
-
|
72
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
73
73
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
ActiveRecord::Schema.define(:version => 1) do
|
75
|
+
create_table :records do |t|
|
76
|
+
csv_headers.map do |column_name|
|
77
|
+
t.text column_name.to_sym
|
78
|
+
end
|
78
79
|
end
|
79
80
|
end
|
80
81
|
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def json_format?
|
84
|
-
@json
|
85
|
-
end
|
86
|
-
|
87
|
-
def sync?
|
88
|
-
@sync
|
89
|
-
end
|
90
82
|
|
91
|
-
|
92
|
-
|
93
|
-
Record.create!(row.to_h)
|
83
|
+
def json_format?
|
84
|
+
@json
|
94
85
|
end
|
95
86
|
|
96
|
-
|
97
|
-
|
98
|
-
|
87
|
+
def run!
|
88
|
+
csv.each do |row|
|
89
|
+
Record.create!(row.to_h)
|
90
|
+
end
|
99
91
|
|
100
|
-
|
101
|
-
end
|
92
|
+
records = InMemoryAR::Record.all
|
102
93
|
|
103
|
-
|
104
|
-
CSV.open(file_path, "wb", encoding: encoding, headers: csv_headers, write_headers: true, force_quotes: true) do |csv|
|
105
|
-
records.each do |record|
|
106
|
-
csv << record.to_h.values
|
107
|
-
end
|
94
|
+
render(records)
|
108
95
|
end
|
109
|
-
end
|
110
96
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
97
|
+
def render records
|
98
|
+
if json_format?
|
99
|
+
puts Array.wrap(records).map { |e| e.to_h }.to_json
|
100
|
+
else
|
101
|
+
rows = Array.wrap(records).map { |e| e.to_h.values }
|
102
|
+
puts Terminal::Table.new :headings => csv_headers, :rows => rows
|
103
|
+
end
|
117
104
|
end
|
118
105
|
end
|
119
106
|
end
|
data/lib/csv/query/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- naofumi-fujii
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|