From 63bc54d966831929eb66da8e06291fd924fb4aef Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Fri, 15 Jul 2016 18:07:41 -0400
Subject: use YAML.mapping for init data

---
 src/guff.cr | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

(limited to 'src')

diff --git a/src/guff.cr b/src/guff.cr
index 1e536cf..8ea29fa 100644
--- a/src/guff.cr
+++ b/src/guff.cr
@@ -2711,14 +2711,23 @@ module Guff
       end
 
       class InitAction < Action
+        class Data
+          YAML.mapping({
+            init_sql:   Array(String),
+            add_user:   String,
+            test_posts: Array(String),
+          })
+
+          def self.load(system_dir : String) : Data
+            self.from_yaml(File.read(File.join(system_dir, "init.yaml")))
+          end
+        end
+
         def initialize(config : Config)
           super(config)
 
           # read init data
-          @data = YAML.parse(File.read(File.join(
-            @config.system_dir,
-            "init.yaml"
-          )))
+          @data = Data.load(@config.system_dir)
         end
 
         def run
@@ -2726,8 +2735,8 @@ module Guff
           Dir.mkdir(@config.data_dir) unless Dir.exists?(@config.data_dir)
 
           Guff::Database.new(@config.db_path) do |db|
-            @data["sql"]["init"].each do |sql|
-              db.query(sql.as_s)
+            @data.init_sql.each do |sql|
+              db.query(sql)
             end
 
             # gen random password and add admin user
@@ -2747,7 +2756,7 @@ module Guff
           email     : String,
           password  : String
         ) : Int64
-          db.query(@data["sql"]["add_user"].as_s, [
+          db.query(@data.add_user, [
             name,
             email,
             Password.create(password),
@@ -2758,8 +2767,8 @@ module Guff
 
         private def add_test_posts(db)
           # STDERR.puts "DEBUG: adding test data"
-          @data["sql"]["test_posts"].each do |sql|
-            db.query(sql.as_s)
+          @data.test_posts.each do |sql|
+            db.query(sql)
           end
         end
       end
-- 
cgit v1.2.3