<commit_before>FS   = require('fs')
Path = require('path')

module.exports = Model = (schema, opts) ->
    {persist, folder, filename} = opts if opts?
    if !persist then persist = true
    if persist
        if !folder then throw new Error("Must supply a folder")
        if !filename then throw new Error("Must supply a filename")
    
    class Model
        # seed is a JSON.parse'd object loaded from disk.
        constructor : (seed) ->
            @_persistentProperties = []
            info = null
            ctor = null

            for key, val of schema
                if typeof val == 'function'
                    ctor = val
                else
                    # options object style.
                    ctor = val['type']
                    persist = val['persist']
                if seed?[key]
                    this[key] = new ctor(seed[key])
                else
                    this[key] = new ctor()
                if persist
                    @_persistentProperties.push(key)
                    if this[key].subscribe?
                        this[key].subscribe (val) =>
                            # Save to disk every time a property changes.
                            @persist()

        _toJSON : () ->
            obj = {}
            @_persistentProperties.forEach (propName) =>
                prop = this[propName]
                if prop.subscribe?
                    obj[propName] = prop()
                else
                    obj[propName] = prop
            return obj

        _dbPath : () ->
            file = this[filename]
            if typeof file == 'function'
                file = file()
            return Path.resolve(folder, file)

        # TODO: rename to "save"
        persist : (callback) ->
            json = JSON.stringify(@_toJSON())
            FS.writeFile @_dbPath(), json, (err) ->
                if err
                    console.log(err)
                    console.log(err.stack)
                    throw new Error("Couldn't persist model: " + json)
                if callback? then callback()

        destroy : (callback) ->
            FS.unlink @_dbPath(), (err) ->
                if err
                    console.log(err)
                    console.log(err.stack)
                    throw new Error("Couldn't destroy model.")
                if callback? then callback()
<commit_msg>Fixed issue where models wouldn't persist after the 1st save.<commit_after>FS   = require('fs')
Path = require('path')

module.exports = (schema, opts) ->
    {persist, folder, filename} = opts if opts?
    if !persist then persist = true
    if persist
        if !folder then throw new Error("Must supply a folder")
        if !filename then throw new Error("Must supply a filename")
    
    class Model
        # seed is a JSON.parse'd object loaded from disk.
        constructor : (seed) ->
            console.log("Seed:")
            console.log(seed)
            @_persistentProperties = []
            info = null
            ctor = null

            for key, val of schema
                if typeof val == 'function'
                    ctor = val
                    persistObj = persist
                else
                    # options object style.
                    ctor = val['type']
                    persistObj = val['persist']
                if seed?[key]
                    this[key] = new ctor(seed[key])
                else
                    this[key] = new ctor()
                if persistObj
                    @_persistentProperties.push(key)
                    if this[key].subscribe?
                        this[key].subscribe (val) =>
                            # Save to disk every time a property changes.
                            @persist()

        _toJSON : () ->
            obj = {}
            @_persistentProperties.forEach (propName) =>
                prop = this[propName]
                if prop.subscribe?
                    obj[propName] = prop()
                else
                    obj[propName] = prop
            return obj

        _dbPath : () ->
            file = this[filename]
            if typeof file == 'function'
                file = file()
            return Path.resolve(folder, file)

        # TODO: rename to "save"
        persist : (callback) ->
            json = JSON.stringify(@_toJSON())
            console.log("Persisting: #{json}")
            FS.writeFile @_dbPath(), json, (err) ->
                if err
                    console.log(err)
                    console.log(err.stack)
                    throw new Error("Couldn't persist model: " + json)
                if callback? then callback()

        destroy : (callback) ->
            FS.unlink @_dbPath(), (err) ->
                if err
                    console.log(err)
                    console.log(err.stack)
                    throw new Error("Couldn't destroy model.")
                if callback? then callback()

{'fraction_non_alphanumeric': 0.0947981522394055, 'fraction_numerical': 0.0002008435428800964, 'mean_word_length': 1.1642764015645373, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6052162', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1162, 'n_words': 356}