<commit_before>#require ST/Object
#require ST/Enumerable
#require ST/Inflector

ST.module 'Model', ->
  @class 'Base', ->
    @classMethod 'fetch', (uuid, yield) ->
      self = this
    
      if ST.Model._byUuid[uuid]
        yield ST.Model._byUuid[uuid] if yield
      else if @FETCH_URL
        $.ajax {
          url:      @FETCH_URL.replace('?', uuid)
          type:     'get'
          data:     @FETCH_DATA || {}
          success:  (data) ->
            model = self.createWithData data, {loaded: true}
            yield model if yield
        }
      else
        ST.error "No find URL for model: #{@_name}"
  
    @classDelegate 'where', 'scoped'
    @classDelegate 'order', 'scoped'
    @classDelegate 'each',  'scoped'
    @classDelegate 'first', 'scoped'
    
    @classMethod 'scoped', ->
      ST.Model.Scope.createWithModel this
  
    @classMethod 'find', (uuid) ->
      ST.Model._byUuid[uuid]
  
    @classMethod 'load', (data) ->
      self = this
      if data instanceof Array
        for row in data
          @load row
      else
        return unless data && data.uuid
        return if ST.Model._byUuid[data.uuid]
        @createWithData data
    
    @classMethod 'master', ->
      @_master ||= ST.List.create()
    
    @classMethod 'index', (attribute) ->
      @_indexes ||= {}
      @_indexes[attribute] ||= ST.Model.Index.createWithModelAttribute(this, attribute)
  
    @initializer (options={}) ->
      @initWithData {}, options
      this
  
    # Initializes a new model, and loads the supplied attribute data, if any
    @initializer 'withData', (data, options={}) ->
      self = this
    
      ST.Object.prototype.init.call this

      @uuid data.uuid || ST.Model._generateUUID()
      @_creating = true
      @_attributes = {}
      for attribute, details of @_class._attributes
        if @_class._attributes.hasOwnProperty(attribute)
          if data[attribute]?
            @[attribute] data[attribute]
          else if !details.virtual
            @[attribute] details.default
      
      # Add class to indexes
      if @_class._indexes
        for attribute of @_class._attributes
          if @_class._attributes.hasOwnProperty(attribute)
            if index = @_class._indexes[attribute]
              index.add @_attributes[attribute], this 
    
      if @_class._manyBinds
        for binding in @_class._manyBinds
          @get(binding.assoc).bind binding.from, self, binding.to
      
      unless options.loaded
        ST.Model.recordChange 'create', @_uuid, @_class._name, @data()

      @_creating = false
      @_class.master().add this
  
    # Creates a new object from model data. If the data includes a 
    # property, as with data genereated by #objectify, the specified model
    # will be used instead of the model createWithData was called on.
    @classMethod 'createWithData', (data, options={}) ->
      # If data is being sent to the wrong model, transfer to correct model
      if data.model && data.model != @_name
        if modelClass = @_namespace.class data.model
          modelClass.createWithData data, options
        else
          null
      # If object with uuid already exists, update object and return it
      else if data.uuid && ST.Model._byUuid[data.uuid]
        object = ST.Model._byUuid[data.uuid]
        attributes = object._class._attributes
        for attribute, details of attributes
          if attributes.hasOwnProperty attribute
            object.set attribute, data[attribute] if data[attribute]?
        object
      # Otherwise, create a new object
      else
        object = new this
        object.initWithData data, options
        object
  
    @property 'uuid'
  
    @method 'setUuid', (newUuid) ->
      unless @_uuid
        newUuid = String newUuid
        
        # Insert object in global index
        ST.Model._byUuid[newUuid] = this
    
        # Insert object in model-specific index
        @_class._byUuid ||= {}
        @_class._byUuid[newUuid] = this
    
        @_uuid = newUuid
  
    @method 'matches', (conditions) ->
      if @_attributes
        for condition in conditions
          return false unless condition.test this<commit_msg>Removed buggy Model/Base.createWithData update functionality for existing models.<commit_after>#require ST/Object
#require ST/Enumerable
#require ST/Inflector

ST.module 'Model', ->
  @class 'Base', ->
    @classMethod 'fetch', (uuid, yield) ->
      self = this
    
      if ST.Model._byUuid[uuid]
        yield ST.Model._byUuid[uuid] if yield
      else if @FETCH_URL
        $.ajax {
          url:      @FETCH_URL.replace('?', uuid)
          type:     'get'
          data:     @FETCH_DATA || {}
          success:  (data) ->
            model = self.createWithData data, {loaded: true}
            yield model if yield
        }
      else
        ST.error "No find URL for model: #{@_name}"
  
    @classDelegate 'where', 'scoped'
    @classDelegate 'order', 'scoped'
    @classDelegate 'each',  'scoped'
    @classDelegate 'first', 'scoped'
    
    @classMethod 'scoped', ->
      ST.Model.Scope.createWithModel this
  
    @classMethod 'find', (uuid) ->
      ST.Model._byUuid[uuid]
  
    @classMethod 'load', (data) ->
      self = this
      if data instanceof Array
        for row in data
          @load row
      else
        return unless data && data.uuid
        return if ST.Model._byUuid[data.uuid]
        @createWithData data
    
    @classMethod 'master', ->
      @_master ||= ST.List.create()
    
    @classMethod 'index', (attribute) ->
      @_indexes ||= {}
      @_indexes[attribute] ||= ST.Model.Index.createWithModelAttribute(this, attribute)
  
    @initializer (options={}) ->
      @initWithData {}, options
      this
  
    # Initializes a new model, and loads the supplied attribute data, if any
    @initializer 'withData', (data, options={}) ->
      self = this
    
      ST.Object.prototype.init.call this

      @uuid data.uuid || ST.Model._generateUUID()
      @_creating = true
      @_attributes = {}
      for attribute, details of @_class._attributes
        if @_class._attributes.hasOwnProperty(attribute)
          if data[attribute]?
            @[attribute] data[attribute]
          else if !details.virtual
            @[attribute] details.default
      
      # Add class to indexes
      if @_class._indexes
        for attribute of @_class._attributes
          if @_class._attributes.hasOwnProperty(attribute)
            if index = @_class._indexes[attribute]
              index.add @_attributes[attribute], this 
    
      if @_class._manyBinds
        for binding in @_class._manyBinds
          @get(binding.assoc).bind binding.from, self, binding.to
      
      unless options.loaded
        ST.Model.recordChange 'create', @_uuid, @_class._name, @data()

      @_creating = false
      @_class.master().add this
  
    # Creates a new object from model data. If the data includes a 
    # property, as with data genereated by #objectify, the specified model
    # will be used instead of the model createWithData was called on.
    @classMethod 'createWithData', (data, options={}) ->
      # If data is being sent to the wrong model, transfer to correct model
      if data.model && data.model != @_name
        if modelClass = @_namespace.class data.model
          modelClass.createWithData data, options
        else
          null
      # If object with uuid already exists, return existing object
      else if data.uuid && ST.Model._byUuid[data.uuid]
        ST.Model._byUuid[data.uuid]
      # Otherwise, create a new object
      else
        object = new this
        object.initWithData data, options
        object
  
    @property 'uuid'
  
    @method 'setUuid', (newUuid) ->
      unless @_uuid
        newUuid = String newUuid
        
        # Insert object in global index
        ST.Model._byUuid[newUuid] = this
    
        # Insert object in model-specific index
        @_class._byUuid ||= {}
        @_class._byUuid[newUuid] = this
    
        @_uuid = newUuid
  
    @method 'matches', (conditions) ->
      if @_attributes
        for condition in conditions
          return false unless condition.test this
{'fraction_non_alphanumeric': 0.10134310134310134, 'fraction_numerical': 0.0, 'mean_word_length': 2.128724216959511, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '628375', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2349, 'n_words': 675}