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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e81f7c4a5af21d396b610651b90d8815bf57b93721db4b83ceb50accc2dba055
4
- data.tar.gz: 7addf7ffdebdcf96b566812a1e273cab128ded81ec7095b7f93fb26b03b7972d
3
+ metadata.gz: 6e2eb91c43cde7f52f32a6cb81ff9bfbe1db93d78a5e40ee0faecedec42c4e0d
4
+ data.tar.gz: a5d750c90a8a2ca068a0ee0767aa16855532a8bbb81ff2d956e1c0d0f8602656
5
5
  SHA512:
6
- metadata.gz: 9304d5cc064e2d53f1e2756278cbf9a753be72da559ab900b5baac7e76b526fca1b0dc5292bddc413dcdf8fe9271e262347a8aa506d815eed22f853519b657e2
7
- data.tar.gz: 26506e5378dcd310bd6ab3de5a927f62859e5ea6fa83dc599ced5596856a85216a6a2d07d8a75b4370f3897ad29ebab2745641e944ae525b3468bc1439421b64
6
+ metadata.gz: 6e28babdaf65665beaa4d73fec25c86403811a3fa5a0d310033cfe775ad92073132347c4db40d4234daba0d51d5706d00ceb81091c35790c090cdabedd52b4a2
7
+ data.tar.gz: 9fda521cd933cd080e4fdb50672c5132f7014225c37da0eb78bf4722082722799642aff4d1fc09020d37a8876272d7d043237faccf741aeddd1de7d0d87e396e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv-query (0.1.11)
4
+ csv-query (0.1.12)
5
5
  activerecord
6
6
  charlock_holmes
7
7
  sqlite3
@@ -1,5 +1,5 @@
1
1
  module Csv
2
2
  module Query
3
- VERSION = "0.1.11"
3
+ VERSION = "0.1.12"
4
4
  end
5
5
  end
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 csv
38
+ def encoding
35
39
  contents = File.read(file_path)
36
40
  detection = contents.detect_encoding
37
- case detection[:encoding]
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
- InMemoryAR::Record.all
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.attributes }.to_json
111
+ puts records.map { |e| e.to_h }.to_json
86
112
  else
87
- rows = records.map { |e| e.attributes.values }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv-query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - naofumi-fujii