Class: Csv::Query::InMemoryAR
- Inherits:
-
Object
- Object
- Csv::Query::InMemoryAR
- Defined in:
- lib/csv/query.rb
Defined Under Namespace
Classes: Record
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
- #csv ⇒ Object
- #csv_headers ⇒ Object
- #encoding ⇒ Object
- #export_as_csv(records) ⇒ Object
-
#header_converter ⇒ Object
‘LP(iphone)`みたいに 半角カッコ内にアルファベットだと Rubyのsyntax的にsetterと勘違いされるので対策.
-
#initialize(file_path, json, sync) ⇒ InMemoryAR
constructor
A new instance of InMemoryAR.
- #json_format? ⇒ Boolean
- #migration(csv_headers) ⇒ Object
- #render(records) ⇒ Object
- #run! ⇒ Object
- #sync? ⇒ Boolean
Constructor Details
#initialize(file_path, json, sync) ⇒ InMemoryAR
Returns a new instance of InMemoryAR.
61 62 63 64 65 66 67 |
# File 'lib/csv/query.rb', line 61 def initialize(file_path, json, sync) @sync = sync @json = json @file_path = file_path migration(csv_headers) end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
59 60 61 |
# File 'lib/csv/query.rb', line 59 def file_path @file_path end |
Instance Method Details
#csv ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/csv/query.rb', line 44 def csv case encoding when "Shift_JIS" CSV.read(file_path, encoding: "SJIS:UTF-8", headers: true, header_converters: header_converter) when "UTF-8" CSV.read(file_path, encoding: "UTF-8:UTF-8", headers: true, header_converters: header_converter) when "ISO-8859-1" CSV.read(file_path, encoding: "ISO8859-1:UTF-8", headers: true, header_converters: header_converter) end end |
#csv_headers ⇒ Object
55 56 57 |
# File 'lib/csv/query.rb', line 55 def csv_headers csv.headers end |
#encoding ⇒ Object
38 39 40 41 42 |
# File 'lib/csv/query.rb', line 38 def encoding contents = File.read(file_path) detection = contents.detect_encoding detection[:encoding] end |
#export_as_csv(records) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/csv/query.rb', line 103 def export_as_csv records CSV.open(file_path, "wb", encoding: encoding, headers: csv_headers, write_headers: true, force_quotes: true) do |csv| records.each do |record| csv << record.to_h.values end end end |
#header_converter ⇒ Object
‘LP(iphone)`みたいに半角カッコ内にアルファベットだとRubyのsyntax的にsetterと勘違いされるので対策
32 33 34 35 36 |
# File 'lib/csv/query.rb', line 32 def header_converter lambda do |h| h.gsub('(','(').gsub(')', ')') end end |
#json_format? ⇒ Boolean
83 84 85 |
# File 'lib/csv/query.rb', line 83 def json_format? @json end |
#migration(csv_headers) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/csv/query.rb', line 69 def migration csv_headers ActiveRecord::Migration.verbose = false ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") ActiveRecord::Schema.define(:version => 1) do create_table :records do |t| csv_headers.map do |column_name| t.text column_name.to_sym end end end end |
#render(records) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/csv/query.rb', line 111 def render records if json_format? puts records.map { |e| e.to_h }.to_json else rows = records.map { |e| e.to_h.values } puts Terminal::Table.new :headings => csv_headers, :rows => rows end end |
#run! ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/csv/query.rb', line 91 def run! csv.each do |row| Record.create!(row.to_h) end records = InMemoryAR::Record.all export_as_csv(records) if sync? render(records) end |
#sync? ⇒ Boolean
87 88 89 |
# File 'lib/csv/query.rb', line 87 def sync? @sync end |