Class: Csv::Query::CLI::InMemoryAR

Inherits:
Object
  • Object
show all
Defined in:
lib/csv/query.rb

Defined Under Namespace

Classes: Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, json, sql) ⇒ InMemoryAR

Returns a new instance of InMemoryAR.



64
65
66
67
68
69
70
# File 'lib/csv/query.rb', line 64

def initialize(file_path, json, sql)
  @sql = sql
  @json = json
  @file_path = file_path

  migration(csv_headers)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



62
63
64
# File 'lib/csv/query.rb', line 62

def file_path
  @file_path
end

#jsonObject (readonly)

Returns the value of attribute json.



62
63
64
# File 'lib/csv/query.rb', line 62

def json
  @json
end

#sqlObject (readonly)

Returns the value of attribute sql.



62
63
64
# File 'lib/csv/query.rb', line 62

def sql
  @sql
end

Instance Method Details

#csvObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/csv/query.rb', line 47

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_headersObject



58
59
60
# File 'lib/csv/query.rb', line 58

def csv_headers
  csv.headers
end

#encodingObject



41
42
43
44
45
# File 'lib/csv/query.rb', line 41

def encoding
  contents = File.read(file_path)
  detection = contents.detect_encoding
  detection[:encoding]
end

#header_converterObject

‘LP(iphone)`みたいに 半角カッコ内にアルファベットだと Rubyのsyntax的にsetterと勘違いされるので対策



35
36
37
38
39
# File 'lib/csv/query.rb', line 35

def header_converter
  lambda do |h|
    h.gsub('(','').gsub(')', '')
  end
end

#json_format?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/csv/query.rb', line 86

def json_format?
  json
end

#migration(csv_headers) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/csv/query.rb', line 72

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

#recordsObject



98
99
100
101
102
103
104
# File 'lib/csv/query.rb', line 98

def records
  if sql.present?
    InMemoryAR::Record.find_by_sql(sql)
  else
    InMemoryAR::Record.all
  end
end

#render(records) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/csv/query.rb', line 106

def render records
  if json_format?
    puts Array.wrap(records).map { |e| e.to_h }.to_json
  else
    rows = Array.wrap(records).map { |e| e.to_h.values }
    puts Terminal::Table.new :headings => csv_headers, :rows => rows
  end
end

#run!Object



90
91
92
93
94
95
96
# File 'lib/csv/query.rb', line 90

def run!
  csv.each do |row|
    Record.create!(row.to_h)
  end

  render(records)
end