<commit_before>angular.module('angular_objectify_resource')

.factory 'aor.BaseModel', ['aor.utils', (utils)->
  class BaseModel

    @has_many: (name, options)->
      @HAS_MANY_RELATIONS ?= []
      @HAS_MANY_RELATIONS.push(angular.extend({ name: name }, options))

    @has_one: (name, options)->
      @HAS_ONE_RELATIONS ?= []
      @HAS_ONE_RELATIONS.push(angular.extend({ name: name }, options))

    @decorator: (klass)->
      @DECORATOR = klass

    @skip_date_conversion: (keys...)->
      @SKIP_DATE_CONVERSION = keys

    constructor: (resource, @_resource = null)->
      angular.extend(@, resource)
      @before_init()
      @_extend_children()
      @_convert_dates()
      @after_init()

    before_init: ->

    after_init: ->

    decorator: ->
      @_decorator ?= new (@constructor.DECORATOR)(@)

    toParams: ->
      result = {}
      _.forIn @, (value, key)->
        result[key] = value unless utils.string_starts_with(key, '_') || angular.isFunction(value)
      result

    save: (args...)->
      params = @toParams()
      on_success = ->
      on_error   = ->
      if _.isObject(args[0])
        _.extend params, args[0]
        on_success = args[1] if_.isFunction(args[1])
        on_error   = args[2] if_.isFunction(args[2])
      else
        on_success = args[0] if_.isFunction(args[0])
        on_error   = args[1] if_.isFunction(args[2])
      if @id
        @_resource.update(params, on_success, on_error)
      else
        @_resource.create(params, on_success, on_error)

    destroy: (on_success, on_error)->
      @_resource.destroy(@toParams(), on_success, on_error)

    _convert_dates: ->
      for own key, value of @
        @[key] = @_convert_date(value) if @_is_date_to_convert(key, value)

    _convert_date: (date)->
      return null unless date
      moment(date, 'YYYY-MM-DDTHH:mm:ssZZ').toDate()

    _convert_date_to_time_zone: (date, local_offset_in_seconds)->
      date = @_convert_date(date)
      date.setSeconds(date.getSeconds() + local_offset_in_seconds)
      date

    _extend_children: ->
      @constructor.HAS_MANY_RELATIONS ?= []
      for relation in @constructor.HAS_MANY_RELATIONS
        relation_name = relation.name
        camelized_relation_name = utils.camelize(utils.singularize(relation_name))
        build_method_name = "build#{ camelized_relation_name }"

        @[build_method_name] = @_build_method(relation)

        if @[relation_name]
          add_method_name = "add#{ camelized_relation_name }"<commit_msg>persiatnce check through method<commit_after>angular.module('angular_objectify_resource')

.factory 'aor.BaseModel', ['aor.utils', (utils)->
  class BaseModel

    @has_many: (name, options)->
      @HAS_MANY_RELATIONS ?= []
      @HAS_MANY_RELATIONS.push(angular.extend({ name: name }, options))

    @has_one: (name, options)->
      @HAS_ONE_RELATIONS ?= []
      @HAS_ONE_RELATIONS.push(angular.extend({ name: name }, options))

    @decorator: (klass)->
      @DECORATOR = klass

    @skip_date_conversion: (keys...)->
      @SKIP_DATE_CONVERSION = keys

    constructor: (resource, @_resource = null)->
      angular.extend(@, resource)
      @before_init()
      @_extend_children()
      @_convert_dates()
      @after_init()

    before_init: ->

    after_init: ->

    decorator: ->
      @_decorator ?= new (@constructor.DECORATOR)(@)

    toParams: ->
      result = {}
      _.forIn @, (value, key)->
        result[key] = value unless utils.string_starts_with(key, '_') || angular.isFunction(value)
      result

    save: (args...)->
      params = @toParams()
      on_success = ->
      on_error   = ->
      if _.isObject(args[0])
        _.extend params, args[0]
        on_success = args[1] if_.isFunction(args[1])
        on_error   = args[2] if_.isFunction(args[2])
      else
        on_success = args[0] if_.isFunction(args[0])
        on_error   = args[1] if_.isFunction(args[2])
      if @_is_persisted()
        @_resource.update(params, on_success, on_error)
      else
        @_resource.create(params, on_success, on_error)

    destroy: (on_success, on_error)->
      @_resource.destroy(@toParams(), on_success, on_error)

    _is_persisted: ->
      _.has(@, 'id')

    _convert_dates: ->
      for own key, value of @
        @[key] = @_convert_date(value) if @_is_date_to_convert(key, value)

    _convert_date: (date)->
      return null unless date
      moment(date, 'YYYY-MM-DDTHH:mm:ssZZ').toDate()

    _convert_date_to_time_zone: (date, local_offset_in_seconds)->
      date = @_convert_date(date)
      date.setSeconds(date.getSeconds() + local_offset_in_seconds)
      date

    _extend_children: ->
      @constructor.HAS_MANY_RELATIONS ?= []
      for relation in @constructor.HAS_MANY_RELATIONS
        relation_name = relation.name
        camelized_relation_name = utils.camelize(utils.singularize(relation_name))
        build_method_name = "build#{ camelized_relation_name }"

        @[build_method_name] = @_build_method(relation)

        if @[relation_name]
          add_method_name = "add#{ camelized_relation_name }"
{'fraction_non_alphanumeric': 0.13561994874827518, 'fraction_numerical': 0.003942440370589394, 'mean_word_length': 2.9121048573631456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, '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': '3317283', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1680, 'n_words': 294}