<commit_before># ------------------------------------------------------------------ #
# The ModelChangeMonitor watches for changes on the                  #
# Model and remembers which attributes have been changed             #
# ------------------------------------------------------------------ #

class Lanes.Models.ChangeMonitor
    constructor: (model) ->
        model.on('change', this.onChange, this)
        @_unsaved = {}

    onChange: (record, options) ->
        attrs = record.changedAttributes()
        return if _.isEmpty(attrs)
        if attrs[record.idAttribute] and record.associations
            record.associations.onIdChange(record)
        if attrs.isDirty == false
            @_unsaved = {}
        else
            this.recordChanges(record, _.keys(attrs))


    recordChanges: (record, names) ->
        # console.log "Change: #{names}"
        # console.log record.getAttributes(props:true, session: true)
        for name in names
            if name != record.idAttribute && record._definition[name] && !record._definition[name].session
                record.isDirty = true
                @_unsaved[ name ] = true

    changedAttributes: ->
        _.keys(@_unsaved)

    isDirty: -><commit_msg>reset() method to clear changes<commit_after># ------------------------------------------------------------------ #
# The ModelChangeMonitor watches for changes on the                  #
# Model and remembers which attributes have been changed             #
# ------------------------------------------------------------------ #

class Lanes.Models.ChangeMonitor
    constructor: (model) ->
        model.on('change', this.onChange, this)
        @_unsaved = {}

    onChange: (record, options) ->
        attrs = record.changedAttributes()
        return if _.isEmpty(attrs)
        if attrs[record.idAttribute] and record.associations
            record.associations.onIdChange(record)
        if attrs.isDirty == false
            @_unsaved = {}
        else
            this.recordChanges(record, _.keys(attrs))

    reset: ->
        @_unsaved = {}

    recordChanges: (record, names) ->
        # console.log "Change: #{names}"
        # console.log record.getAttributes(props:true, session: true)
        for name in names
            if name != record.idAttribute && record._definition[name] && !record._definition[name].session
                record.isDirty = true
                @_unsaved[ name ] = true

    changedAttributes: ->
        _.keys(@_unsaved)

    isDirty: ->
{'fraction_non_alphanumeric': 0.20238568588469186, 'fraction_numerical': 0.0, 'mean_word_length': 1.5595116988809765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2479829', 'n_tokens_mistral': 611, 'n_tokens_neox': 598, 'n_words': 148}