Sha256: 7c392540a4b848b3a36061bf2316ba8a11154cbbf744f290206c580a5a87df48

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'active_record'
require 'active_support/core_ext/class/inheritable_attributes'

class Memento
  include Singleton
  
  class ErrorOnRewind < StandardError;end
  
  def memento(user)
    start(user)
    yield
    !@session.states.count.zero? && @session rescue false
  ensure
    stop
  end
  
  def start(user_or_id)
    user = user_or_id.is_a?(User) ? user_or_id : User.find_by_id(user_or_id)
    @session = user ? Memento::Session.new(:user => user) : nil
  end
  
  def stop
    @session.destroy if @session && @session.states.count.zero?
    @session = nil
  end
  
  def add_state(action_type, record)
    return unless save_session
    @session.add_state(action_type, record)
  end
  
  def active?
    !!(defined?(@session) && @session) && !ignore?
  end
  
  def ignore
    @ignore = true
    yield
  ensure
    @ignore = false
  end
  
  def self.serializer=(serializer)
    @serializer = serializer
  end
  
  def self.serializer
    @serializer ||= YAML
  end
  
  private
  
  def ignore?
    defined?(@ignore) && @ignore
  end
  
  def save_session
    active? && (!@session.changed? || @session.save)
  end
end

require 'memento/result'
require 'memento/action'
require 'memento/active_record_methods'
require 'memento/action_controller_methods'
require 'memento/state'
require 'memento/session'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
memento-0.3.4 lib/memento.rb