Sha256: 39810bc59efa55f64c567242fc546c04bc3929ab17b08230e2e914e119f9fe64

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

require 'active_record'

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

6 entries across 6 versions & 1 rubygems

Version Path
memento-0.3.7 lib/memento.rb
memento-0.3.6 lib/memento.rb
memento-0.3.5 lib/memento.rb
memento-0.3.3 lib/memento.rb
memento-0.3.2 lib/memento.rb
memento-0.3.1 lib/memento.rb