Undo
Undo reverts operation made upon object.
It stores the object state before the mutator operation and allows to
restore this state later.
Undo uses adapters for storage (Redis, ActiveRecord, etc) and custom serializers (ActiveRecord, Virtus, etc). It is very lightweight solution that can be used as well with heavy ActiveRecord as with simple Hash or Virtus objects. No database required: store data as it suites you.
Contents
- Installation
- Requirements
- Usage
- Undo operation
- Configuration options
- In place configuration
- Contacts
- Compatibility
- Contributing
- Copyright
Installation
Add this line to your application's Gemfile:
gem 'undo'
And then execute:
$ bundle
Or install it yourself as:
$ gem install undo
Requirements
- Ruby 1.9 or above
- gem virtus ~> 1.0
Usage
Undo operation
Wrap object in Undo decorator:
Undo.wrap object
And use it as usual afterwards. Undo gem will store object state on each hit to mutator methods.
By default mutator_methods are update, delete, destroy.
To append custom mutator_methods use
Undo.configure do |config|
config.mutator_methods += [:put, :push, :pop]
end
To restore previous version
Undo.restore uuid
uuid is provided by wrapped object:
Undo.wrap(object).uuid
By default it is using SecureRandom.uuid.
To define custom uuid generator use uuid_generator option:
Undo.configure do |config|
config.uuid_generator = ->(object) { "#{object.class.name}_#{object.id}" }
end
Configuration options
storage option responsible for putting and fetching object state to or from some storage.
Implement put(uuid, object) and fetch(uuid) methods.
Currently available storages:
Undo::Storage::Memorysimple runtime storage (Hash)gem "undo-storage-redis"designed to be used withgem "redis"from v0.1 to current
See also documentation
on project repository for full list of currently available storage adapters.
To convert objects to data that can be processed by storage adapters and backward, use serializers:
Undo.configure do |config|
config.serializer = CustomSerializer.new
end
Currently available serializers:
Undo::Serializer::Nullpass though serializer. It do nothing and returns whatever passed to it.
Check documentation on project repository for currently available serializers.
uuid_generator option allows to setup custom uuid generator.
By default it is using SecureRandom.uuid.
To define custom uuid generator use uuid_generator option:
Undo.configure do |config|
config.uuid_generator = ->(object) { "#{object.class.name}_#{object.id}" }
end
mutator methods option defines a list of methods that will trigger storage#put
By default mutator_methods are update, delete, destroy.
To set custom mutator_methods use
Undo.configure do |config|
config.mutator_methods = [:put, :push, :pop]
end
In place configuration
Any configuration option from previous chapter can be applied in
place for given operation. To restore object from another storage use
storage option:
Undo.restore uuid, storage: AnotherStorage.new
To wrap an object using custom mutator_methods use mutator_methods option:
Undo.wrap object, mutator_methods: [:save]
To use custom serializer or deserializer use serializer option:
Undo.wrap post, serializer: PostSerializer.new(post)
post.destroy
Undo.restore uuid, serializer: PostDeserializer.new()
Contacts
Have questions or recommendations? Contact me via [email protected]
Found a bug or have enhancement request? You are welcome at Github bugtracker
Compatibility
tested with Ruby
- 2.1
- 2.0
- 1.9.3
- ruby-head
- rbx
- jruby-19mode
- jruby-head
See build history
Contributing
- Fork repository ( http://github.com/AlexParamonov/undo/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Copyright
Copyright © 2014 Alexander Paramonov. Released under the MIT License. See the LICENSE file for further details.



