Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/voltage
A simple observer implementation on POROs (Plain Old Ruby Object) and ActiveRecord objects.
https://github.com/fnando/voltage
Last synced: 27 days ago
JSON representation
A simple observer implementation on POROs (Plain Old Ruby Object) and ActiveRecord objects.
- Host: GitHub
- URL: https://github.com/fnando/voltage
- Owner: fnando
- License: mit
- Created: 2013-01-26T13:38:37.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2023-09-05T01:06:08.000Z (about 1 year ago)
- Last Synced: 2024-10-06T04:25:21.399Z (about 1 month ago)
- Language: Ruby
- Size: 87.9 KB
- Stars: 31
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
A simple observer implementation for POROs (Plain Old Ruby Object)
and ActiveRecord objects.---
## Installation
Add this line to your application's Gemfile:
```ruby
gem "voltage"
```And then execute:
$ bundle
Or install it yourself as:
$ gem install voltage
## Usage
You can use Voltage with PORO (Plain Old Ruby Object) and ActiveRecord.
### Plain Ruby
All you have to do is including the `Voltage` module. Then you can add listeners
and trigger events.```ruby
class Status
include Voltagedef ready!
emit(:ready)
end
endstatus = Status.new
status.before(:ready) { puts "Before the ready event!" }
status.on(:ready) { puts "I'm ready!" }
status.after(:ready) { puts "After the ready event!" }
status.ready!
#=> Before the ready event!
#=> I'm ready!
#=> After the ready event!
```You can also pass objects that implement methods like `before_*`, `on_*` and
`after_*`.```ruby
class MyListener
def before_ready
puts "Before the ready event!"
enddef on_ready
puts "I'm ready!"
enddef after_ready
puts "After the ready event!"
end
endStatus.new
.add_listener(MyListener.new)
.ready!
#=> Before the ready event!
#=> I'm ready!
#=> After the ready event!
```Executed blocks don't switch context. You always have to emit the object you're
interested in. The follow example uses `emit(:output, self)` to send the
`Contact` instance to all listeners.```ruby
class Contact
include Voltageattr_reader :name, :email
def initialize(name, email)
@name, @email = name, email
enddef output!
emit(:output, self)
end
endcontact = Contact.new('John Doe', '[email protected]')
contact.on(:output) {|contact| puts contact.name, contact.email }
contact.output!
#=> John Doe
#=> [email protected]
```You can provide arguments while emitting a voltage:
```ruby
class Arguments
include Voltage
endclass MyListener
def on_args(a, b)
puts a, b
end
endArguments.new
.on(:args) {|a, b| puts a, b }
.add_listener(MyListener.new)
.emit(:args, 1, 2)
```### ActiveRecord
You can use Voltage with ActiveRecord, which will give you some default events
like `:create`, `:update`, `:remove` and `:validation`.```ruby
class Thing < ActiveRecord::Base
include Voltage.active_recordvalidates_presence_of :name
endthing = Thing.new(:name => "Stuff")
thing.on(:create) {|thing| puts thing.updated_at, thing.name }
thing.on(:update) {|thing| puts thing.updated_at, thing.name }
thing.on(:remove) {|thing| puts thing.destroyed? }
thing.on(:validation) {|thing| p thing.errors.full_messages }thing.save!
#=> 2013-01-26 10:32:39 -0200
#=> Stuffthing.update_attributes(:name => "Updated stuff")
#=> 2013-01-26 10:33:11 -0200
#=> Updated stuffthing.update_attributes(:name => nil)
#=> ["Name can't be blank"]thing.destroy
#=> true
```These are the available events:
- `before(:create)`: triggered before creating the record (record is valid).
- `on(:create)`: triggered after `before(:create)` event.
- `after(:create)`: triggered after the `on(:create)` event.
- `before(:update)`: triggered before updating the record (record is valid).
- `on(:update)`: triggered when the `before(:update)` event.
- `after(:update)`: triggered after the `on(:update)` event.
- `before(:remove)`: triggered before removing the record.
- `on(:remove)`: triggered after the `before(:remove)`.
- `after(:remove)`: triggered after the `on(:remove)` event.
- `before(:validation)`: triggered before validating record.
- `on(:validation)`: triggered when record is invalid.
- `after(:validation)`: triggered after validating record.### Inside Rails
Although there's no special code for Rails, here's just an example of how you
can use it:```ruby
class UsersController < ApplicationController
def create
@user = User.new(user_params)Signup.new(@user)
.on(:success) { redirect_to login_path, notice: 'Welcome to MyApp!' }
.on(:failure) { render :new }
.call
end
end
```If you're using plain ActiveRecord, just do something like the following:
```ruby
class UsersController < ApplicationController
def create
@user = User.new(user_params)
@user
.on(:create) { redirect_to login_path, notice: 'Welcome to MyApp!' }
.on(:validation) { render :new }
.save
end
end
```### Voltage::Call
You can include `Voltage.call` instead, so you can have a common interface for
your observable object. This will add the `.call()` method to the target class,
which will delegate attributes to the observable's `initialize` method and call
its `call` method.```ruby
class Contact
include Voltage.callattr_reader :name, :email
def initialize(name, email)
@name, @email = name, email
enddef call
emit(:output, self)
end
endContact.call('John', '[email protected]') do |o|
o.on(:output) {|contact| puts contact }
end
```Notice that you don't have to explicit call the instance's `call` method;
`Contact.call` will initialize the object with all the provided parameters and
call `Contact#call` after the block has been executed.### Testing
`Voltage::Mock` can be helpful for most test situations where you don't want to
bring other mock libraries.```ruby
require "voltage/mock"class SomeTest < Minitest::Test
def test_some_test
mock = Voltage::Mock.new# Using listener
sum = Sum.new
sum.add_listener(mock)# Calling `mock.on(event_name)` is required because
# the handler doesn't receive the event name, just the
# arguments.
sum = Sum.new
sum.on(:result, &mock.on(:result))# Using with Voltage.call
Sum.call(1, 2, &mock)assert mock.received?(:result)
assert mock.received?(:result, times: 1)
assert mock.received?(:result, with: [3])
assert mock.received?(:result, with: ->(result) { result == 3 } )
end
end```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## License
Copyright (c) 2013 Nando Vieira
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.