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

(c) 2011 Koding, Inc.

@class: bongo
@description: the main klass of the library.
@author: <NAME> <<EMAIL>>
###

{EventEmitter} = require 'events'

module.exports = class Bongo extends EventEmitter
  [READY, NOTREADY] = [0,1]
  # core
  url = require 'url'
  fs = require 'fs'
  nodePath = require 'path'
  # bongo = new EventEmitter
  # client-side boilerplate
  #@client = bongoClient = require 'bongo-client'
  # dnode           = require 'koding-dnode'
  # contrib
  @mongo          = require 'mongoskin'
  {extend}        = require 'underscore'
  @JsPath = JsPath = require 'jspath'
  @Inflector      = require 'inflector'
  # lib
  # the base klass of the library:
  @Base = Base    = require './base'
  # the model klass of the library:
  @Model = Model  = require './model'
  ModelRegistry   = require './modelregistry'
  @ModelCursor    = require './modelcursor'
  # the rest of the basics:
  @Subcollection  = require './subcollection'
  @ObjectId       = require './objectid'
  @ObjectRef      = require './objectref'
  @Validator      = require './validator'
  @Plugin         = require './plugin'
  @Register       = require './register'
  @util           = require './util'

  {daisy, @daisy, @dash, @sequence, @race, @future} = require 'sinkrow'

  {Scrubber, Store} = require 'koding-dnode-protocol'

  Traverse = require 'traverse'

  @secure =(fn)->
    fn.isSecurityEnabled = yes
    fn

  {@asynchronizeOwnMethods} = require './util'

  {BongoError}    = require './errortypes'

  Client = require './client'

  @throwIt =(it)-> throw it if it?

  constructor:(options)->
    {@mq, @mongo, @resourceName, @root, @fetchClient, models} = options
    @setClient @mongo
    @localStore = new Store
    @remoteStore = new Store
    @clients = {}
    @models = {}
    @stack = []
    @services = {}
    #@initBlacklist options.blacklist
    @loadModels models
    @on 'error', Bongo.throwIt
    @setMaxListeners 0

  initBlacklist:do ->
    blacklister = (k, v)->
      return  if k in @blacklist
      return v
    (@blacklist=[])->
      if blacklist.length
        @blacklisting = yes
        @replacer = blacklister.bind @

  defineModel:(name, model)->
    @models[name] = model
    return  unless model.addBongo? this
    @emit 'newModel', name, model
    model.on 'needsPopulated', (def, path)=>
      path = if Array.isArray path then path else path.split '.'
      names = JsPath.getAt def, path
      unless Array.isArray names
        names = [names]
        JsPath.setAt def, path, names
      names.forEach (name, i)=>
        if target = @models[name]
          JsPath.setAt def, path.concat(i), target
      @on 'newModel', (newName, newModel)->
        if newName in names
          JsPath.setAt def, path.concat(names.indexOf newName), newModel

  listModels=(root="", modelPath, acc={})->
    path = nodePath.join root, modelPath
    stat = fs.statSync path
    if stat.isDirectory()
      names = fs.readdirSync path
      for name in names
        listModels root, nodePath.join(modelPath, name), acc
    else if /(\.coffee|\.js)$/.test path
      acc[path] = NOTREADY
    return acc

  createModelRegistry:(modelPath)->
    acc = {}
    if Array.isArray modelPath
      listModels @root, item, acc  for item in modelPath
    else
      listModels @root, modelPath, acc
    new ModelRegistry acc

  loadModels:(modelPath)->
    @registry = @createModelRegistry(modelPath)
    @registry.forEach (path)=>
      try model = require path
      catch e then return @emit 'error', e
      if model.name? and ('function' is typeof model)
        @defineModel model.name, model
      else
        @defineModel name, konstructor  for own name, konstructor of model
      @registry.setState path, READY
    @emit 'ready'

  getInstanceRoutingKey =(inst, event)->  
    "oid.#{inst.getId()}.event.#{event}"

  getStaticRoutingKey =(konstructor, event)->
    "constructor.#{konstructor.name}.event.#{event}"

  handleEvent:(type, ctx, event, rest)->
    switch type
      when 'instance'
        @mq.emit 'updateInstances', getInstanceRoutingKey(ctx, event), (rest[0] or null), autoDelete: no
      when 'static'
        if event is 'global-listener-added'
          [actualEvent, listener] = rest
          #@mq.on 'event-'+ctx.name, actualEvent, listener]
        else if event is 'broadcast'
          [secretChannelId, message] = rest
          @fetchBrokerExchange (brokerExchange)->
            brokerExchange.publish secretChannelId, JSON.stringify message<commit_msg>bongo: bugfix: seems like we need to stringify the payload for updateInstances…<commit_after>###
Bongo.js
Unfancy models for MongoDB

(c) 2011 Koding, Inc.

@class: bongo
@description: the main klass of the library.
@author: <NAME> <<EMAIL>>
###

{EventEmitter} = require 'events'

module.exports = class Bongo extends EventEmitter
  [READY, NOTREADY] = [0,1]
  # core
  url = require 'url'
  fs = require 'fs'
  nodePath = require 'path'
  # bongo = new EventEmitter
  # client-side boilerplate
  #@client = bongoClient = require 'bongo-client'
  # dnode           = require 'koding-dnode'
  # contrib
  @mongo          = require 'mongoskin'
  {extend}        = require 'underscore'
  @JsPath = JsPath = require 'jspath'
  @Inflector      = require 'inflector'
  # lib
  # the base klass of the library:
  @Base = Base    = require './base'
  # the model klass of the library:
  @Model = Model  = require './model'
  ModelRegistry   = require './modelregistry'
  @ModelCursor    = require './modelcursor'
  # the rest of the basics:
  @Subcollection  = require './subcollection'
  @ObjectId       = require './objectid'
  @ObjectRef      = require './objectref'
  @Validator      = require './validator'
  @Plugin         = require './plugin'
  @Register       = require './register'
  @util           = require './util'

  {daisy, @daisy, @dash, @sequence, @race, @future} = require 'sinkrow'

  {Scrubber, Store} = require 'koding-dnode-protocol'

  Traverse = require 'traverse'

  @secure =(fn)->
    fn.isSecurityEnabled = yes
    fn

  {@asynchronizeOwnMethods} = require './util'

  {BongoError}    = require './errortypes'

  Client = require './client'

  @throwIt =(it)-> throw it if it?

  constructor:(options)->
    {@mq, @mongo, @resourceName, @root, @fetchClient, models} = options
    @setClient @mongo
    @localStore = new Store
    @remoteStore = new Store
    @clients = {}
    @models = {}
    @stack = []
    @services = {}
    #@initBlacklist options.blacklist
    @loadModels models
    @on 'error', Bongo.throwIt
    @setMaxListeners 0

  initBlacklist:do ->
    blacklister = (k, v)->
      return  if k in @blacklist
      return v
    (@blacklist=[])->
      if blacklist.length
        @blacklisting = yes
        @replacer = blacklister.bind @

  defineModel:(name, model)->
    @models[name] = model
    return  unless model.addBongo? this
    @emit 'newModel', name, model
    model.on 'needsPopulated', (def, path)=>
      path = if Array.isArray path then path else path.split '.'
      names = JsPath.getAt def, path
      unless Array.isArray names
        names = [names]
        JsPath.setAt def, path, names
      names.forEach (name, i)=>
        if target = @models[name]
          JsPath.setAt def, path.concat(i), target
      @on 'newModel', (newName, newModel)->
        if newName in names
          JsPath.setAt def, path.concat(names.indexOf newName), newModel

  listModels=(root="", modelPath, acc={})->
    path = nodePath.join root, modelPath
    stat = fs.statSync path
    if stat.isDirectory()
      names = fs.readdirSync path
      for name in names
        listModels root, nodePath.join(modelPath, name), acc
    else if /(\.coffee|\.js)$/.test path
      acc[path] = NOTREADY
    return acc

  createModelRegistry:(modelPath)->
    acc = {}
    if Array.isArray modelPath
      listModels @root, item, acc  for item in modelPath
    else
      listModels @root, modelPath, acc
    new ModelRegistry acc

  loadModels:(modelPath)->
    @registry = @createModelRegistry(modelPath)
    @registry.forEach (path)=>
      try model = require path
      catch e then return @emit 'error', e
      if model.name? and ('function' is typeof model)
        @defineModel model.name, model
      else
        @defineModel name, konstructor  for own name, konstructor of model
      @registry.setState path, READY
    @emit 'ready'

  getInstanceRoutingKey =(inst, event)->  
    "oid.#{inst.getId()}.event.#{event}"

  getStaticRoutingKey =(konstructor, event)->
    "constructor.#{konstructor.name}.event.#{event}"

  handleEvent:(type, ctx, event, rest)->
    switch type
      when 'instance'
        payload     = JSON.stringify rest[0] or null
        routingKey  = getInstanceRoutingKey(ctx, event)
        @mq.emit 'updateInstances', routingKey, payload, autoDelete: no
      when 'static'
        if event is 'global-listener-added'
          [actualEvent, listener] = rest
          #@mq.on 'event-'+ctx.name, actualEvent, listener]
        else if event is 'broadcast'
          [secretChannelId, message] = rest
          @fetchBrokerExchange (brokerExchange)->
            brokerExchange.publish secretChannelId, JSON.stringify message
{'fraction_non_alphanumeric': 0.12262110726643599, 'fraction_numerical': 0.0017301038062283738, 'mean_word_length': 2.724929520741039, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, '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': '3388944', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2855, 'n_words': 835}