[nap] refactoring and adding templates

This commit is contained in:
2021-05-23 22:35:10 -06:00
parent 0e4c21d209
commit 13464a4ee2
6 changed files with 51 additions and 22 deletions

View File

@@ -1,15 +1,28 @@
(defnew [&prop :String archiveDir
&prop :Array<System> systems]
(defnew [&prop :String archiveDir]
[:Map<String,Entry> entries
[:Array<System> systems
[]
:Map<String,Template> templates
(new Map)
:Map<String,Entry> entries
(let [entryFiles (FileSystem.readDirectory (Path.join [archiveDir "entries"]))]
(for file entryFiles =>(file.withoutExtension) ~(the Entry (Json.parse (File.getContent (Path.join [archiveDir "entries" file]))))))]
(for file entryFiles =>(file.withoutExtension) ~(the Entry (Json.parse (File.getContent (Path.join [archiveDir "entries" file]))))))])
(defmethod addSystem [:System system]
// Assign entries to the Systems that care about them
(doFor system systems
(doFor =>id entry entries
(when (system.canProcessEntry entry)
(system.entries.push entry)))))
(doFor =>id entry entries
(system.checkEntryInOrOut this entry))
(systems.push system))
(defmethod addTemplate [name template]
(dictSet templates name template))
(defmethod :Entry createEntry [template]
(let [e (_newEntry)]
(.prepareEntry (dictGet templates template) e)
(doFor system systems
(system.checkEntryInOrOut this e))
e))
(defun :Entry _newEntry []
(object

View File

@@ -2,5 +2,8 @@ package nap;
import kiss.Prelude;
typedef EntryChecker = (Archive, Entry) -> Bool;
typedef EntryProcessor = (Archive, Entry) -> Void;
@:build(kiss.Kiss.build())
class System {}

View File

@@ -1,10 +1,13 @@
(defprop :List<Entry> entries (new List))
(defprop :Map<String,Entry> entries (new Map))
(defmethod :Void process []
(entries.map processEntry))
(defmethod :Void process [:Archive archive]
(doFor e (entries.iterator) (processEntry archive e)))
(defmethod :Void processEntry [:Entry e]
(throw "can't process $e"))
(defnew [&prop :EntryChecker canProcessEntry
&prop :EntryProcessor processEntry]
[])
(defmethod :Bool canProcessEntry [:Entry e]
(throw "can't decide whether to process $e"))
(defmethod :Void checkEntryInOrOut [:Archive archive :Entry e]
(if (canProcessEntry archive e)
(dictSet entries e.id e)
(entries.remove e.id)))

View File

@@ -0,0 +1,5 @@
package nap;
interface Template {
function prepareEntry(e:Entry):Void;
}

View File

@@ -1,4 +1,9 @@
(defnew [&prop :String tagFilterString] [])
(load "../Lib.kiss")
(defmethod &override :Bool canProcessEntry [:Entry e] (print false))
// (Lib.evalBoolExp tagFilterString (getTags e)))
// TODO make a &super annotation that passes an argument to the super constructor
(defnew [&prop :String tagFilterString
&prop :EntryProcessor processor]
[]
(super
(lambda [:Archive archive :Entry e] (tagsMatch archive e tagFilterString))
processor))

View File

@@ -1,4 +1,6 @@
// TODO external programs need to be able to find and (load) this path to get the macros:
// ^ That should be solved by allowing an optional first argument to load that is a symbol
// of a library name that can be used to resolve the source dir in the user's Haxelib maybe?
(load "../nap/Lib.kiss")
@@ -10,10 +12,8 @@
(assert !(BoolExpInterp.eval "(and flag false)" ["flag"]))
(assert (BoolExpInterp.eval "(or flag otherFlag)" ["otherFlag"]))
//trace(error);
(let [systems
[]
archive
(new Archive "src/test/example-archive" systems)
(let [archive
(new Archive "src/test/example-archive")
song1
(dictGet archive.entries "song1")
song2