<commit_before><commit_msg>Collection interface with Enumerable methods<commit_after>



class Sirisus.Collection

  #klass Model klass, must be extend BaseModel
  constructor: (klass, options = {every: 0, on_add: @on_add, on_remove: @on_remove, remote: ->}) ->
    throw new Error("Collection must be used only with `BaseModel` inheritor") if klass.__super__.constructor.name isnt 'BaseModel'
    @_array = []
    @_type  = klass.name
    @on_add    = options.on_add || @on_add
    @on_remove = options.on_remove || @on_add
    @remove    = options.remove
    _start_sync(every)

  @_start_sync: (every) ->
    #timeout and push when every != 0

  #check if is instance of BaseModel
  push: (model) ->
    type = model.constructor.name
    throw new Error("Require `#{@_type}`, but given `#{model.constructor.name}`") if @_type isnt type
    @_array.push(model) #maybe it's a hash ? because hash have a keys, and simple remove, but need a unique id
    @on_add(model)

  add: (model) ->
    @push(model)

  #remove

  find: (key, value) ->
    @findAll(key, value)[0] || null

  findAll: (key, value) ->
    for model in @_array when model.get(key) == value then model

  filter: (fn = ->) ->
    for model in @_array when fn.call(null, model) then model

  each: (fn = ->) ->
    for model in @_array then fn.call(model)

  first: () ->
    @_array[0]

  last: () ->
    @_array[@_array.length - 1]

  all: () ->
    @_array

  on_remove: (model) ->

  on_add: (model) ->


{'fraction_non_alphanumeric': 0.14101694915254237, 'fraction_numerical': 0.003389830508474576, 'mean_word_length': 3.122905027932961, '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': '881746', 'n_tokens_mistral': 517, 'n_tokens_neox': 502, 'n_words': 150}