<commit_before>define ['cuffs/utils'], (utils) ->

    class Context
        constructor: (obj)->
            @__parent__ = null
            @__children__ = []
            @__observers__ = {}
            for key, value of obj
                this[key] = value

        new: (obj)->
            # Create a new child context
            Context = ()->
            Context.prototype = this

            child = new Context

            this.__children__.push child

            child.__parent__ = this
            child.__children__ = []
            child.__observers__ = {}

            for key, value of obj
                child[key] = value

            child

        destroy: ()->
            # Destroy this context, all its links and children.
            if this.__parent__?
                this.__parent__.__children__.splice(
                    this.__parent__.__children__.indexOf(this),
                    1
                )
            this.__observers__ = {}
            this.__children__[0].destroy() while this.__children__.length > 0
            this.__parent__ = null
            this

        watch: (attr, observer)->
            # Watch a property on this context
            if this.__observers__[attr]?
                this.__observers__[attr].push observer
            else
                this.__observers__[attr] = [observer]
            this

        apply: (attr)->
            # Call all observers and propagate down to children. If `attr`
            # is specified only call observers of `attr`.
            if not attr?
                for attr, observers of this.__observers__
                    value = @get attr
                    observer value for observer in observers
                for child in this.__children__
                    child.apply()
            else
                value = @get attr
                observer value for observer in this.__observers__[attr] or []
                child.apply attr for child in this.__children__
            this

        parent: ()->
            this.__parent__

        root: ()->
            if this.__parent__?
                return this.__parent__.root()
            return this

        get: (name, call = true)->
            # Pull the object referenced by `name` from the context. Allow
            # for dotted notation.
            # Examples:
            #
            # `name` = 'todo' => @['todo']
            # `name` = 'todo.task' => @['todo']['task']
            # `name` = 'todo.task.priority' => @['todo']['task']
            #
            # If the value is a function, it is called and its return value
            # returned if `call` is `true`, else the function is returned.

            parts = (part.trim() for part in name.split '.')

            returnFunc = (value)->
                if utils.typeOf(value) == "function" and call == true
                    return value()
                return value

            if parts.length == 1
                return returnFunc @[name]
            else
                current = this
                for part in parts
                    if not current?
                        return null
                    current = current[part]
                return returnFunc current

        hasProp: (name)->
            {}.hasOwnProperty.call this, name

        set: (name, value)->
            # Set the object referenced by `name` on the context. Allow
            # for dotted notation.
            #
            # Examples:
            # `name` = 'todo' => @['todo'] = value
            # `name` = 'todo.task' => @['todo']['task'] = value
            # `name` = 'todo.task.priority' => @['todo']['task']['priority'] = value
            #
            # If the nested object does not exist yet, it's created on the fly.
            #
            # If `value` is not a function, all observers will be notified.

            parts = (part.trim() for part in name.split '.')

            if parts.length == 1
                @[name] = value
            else
                current = this
                for part in parts[0..parts.length-2]
                    current = current[part] or current[part] = {}
                current[parts[parts.length-1]] = value

            # Don't notify observers. Apply calls `get()` which would run
            # the function without possibly expected arguments.
            if utils.typeOf(value) == "function"
                return

            if @hasProp parts[0]
                return @apply name
            ctx = this
            while ctx = ctx.parent()
                continue if not ctx.hasProp parts[0]
                return ctx.apply name


    return {
        Context: Context<commit_msg>Modified Context::set optionally silent<commit_after>define ['cuffs/utils'], (utils) ->

    class Context
        constructor: (obj)->
            @__parent__ = null
            @__children__ = []
            @__observers__ = {}
            for key, value of obj
                this[key] = value

        new: (obj)->
            # Create a new child context
            Context = ()->
            Context.prototype = this

            child = new Context

            this.__children__.push child

            child.__parent__ = this
            child.__children__ = []
            child.__observers__ = {}

            for key, value of obj
                child[key] = value

            child

        destroy: ()->
            # Destroy this context, all its links and children.
            if this.__parent__?
                this.__parent__.__children__.splice(
                    this.__parent__.__children__.indexOf(this),
                    1
                )
            this.__observers__ = {}
            this.__children__[0].destroy() while this.__children__.length > 0
            this.__parent__ = null
            this

        watch: (attr, observer)->
            # Watch a property on this context
            if this.__observers__[attr]?
                this.__observers__[attr].push observer
            else
                this.__observers__[attr] = [observer]
            this

        apply: (attr)->
            # Call all observers and propagate down to children. If `attr`
            # is specified only call observers of `attr`.
            if not attr?
                for attr, observers of this.__observers__
                    value = @get attr
                    observer value for observer in observers
                for child in this.__children__
                    child.apply()
            else
                value = @get attr
                observer value for observer in this.__observers__[attr] or []
                child.apply attr for child in this.__children__
            this

        parent: ()->
            this.__parent__

        root: ()->
            if this.__parent__?
                return this.__parent__.root()
            return this

        get: (name, call = true)->
            # Pull the object referenced by `name` from the context. Allow
            # for dotted notation.
            # Examples:
            #
            # `name` = 'todo' => @['todo']
            # `name` = 'todo.task' => @['todo']['task']
            # `name` = 'todo.task.priority' => @['todo']['task']
            #
            # If the value is a function, it is called and its return value
            # returned if `call` is `true`, else the function is returned.

            parts = (part.trim() for part in name.split '.')

            returnFunc = (value)->
                if utils.typeOf(value) == "function" and call == true
                    return value()
                return value

            if parts.length == 1
                return returnFunc @[name]
            else
                current = this
                for part in parts
                    if not current?
                        return null
                    current = current[part]
                return returnFunc current

        hasProp: (name)->
            {}.hasOwnProperty.call this, name

        set: (name, value, doApply = true)->
            # Set the object referenced by `name` on the context. Allow
            # for dotted notation.
            #
            # Examples:
            # `name` = 'todo' => @['todo'] = value
            # `name` = 'todo.task' => @['todo']['task'] = value
            # `name` = 'todo.task.priority' => @['todo']['task']['priority'] = value
            #
            # If the nested object does not exist yet, it's created on the fly.
            #
            # If `value` is not a function, all observers will be notified.

            parts = (part.trim() for part in name.split '.')

            if parts.length == 1
                @[name] = value
            else
                current = this
                for part in parts[0..parts.length-2]
                    current = current[part] or current[part] = {}
                current[parts[parts.length-1]] = value

            # Don't notify observers. Apply calls `get()` which would run
            # the function without possibly expected arguments.
            if utils.typeOf(value) == "function"
                return

            # Don't notify observers if we've received a `doApply==false`
            if not doApply
                return

            if @hasProp parts[0]
                return @apply name
            ctx = this
            while ctx = ctx.parent()
                continue if not ctx.hasProp parts[0]
                return ctx.apply name


    return {
        Context: Context
{'fraction_non_alphanumeric': 0.09381863821775965, 'fraction_numerical': 0.00209183139838929, 'mean_word_length': 1.2557206888417078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '7357868', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2239, 'n_words': 748}