Riposte
Riposte was put together to scratch an itch that I had when working with multiple outcomes of telling an object what to do and then responding differently depending on the results of the action. This happens all the time in the software that I write. I want to save an object to the database and I can't because of the object not meeting the database requirements, or the database is down. I might have a whole host of different responses for the user. This is where Riposte is geared. It is a very small gem, and I hope to keep it that way. Finally after years of using similar ideas I've finally made a gem out of it.
Installation
Add this line to your application's Gemfile:
gem 'riposte'
And then execute:
$ bundle
Or install it yourself as:
$ gem install riposte
Usage
I'm going to show an example usage within a basic Rails application because that is where I've found this to be used the most.
class MyController < ApplicationController
def create
@article = Article.new
@article.publish { |on|
on.published { send_email_to_subscribers }
on.invalid { handle_invalid }
on.exception { |e| database_down_handler(e) }
}
respond_with @artcile
end
end
class Article < ActiveRecord::Base
include Riposte::Helper
def publish(&block)
if valid?
#publish logic
react_to :published, &block
else
#failure logic
react_to :invalid, &block
end
rescue SomeError => e
react_to :exception, e, &block #passing an argument for reactions
end
end
You may prefer not to use blocks. This is powerful as you can pass the response object around. You can also use the object multiple times in different places and with different blocks.
class MyController < ApplicationController
def create
@article = Article.new
on = @article.publish
on.published { send_email_to_subscribers }
on.invalid { handle_invalid }
on.exception { |e| database_down_handler(e) }
respond_with @artcile
end
end
class Article < ActiveRecord::Base
include Riposte::Helper
def publish
if valid?
#publish logic
react_to :published
else
#failure logic
react_to :invalid
end
rescue SomeError => e
react_to :exception, e, &block #passing an argument for reactions
end
end
Contributing
- Fork it ( https://github.com/adkron/riposte/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 a new Pull Request
License
Licensed under the MIT license. See the LICENSE.txt file for more details.