Sha256: 5dd810d8a628d96d0cb82e40df108857dbb6adce4a7de9e566e091acd278d528

Contents?: true

Size: 1.67 KB

Versions: 22

Compression:

Stored size: 1.67 KB

Contents

module CassandraObject
  module Savepoints
    class Rollback
      attr_accessor :action, :record, :rollback_attributes
      def initialize(action, record)
        @action = action
        @record = record
        if action == :update
          @rollback_attributes = record.changed_attributes.deep_dup
        elsif action == :create
          @rollback_attributes = record.attributes.deep_dup
        end
      end

      def run!
        case action
        when :destroy
          record.class.remove(record.id)
        when :create, :update
          record.class.write(record.id, rollback_attributes)
        end
      end
    end

    class Savepoint
      attr_accessor :rollbacks
      def initialize
        self.rollbacks = []
      end

      def add_rollback(action, record)
        states << Rollback.new(action, record)
      end

      def rollback!
        rollbacks.reverse_each(&:run!)
      end
    end

    extend ActiveSupport::Concern

    included do
      class_attribute :savepoints
      self.savepoints = []
    end

    module ClassMethods
      def savepoint
        self.savepoints.push Savepoint.new
        yield
        savepoints.pop
      rescue => e
        savepoints.pop.rollback!
      end

      def add_savepoint_rollback(action, record)
        unless savepoints.empty?
          savepoints.last.add_rollback(action, record)
        end
      end
    end

    def destroy
      self.class.add_savepoint_rollback(:create, self)
      super
    end

    private
      def create
        self.class.add_savepoint_rollback(:destroy, self)
        super
      end

      def update
        self.class.add_savepoint_rollback(:update, self)
        super
      end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
gotime-cassandra_object-4.11.4 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.11.3 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.11.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.11.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.11.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.5 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.4 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.3 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.1 lib/cassandra_object/savepoints.rb
cassandra_object_rails-0.0.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.10.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.9.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.9.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.9.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.8.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.8.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.7.3 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.7.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.7.1 lib/cassandra_object/savepoints.rb