<commit_before>module AMEE
  module DataAbstraction
    module PersistenceSupport

      def self.included(base)
        base.extend ClassMethods
      end

      attr_accessor :db_calculation

      def id
        db_calculation.nil?? nil : db_calculation.id
      end
      
      def save
        validate!
        record = db_calculation || AMEE::Db::Calculation.new
        record.update_calculation!(to_hash)
        true
      rescue ActiveRecord::RecordNotSaved
        false
      end

      def delete
        record = db_calculation || get_db_calculation
        self.db_calculation = nil
        AMEE::Db::Calculation.delete record.id
      end

      def stored_terms
        stored_terms = []
        stored_terms += metadata if OngoingCalculation.store_metadata?
        stored_terms += inputs if OngoingCalculation.store_inputs?
        stored_terms += outputs if OngoingCalculation.store_outputs?
        stored_terms
      end

      def to_hash(representation=:stored_terms_only)
        hash = {}
        hash[:calculation_type] = label
        hash[:profile_item_uid] = send :profile_item_uid
        hash[:profile_uid] = send :profile_uid
        (representation == :full ? terms : stored_terms ).each do |term|
          hash[term.label.to_sym] = (term.unit.nil? ? term.value : "#{term.value} #{term.unit}")
        end
        return hash
      end

      module ClassMethods

        def find(*args)
          args.last[:include] = "terms" if args.last[:joins].present?
          result = AMEE::Db::Calculation.find(*args)
          return nil unless result
          if result.respond_to?(:map)
            result.compact.map do |calc|
              initialize_from_db_record(calc)
            end
          else<commit_msg>Raise error if use include and then abuse the to use terms in conditions<commit_after>module AMEE
  module DataAbstraction
    module PersistenceSupport

      def self.included(base)
        base.extend ClassMethods
      end

      attr_accessor :db_calculation

      def id
        db_calculation.nil?? nil : db_calculation.id
      end
      
      def save
        validate!
        record = db_calculation || AMEE::Db::Calculation.new
        record.update_calculation!(to_hash)
        true
      rescue ActiveRecord::RecordNotSaved
        false
      end

      def delete
        record = db_calculation || get_db_calculation
        self.db_calculation = nil
        AMEE::Db::Calculation.delete record.id
      end

      def stored_terms
        stored_terms = []
        stored_terms += metadata if OngoingCalculation.store_metadata?
        stored_terms += inputs if OngoingCalculation.store_inputs?
        stored_terms += outputs if OngoingCalculation.store_outputs?
        stored_terms
      end

      def to_hash(representation=:stored_terms_only)
        hash = {}
        hash[:calculation_type] = label
        hash[:profile_item_uid] = send :profile_item_uid
        hash[:profile_uid] = send :profile_uid
        (representation == :full ? terms : stored_terms ).each do |term|
          hash[term.label.to_sym] = (term.unit.nil? ? term.value : "#{term.value} #{term.unit}")
        end
        return hash
      end

      module ClassMethods

        def find(*args)
          raise ActiveRecord::ActiveRecordError.new("Using :include with terms and then conditioning on terms doesn't work due to rails caching.  Use the :joins option instead.") if args.last[:include].to_s.match(/terms/) && args.last[:conditions].to_s.match(/terms/)
          args.last[:include] = "terms" if args.last[:joins].to_s.match(/terms/)
          result = AMEE::Db::Calculation.find(*args)
          return nil unless result
          if result.respond_to?(:map)
            result.compact.map do |calc|
              initialize_from_db_record(calc)
            end
          else
{'fraction_non_alphanumeric': 0.08494007295466388, 'fraction_numerical': 0.0, 'mean_word_length': 2.292452830188679, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2395187', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1081, 'n_words': 250}