<commit_before>JProduct = require '../product'

{Module} = require 'jraphical'

module.exports = class JBundle extends JProduct

  {dash} = require 'bongo'

  # @create = ()

  @setLimits = (limits) ->
    JLimit = require '../limit'

    relationships =
      limit         :
        targetType  : JLimit
        as          : (limitName for own limitName of limits)

    @setRelationships relationships

    # prevent subclasses from trying to set relationships + limits:
    @setRelationships = ->
      throw new Error "You can't set relationships on a bundle class"

    normalizedLimits = {}
    for own limitName, limit of limits
      normalizedLimits[limitName] =
        if 'string' is typeof limit then { unit: limit }
        else limit

    @limits_ = normalizedLimits

  constructor: (data, limits) ->

    super data

    if limits?

      { limits_: defaultLimits } = @constructor

      @once 'save', =>
        console.log 'does this happen?'

        queue = Object.keys(defaultLimits).map (limitName) => =>
          limitOptions = limits[limitName]
          limitOptions = { unit: limit }  if 'string' is typeof limitOptions
          limit = new Limit limitOptions
          limit.save (err)=>
            return next err  if err
            @addLimit limit, limitName, next

        dash queue, => @emit 'limitsAreSet'

        next = queue.next.bind queue

        return<commit_msg>bundle: fix initialization logic<commit_after>JProduct = require '../product'

{Module} = require 'jraphical'

module.exports = class JBundle extends JProduct

  {dash} = require 'bongo'

  {extend} = require 'underscore'
  # @create = ()

  @setLimits = (limits) ->
    JLimit = require '../limit'

    relationships =
      limit         :
        targetType  : JLimit
        as          : (limitName for own limitName of limits)

    @setRelationships relationships

    # prevent subclasses from trying to set relationships + limits:
    @setRelationships = ->
      throw new Error "You can't set relationships on a bundle class"

    normalizedLimits = {}
    for own limitName, limit of limits
      normalizedLimits[limitName] =
        if 'string' is typeof limit then { unit: limit }
        else limit

    @limits_ = normalizedLimits

  constructor: (data, limits) ->

    super data

    if limits?

      { limits_: defaultLimits } = @constructor

      @once 'save', =>
        JLimit = require '../limit'

        queue = Object.keys(defaultLimits).map (limitName) => =>
          defaultOptions = defaultLimits[limitName]

          limitOptions = limits[limitName]

          limitOptions = { unit: limit }  if 'string' is typeof limitOptions

          limitOptions = extend { title: limitName }, defaultOptions, limitOptions

          limit = new JLimit limitOptions
          limit.save (err)=>
            return next err  if err
            @addLimit limit, limitName, fin

        dash queue, => @emit 'limitsAreSet'

        fin = queue.fin.bind queue

        return
{'fraction_non_alphanumeric': 0.09160559626915389, 'fraction_numerical': 0.0, 'mean_word_length': 2.2927631578947367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4622298', 'n_tokens_mistral': 829, 'n_tokens_neox': 786, 'n_words': 262}