activerecord の内容をサクッと表示する。

## Tips
デバッグ中に ActiveRecord のデータ内容を単に puts や pp で表示するのもいいが、
表形式、csv表示もちょっとした工夫で可能になる。

## サンプルコード

````samole.rb
# coding: utf-8

require 'sqlite3'
require 'active_record'
require 'faker'
require 'hirb'
require 'hirb-unicode'
require 'awesome_print'
require 'csv'

ActiveRecord::Base.establish_connection(
    :adapter => 'sqlite3',
    :database => 'db.sqlite3'
)

ActiveRecord::Migration.create_table :users do |t|
  t.column :name, :string
  t.column :birth, :datetime
  t.column :email, :string
  # t.timestamps
end

class User < ActiveRecord::Base
end

# ダミーの誕生日の作成
def rand_date(from_day = '1910-01-01', to_day ='2014-01-01')
  from, to = [Date.parse(from_day), Date.parse(to_day)]
  return from + rand(to - from + 1)
end

# ダミーデータの作成
def insert_fake_data(num = 1)
  num.times do |i|
    u = User.new(name: Faker::Name.name, birth: rand_date, email: Faker::Internet.safe_email)
    u.save!
  end
end

# csv 形式にする
def generate_csv(data, column_names = data.column_name, options = {})
  CSV.generate(options) do |csv|
    data.each do |item|
      csv << item.attributes.values_at(*column_names)
    end
  end
end

insert_fake_data(10) if User.count == 0

# 日時の書式をカスタマイズ
# Time::DATE_FORMATS[:default] = '%Y-%m-%d %H:%M:%S'
Time::DATE_FORMATS[:default] = '%Y-%m-%d'

# 表形式で表示
puts '--------------------------'
puts Hirb::Helpers::AutoTable.render User.all

# csv 形式で表示
puts '--------------------------'
cols = User.column_names
# cols.delete 'id'   # id 列を消す
options = {force_quotes: true, headers: cols, write_headers: true }
# options = {}
puts generate_csv(User.all, cols, options)

# デバッグ表示
puts '--------------------------'
ap User.all.to_a[0..1]

````

# 実行例

````
$ ruby sample.rb
-- create_table(:users)
--------------------------
+----+----------------------+------------+-----------------------------+
| id | name                 | birth      | email                       |
+----+----------------------+------------+-----------------------------+
| 1  | Lucas Olson          | 1983-05-12 | colten.schumm@example.net   |
| 2  | Celia Simonis        | 1957-10-05 | leda@example.org            |
| 3  | Betsy Bayer          | 1937-09-01 | oceane_reinger@example.org  |
| 4  | Julien Kreiger       | 2012-02-25 | karianne@example.net        |
| 5  | Lura Gleason         | 1918-10-24 | jensen_haley@example.net    |
| 6  | Erling Daugherty     | 2006-05-28 | demario_sanford@example.net |
| 7  | Aubrey Hartmann Jr.  | 1918-03-08 | makayla_reichel@example.com |
| 8  | Rocky Stamm          | 1912-04-13 | zachary.harber@example.org  |
| 9  | Melyssa Price V      | 1961-09-15 | clifton@example.net         |
| 10 | Mrs. Patsy Bahringer | 1979-06-24 | rhett.ferry@example.com     |
+----+----------------------+------------+-----------------------------+
10 rows in set
--------------------------
"id","name","birth","email"
"1","Lucas Olson","1983-05-12","colten.schumm@example.net"
"2","Celia Simonis","1957-10-05","leda@example.org"
"3","Betsy Bayer","1937-09-01","oceane_reinger@example.org"
"4","Julien Kreiger","2012-02-25","karianne@example.net"
"5","Lura Gleason","1918-10-24","jensen_haley@example.net"
"6","Erling Daugherty","2006-05-28","demario_sanford@example.net"
"7","Aubrey Hartmann Jr.","1918-03-08","makayla_reichel@example.com"
"8","Rocky Stamm","1912-04-13","zachary.harber@example.org"
"9","Melyssa Price V","1961-09-15","clifton@example.net"
"10","Mrs. Patsy Bahringer","1979-06-24","rhett.ferry@example.com"
--------------------------
[
    [0] #<User:0x007f85b2952470> {
           :id => 1,
         :name => "Lucas Olson",
        :birth => 1983-05-12 00:00:00 UTC,
        :email => "colten.schumm@example.net"
    },
    [1] #<User:0x007f85b2951e80> {
           :id => 2,
         :name => "Celia Simonis",
        :birth => 1957-10-05 00:00:00 UTC,
        :email => "leda@example.org"
    }
]
````

## ここで使った 使ったもの

- hirb  https://github.com/cldwalker/hirb

- awesome-print  https://github.com/michaeldv/awesome_print

- csv  http://docs.ruby-lang.org/ja/2.1.0/class/CSV.html