;;(in-package #:cl-user) ; Not sure if this needs to exist (Chapter 4)
( defpackage rails-to-caveman.model
( :use #:cl
#:rails-to-caveman.db
#:mito ) )
( in-package #:rails-to-caveman.model )
;;; Defines the USER table class for the database.
;;; Will be used by mito (an ORM).
( defclass user ( )
( ( number
:col-type
:integer
:initarg
:number
:reader number-of )
( name :col-type ( :varchar 64 )
:initarg
:name
:reader name-of )
( full-name :col-type
( or ( :varchar 128 ) :null )
:initarg
:full-name
:reader full-name-of )
( email :col-type
( or :null :text )
:initarg
:email
:accessor email-of )
( birthday :col-type
( or :null :date )
:initarg
:birthday
:reader birthday-of )
( sex :col-type
:integer
:initarg
:sex
:initform 1
:reader sex-of )
#| CHANGED :ACCESSOR VALUE FROM TUTORIAL (CHAPTER 4)
===================================================
The Chapter 4 tutorial has the :accessor value set to
'administratorp '. Unfortunately, this causes initialisation
argument errors when trying to seed the database. To fix this, I
had to remove the 'p ' part from that line. At the time of
writing, I do not know if that will have a negative effect on
future tutorials.
I have left a note in the 'seeds ' function below highlighting the
change to the :accessor value.
| #
( administrator :col-type
:boolean
:initarg
:administrator
:initform nil
:accessor administrator ) )
( :metaclass mito:dao-table-class ) )
( defun seeds ( )
;; '#(' are ARRAY LITERALS. I keep forgetting this and need to look
;; it up.
( let ( ( names
#( "Taro" "Jiro" "Hana" "John" "Mike" "Sophy" "Bill" "Alex" "Mary" "Tom" ) )
( fnames ; First Names
#( "Hippo" "Darling" "Lopez" "Jerry" ) )
( gnames ; Given Names
#( "Orange" "Fox" "Snake" ) ) )
( with-connection ( db )
( dotimes ( x 10 )
( mito:create-dao 'user
:number ( + x 10 )
:name ( aref names x )
:full-name ( format nil "~A ~A"
( aref fnames ( rem x 4 ) )
( aref gnames ( rem x 3 ) ) )
:email ( format nil "~A@example.com" ( aref names x ) )
:birthday "1981-12-01"
:sex ( nth ( rem x 3 ) ' ( 1 1 2 ) )
;; Removed 'p' from end of :administrator --
;; so the code differs from the code in the
;; tutorial (Chapter 4). I had to change it
;; because it produced errors when trying to
;; seed the database (using 'seeds'
;; function. I have, also, left a note in the
;; 'user' class definition highlighting this.
:administrator ( zerop 0 ) ) ) ) ) )
( defun rebuild ( )
"Drops the current database table, recreates it and populates it using seeded data."
( with-connection ( db )
( mito:recreate-table 'user ) )
( seeds ) )
( defun ids ( )
" Produces a list of all the Id 's in the database. Part of Chapter 4
tutorial and is a port of the 'ids method ' in the Ruby on Rails book
this tutorial was translated/ported from. "
( rails-to-caveman.db:with-connection ( rails-to-caveman.db:db )
( mapcar #' mito:object-id
( mito:retrieve-dao 'rails-to-caveman.model::user ) ) ) )