#!/usr/bin/env ruby

require 'bundler/setup'
require 'ar_mapper'
require 'pry'
require 'sqlite3'

# Open a database
begin
  @db = SQLite3::Database.new 'test.db'

  # Create a table
  @db.execute_batch %{
    create table authors (
      id    integer primary key,
      name  varchar
    );

    create table articles (
      id        integer primary key,
      author_id integer,
      title     varchar,
      body      text,
      foreign key(author_id) references authors(id)
    );

    create table messages (
      id   integer primary key,
      user_id integer,
      body varchar,
      foreign key(user_id) references authors(id)
    );
  }
rescue SQLite3::Exception => e
  puts 'test.db already exist'
  puts e
end

# Configuration
ArMapper.configure do |config|
  config.database = @db
end

# default usage
class Author < ArMapper::Base
  has_many :articles
end

class Article < ArMapper::Base
  belongs_to :authors
end

# custom usage
class Comment < ArMapper::Base
  set_table :messages
  belongs_to :authors, :user_id
end

Pry.start
