Add Yard, and begin some documentation

Use Yard to generate documentation. Included in the `.yardopts` file is
configuration to also publish the markdown files in the `wiki/`
directory.

wiki/
- addons.md: Information on writing new addons.
- database.md: Information on using the database and migrations

db/migrate/*: Rename in accordance with documentation

Rakefile: Add the Yard Rake tasks
This commit is contained in:
Bill Niblock 2021-03-07 16:57:27 -05:00
parent ce06aa568b
commit c10b847e81
7 changed files with 140 additions and 7 deletions

5
.yardopts Normal file
View file

@ -0,0 +1,5 @@
--protected
--private
-
wiki/database.md
wiki/addons.md

View file

@ -9,3 +9,5 @@ gem "activerecord", "~> 6.1"
gem "rake", "~> 13.0"
gem "sqlite3", "~> 1.4"
gem "yard", "~> 0.9.26"

View file

@ -4,7 +4,7 @@ namespace :chronicle do
require 'active_record'
require_relative 'lib/chronicle'
db_config = YAML::load(File.open('config/database.yml'))
db_config = YAML::load(File.open('config/db.yml'))
ActiveRecord::Base.establish_connection(db_config)
bot_config = YAML::load(File.open('config/bot.yml'))
@ -18,18 +18,20 @@ namespace :db do
require 'active_record'
require 'yaml'
Dir[File.join(__dir__, 'db', 'migrate', '*.rb')].each do |file|
klasses = []
Dir[File.join(__dir__, 'db', 'migrate', '*.rb')].sort.each do |file|
require file
klasses << file.split('_')[1..-1].map(&:capitalize).join[0..-4]
end
task :connect do
connection_details = YAML::load(File.open('config/database.yml'))
connection_details = YAML::load(File.open('config/db.yml'))
ActiveRecord::Base.establish_connection(connection_details)
end
desc "Create a new database"
task :create do
connection_details = YAML::load(File.open('config/database.yml'))
connection_details = YAML::load(File.open('config/db.yml'))
if connection_details["adapter"] == 'sqlite3'
if File.exists?(connection_details["database"])
@ -48,12 +50,24 @@ namespace :db do
desc "Run the migrations"
task :migrate => 'db:connect' do
# ActiveRecord::MigrationContext.new('db/migrate/').migrate()
CreateCustomCommands.migrate(:up)
# CreateCustomCommands.migrate(:up)
klasses.each do |k|
k.constantize.public_send('migrate', :up)
end
end
desc "Clear the database"
task :drop => 'db:connect' do
# ActiveRecord::Migration.migrate(:down)
CreateCustomCommands.migrate(:down)
# CreateCustomCommands.migrate(:down)
klasses.each do |k|
k.constantize.public_send('migrate', :down)
end
end
end
namespace :dev do
require 'yard'
YARD::Rake::YardocTask.new
end

View file

@ -0,0 +1,15 @@
class CreateGeneral < ActiveRecord::Migration[6.1]
def up
create_table :general do |table|
table.string :roomid
table.string :protocol
table.string :prefix_char
table.integer :permission_level
table.timestamps
end
end
def down
drop_table :general
end
end

View file

@ -1,4 +1,4 @@
class CreateCustomCommands < ActiveRecord::Migration[5.2]
class CreateCustomCommands < ActiveRecord::Migration[6.1]
def up
create_table :custom_commands do |table|
table.string :roomid

19
wiki/addons.md Normal file
View file

@ -0,0 +1,19 @@
# @markup markdown
# @title Add-Ons
# Chronicle Add-ons
Add-ons represent functionality that may not be universally enabled or desired
by default. Provided examples include a dice roller, a custom command handler,
and some simple games.
Writing an add-on for Chronicle is relatively straight forward, and being
refined as I develop it more.
## Creating an Add-on
### General requirements
### Protocol requirements
### Database requirements

78
wiki/database.md Normal file
View file

@ -0,0 +1,78 @@
# @markup markdown
# @title Database
# Chronicle Database
Chronicle leverages a database for persistent storage. It uses ActiveRecord for
interfacing with the supported database types, performing migrations, and
interacting with models.
## Supported Databases
### Currently Supported:
- SQLite
### Planned Support:
- PostgreSQL
- MySQL
- MariaDB
## Creating the Database
There's a Rake task for creating the database according to ActiveRecord: `rake
db:create`. For configuration, check out `config/db.yml`. The default
configuration will create a SQLite3 database at `db/chronicle-bot.db`.
## Working With the Migrations
ActiveRecord migrations are database-agnostic schema alterations, stored as
code. This allows Chronicle to support multiple different database
architectures, without having to write custom code for each one. Migrations can
represent changes tied to new versions, or additional database configuration
tied to an add-on or protocol, for example.
To apply the migrations, use the Rake task `rake db:migrate`.
To roll-back the migrations, use the Rake task `rake db:down`.
### Provided Migrations
By default, Chronicle includes a migration for setting up the "general" table,
which provides details around the bot itself.
It also includes a migration for the Custom Commands add-on, which creates a
table for storing the commands and responses, along with the room ID the command
was created in.
Each of these migrations include a "roll-back" solution, which simply drops the
corresponding tables.
### Writing Additional Migrations
The most important thing when creating new migrations is to follow the naming
convention:
```
XXXX_name_of_migration.rb
```
The Rake tasks for working with migrations will look at every file in the
`db/migrate/` directory, and sort them. It will `require` those files, and then
compile a list of the classes within those files according to specific logic: it
takes the file name, drops the numbers at the beginning, and converts the file
name from snake case and to camel case, dropping the file extension. For example,
"0000_create_general.rb" will be processed to the class named "CreateGeneral",
which will be added to a list. The Rake task will then go through each the list
of all classes and run the "migrate" method of each class, passing it "up" if
migrations are being applied, or "down" if migrations are being rolled-back.
Generally speaking, the number for the migration is mostly important for sorting
order. However, a simple naming convention can help distinguish between types of
migrations, by changing the 2nd-position digit:
- For migrations tied to Chronicle functionality, use `00XX`.
- For migrations tied to addon functionality, use `01XX`.
- For migrations tied to protocol functionality, use `02XX`.
- For any miscellaneous migrations, use `03XX`.