<commit_before># TODO: spec the ActiveRecord behaviour
# - processing of remote errors
# - response parsing (using data: [] format)
# - save methods
module MnoEnterprise
  class BaseResource
    include Her::Model
    #parse_root_in_json :data
    include_root_in_json :data
    use_api MnoEnterprise.mnoe_api_v1
    
    # TODO: spec that changed_attributes is empty
    # after a KLASS.all / KLASS.where etc..
    after_find { |res| res.instance_variable_set(:@changed_attributes, {}) }
    
    # Attributes common to all classes
    attributes :id, :created_at, :updated_at
    
    # Class query methods
    class << self
      # Delegate the following methods to `scoped`
      # Clear
      [:all, :where, :create, :build, :find, :first_or_create, :first_or_initialize, :limit, :order_by, :sort_by, :order, :sort].each do |method|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{method}(*params)
            scoped.reset_params
            scoped.send(#{method.to_sym.inspect}, *params)
          end
        RUBY
      end
    end
    
    # Emulate ActiveRecord for Her
    def read_attribute(attr_name)
      get_attribute(attr_name)
    end
    
    # Emulate ActiveRecord for Her
    def write_attribute(attr_name, value)
      assign_attributes(attr_name => value)
    end
    alias []= write_attribute
    
    # Emulate ActiveRecord for Her
    def save(options={})
      if perform_validations(options) 
        ret = super()
        process_response_errors
        ret
      else
        false
      end
    end
    
    # Emulate ActiveRecord for Her
    def save!(options={})
      if perform_validations(options) 
        ret = super()<commit_msg>added first/last class methods<commit_after># TODO: spec the ActiveRecord behaviour
# - processing of remote errors
# - response parsing (using data: [] format)
# - save methods
module MnoEnterprise
  class BaseResource
    include Her::Model
    #parse_root_in_json :data
    include_root_in_json :data
    use_api MnoEnterprise.mnoe_api_v1
    
    # TODO: spec that changed_attributes is empty
    # after a KLASS.all / KLASS.where etc..
    after_find { |res| res.instance_variable_set(:@changed_attributes, {}) }
    
    # Attributes common to all classes
    attributes :id, :created_at, :updated_at
    
    # Class query methods
    class << self
      # Delegate the following methods to `scoped`
      # Clear
      [:all, :where, :create, :build, :find, :first_or_create, :first_or_initialize, :limit, :order_by, :sort_by, :order, :sort].each do |method|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{method}(*params)
            scoped.reset_params
            scoped.send(#{method.to_sym.inspect}, *params)
          end
        RUBY
      end
      
      # Emulate ActiveRecord for Her
      def first(n = 1)
        return [] unless n > 0
        q = self.order_by('id.asc').limit(n)
        n == 1 ? q.to_a.first : q.to_a
      end
    
      # Emulate ActiveRecord for Her
      def last(n = 1)
        return [] unless n > 0
        q = self.order_by('id.desc').limit(n)
        n == 1 ? q.to_a.first : q.to_a
      end
    end
    
    # Emulate ActiveRecord for Her
    def read_attribute(attr_name)
      get_attribute(attr_name)
    end
    
    # Emulate ActiveRecord for Her
    def write_attribute(attr_name, value)
      assign_attributes(attr_name => value)
    end
    alias []= write_attribute
    
    # Emulate ActiveRecord for Her
    def save(options={})
      if perform_validations(options) 
        ret = super()
        process_response_errors
        ret
      else
        false
      end
    end
    
    # Emulate ActiveRecord for Her
    def save!(options={})
      if perform_validations(options) 
        ret = super()
{'fraction_non_alphanumeric': 0.08989657915672236, 'fraction_numerical': 0.0026518164942985947, 'mean_word_length': 2.3829596412556056, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '1072261', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1153, 'n_words': 314}