<commit_before>###
Bongo.js
Unfancy models for MongoDB

@class: Base
@author: <NAME>
###
{EventEmitter2} = require 'koding-eventemitter2'
nodePath = require 'path'

module.exports = class Base extends EventEmitter2
  # native
  {defineProperty} = Object
  # contrib
  createId = require('hat').rack()
  Inflector = require 'inflector'
  ###
  Static events
  @description: see ./staticemitterdelegate.coffee
  ###
  {@on, @off, @addListener, @removeListener, @removeAllListeners, @once
   @listeners, @setMaxListeners, @emit, @getEmitter} = require './staticemitterdelegate'

  # oldEmit = @emit
  # @emit =(rest...)->
  #   if @::mq?
  #     [event, data] = rest
  #     broadcastable = @getBroadcastable()
  #     if broadcastable
  #       if Array.isArray event
  #         event = event.join '.'
  #       @::mq?.trigger @name, event, data
  #   else
  #     oldEmit.apply @, rest

  ###
  @method: property of the constructor
  @signature: Base.mixin(stuff)
  @description: copy all the static and prototype members.
  ###
  mixin: @mixin = (source)->
    @[key] = val for own key,val of source when key isnt 'constructor'
  
  @trait =(root, trait)->
    if 'function' is typeof root
      @mixin root
      @::mixin root.prototype if root.prototype?
    else
      [trait, root] = [root, trait] unless trait
      root ?= ''
      err = null
      traitPath = nodePath.join(root, trait)
      traitModule =\
        try require traitPath
        catch e then err = e
      if 'function' is typeof traitModule
        @trait traitModule
      else
        throw new Error "Couldn't load the trait #{traitPath} #{err}"

  @addGlobalListener = (event, listener) ->
    @emit "global-listener-added", event, listener

  getInterfaceName: @getInterfaceName = (verb, noun, isPlural)->
    noun = Inflector.pluralize noun if isPlural
    name = Inflector.camelize "#{verb}_#{noun}", yes, yes
  
  invoke: @invoke = (verb, noun, rest...)->
    (@[@getInterfaceName(verb, noun)]?(rest...) and true) or false
  ###
  @method.
  @signatature: initializeSharedInstance()
  ###
  initializeSharedInstance:(options)->
    model = @
    if model.constructor.isShared
      if options.bongo_?
        {instanceId} = options.bongo_
        model.bongo_ = options.bongo_
      else
        instanceId = createId()
        model.bongo_ = {
          constructorName : model.constructor.name          
          instanceId
        }
      
  @wildcard = yes
  @maxListeners = 0
  
  warnAboutEventWhitelist =(konstructor, context, event)->
    console.warn """
      No event whitelist in place for #{konstructor.name}!
      Emitting unguarded #{context} event: #{event}
      This needs to be fixed; we will stop supporting non-whitelisted events
      at some point in the near future.
      """

  constructor:(options={})->
    @initializeSharedInstance options
    defineProperty this, '_events', value: {}, writeable: yes
    defineProperty this, 'wildcard', value: yes, writeable: yes
    defineProperty this, 'maxListeners', value: 0, writeable: yes
    defineProperty this, 'listenerTree', value: {}, writeable: yes
    super options
    @on '*', (rest...)=>
      @constructor.bongos.forEach (bongo)=>
        if !@constructor.instanceEvents_?
          warnAboutEventWhitelist @constructor, 'instance', @event
          bongo.handleEvent 'instance', this, @event, rest
        else if @event in @constructor.instanceEvents_
          # TODO: this is insecure by default :(
          {filter} = @constructor.sharedEvents?.instance[@event] or {}
          payload = filter.call this, rest[0]  if 'function' is typeof filter
          bongo.handleEvent 'instance', this, @event, [payload]

  addGlobalListener: (event, listener) ->
    @emit "global-listener-added", event, listener
    
  ###
  @property.
  @signature: Base.constructors
  @description - an array of all constructors (that have schema).
  ###
  defineProperty(@, 'constructors', value: {})<commit_msg>bongo: bugfix: event payload was null if filter was not a function; it should default to the unfiltered value.<commit_after>###
Bongo.js
Unfancy models for MongoDB

@class: Base
@author: <NAME>
###
{EventEmitter2} = require 'koding-eventemitter2'
nodePath = require 'path'

module.exports = class Base extends EventEmitter2
  # native
  {defineProperty} = Object
  # contrib
  createId = require('hat').rack()
  Inflector = require 'inflector'
  ###
  Static events
  @description: see ./staticemitterdelegate.coffee
  ###
  {@on, @off, @addListener, @removeListener, @removeAllListeners, @once
   @listeners, @setMaxListeners, @emit, @getEmitter} = require './staticemitterdelegate'

  # oldEmit = @emit
  # @emit =(rest...)->
  #   if @::mq?
  #     [event, data] = rest
  #     broadcastable = @getBroadcastable()
  #     if broadcastable
  #       if Array.isArray event
  #         event = event.join '.'
  #       @::mq?.trigger @name, event, data
  #   else
  #     oldEmit.apply @, rest

  ###
  @method: property of the constructor
  @signature: Base.mixin(stuff)
  @description: copy all the static and prototype members.
  ###
  mixin: @mixin = (source)->
    @[key] = val for own key,val of source when key isnt 'constructor'
  
  @trait =(root, trait)->
    if 'function' is typeof root
      @mixin root
      @::mixin root.prototype if root.prototype?
    else
      [trait, root] = [root, trait] unless trait
      root ?= ''
      err = null
      traitPath = nodePath.join(root, trait)
      traitModule =\
        try require traitPath
        catch e then err = e
      if 'function' is typeof traitModule
        @trait traitModule
      else
        throw new Error "Couldn't load the trait #{traitPath} #{err}"

  @addGlobalListener = (event, listener) ->
    @emit "global-listener-added", event, listener

  getInterfaceName: @getInterfaceName = (verb, noun, isPlural)->
    noun = Inflector.pluralize noun if isPlural
    name = Inflector.camelize "#{verb}_#{noun}", yes, yes
  
  invoke: @invoke = (verb, noun, rest...)->
    (@[@getInterfaceName(verb, noun)]?(rest...) and true) or false
  ###
  @method.
  @signatature: initializeSharedInstance()
  ###
  initializeSharedInstance:(options)->
    model = @
    if model.constructor.isShared
      if options.bongo_?
        {instanceId} = options.bongo_
        model.bongo_ = options.bongo_
      else
        instanceId = createId()
        model.bongo_ = {
          constructorName : model.constructor.name          
          instanceId
        }
      
  @wildcard = yes
  @maxListeners = 0
  
  warnAboutEventWhitelist =(konstructor, context, event)->
    console.warn """
      No event whitelist in place for #{konstructor.name}!
      Emitting unguarded #{context} event: #{event}
      This needs to be fixed; we will stop supporting non-whitelisted events
      at some point in the near future.
      """

  constructor:(options={})->
    @initializeSharedInstance options
    defineProperty this, '_events', value: {}, writeable: yes
    defineProperty this, 'wildcard', value: yes, writeable: yes
    defineProperty this, 'maxListeners', value: 0, writeable: yes
    defineProperty this, 'listenerTree', value: {}, writeable: yes
    super options
    @on '*', (rest...)=>
      @constructor.bongos.forEach (bongo)=>
        if !@constructor.instanceEvents_?
          warnAboutEventWhitelist @constructor, 'instance', @event
          bongo.handleEvent 'instance', this, @event, rest
        else if @event in @constructor.instanceEvents_
          # TODO: this is insecure by default :(
          {filter} = @constructor.sharedEvents?.instance[@event] or {}
          payload =
            if 'function' is typeof filter then filter.call this, rest[0]
            else rest[0]
          bongo.handleEvent 'instance', this, @event, [payload]

  addGlobalListener: (event, listener) ->
    @emit "global-listener-added", event, listener
    
  ###
  @property.
  @signature: Base.constructors
  @description - an array of all constructors (that have schema).
  ###
  defineProperty(@, 'constructors', value: {})
{'fraction_non_alphanumeric': 0.12366790582403965, 'fraction_numerical': 0.00161090458488228, 'mean_word_length': 2.9103682170542635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '6030546', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2399, 'n_words': 705}