Sha256: 30482a8c184db26126e24a9c9440c70fbf659755d82f5db695b69f2fbefad0f1

Contents?: true

Size: 1.68 KB

Versions: 19

Compression:

Stored size: 1.68 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

19 entries across 19 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.5.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.5.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.4.5 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.4.4 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.4.3 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.4.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.3.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.3.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.3.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.2.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.2.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.1.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.0.2 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.0.1 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-4.0.0 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-3.0.5 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-3.0.4 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-3.0.3 lib/cassandra_object/savepoints.rb
gotime-cassandra_object-3.0.2 lib/cassandra_object/savepoints.rb