<commit_before>module EntityMapper
  module Repository
    module QueryMethods

      def save(entity)
        entity.tap do |e|
          data_store.transaction do
            run_hook :before_save, e
            e.persisted? ? update(e) : create(e)
            run_hook :after_save, e
          end
        end
      end

      def update(entity)
        entity.tap do |e|
          data_store.transaction do
            run_hook :before_update
            data_store.update e.id, map_entity_to_model(e)
            run_hook :after_update
          end
        end
      end

      def create(entity)
        entity.tap do |e|
          data_store.transaction do
            run_hook :before_create
            e.id = data_store.create(map_model_to_entity(e)).id
            run_hook :after_create
          end
        end
      end

      def find(id)
        entity_class.new(map_model_to_entity data_store.find(id))
      end

      protected
      def data_store
        self.class::DataStore
      end

      def map_entity_to_model(entity)
        attribute_mappings.inject({}) do |map, attribute_mapping|
          map[attribute_mapping.name] = entity.send(attribute_mapping.target)
          map
        end
      end<commit_msg>Factor out the common transactional logic<commit_after>module EntityMapper
  module Repository
    module QueryMethods

      def save(entity)
        perform(:save, entity) do |entity|
          entity.persisted? ? update(entity) : create(entity)
        end
      end

      def update(entity)
        perform(:update, entity) do |entity|
          data_store.update entity.id, map_entity_to_model(entity)
        end
      end

      def create(entity)
        perform(:create, entity) do |entity|
          entity.id = data_store.create(map_model_to_entity(entity)).id
        end
      end

      def find(id)
        entity_class.new(map_model_to_entity data_store.find(id))
      end

      protected
      def data_store
        self.class::DataStore
      end

      def perform(query_method, entity)
        entity.tap do |e|
          data_store.transaction do
            run_hook "before_#{query_method}", e
            yield e
            run_hook "after_#{query_method}", e
          end
        end
      end

      def map_entity_to_model(entity)
        attribute_mappings.inject({}) do |map, attribute_mapping|
          map[attribute_mapping.name] = entity.send(attribute_mapping.target)
          map
        end
      end
{'fraction_non_alphanumeric': 0.07047925896093435, 'fraction_numerical': 0.0, 'mean_word_length': 1.8683602771362586, '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': '7141171', 'n_tokens_mistral': 717, 'n_tokens_neox': 701, 'n_words': 161}