New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dry-container for adapter fabric #15

Merged
merged 2 commits into from Aug 3, 2017
Jump to file or symbol
Failed to load files and symbols.
+53 −27
Diff settings

Always

Just for now

View
@@ -20,7 +20,7 @@ Or install it yourself as:
## Usage
### Adapters
Hanami events support different adapters for sending events:
Hanami events support different adapters for sending events. Each adapter loads in memory only in hanami event initialization.
#### Memory
Just initialize `Hanami::Event` instance with adapter:
@@ -29,7 +29,6 @@ Just initialize `Hanami::Event` instance with adapter:
Hanami::Events.build(:memory)
```
#### Redis
Redis adapter works only with `ConnectionPool` gem. Hanami events uses redis `SUBSCRIBE` under the hood.
@@ -38,6 +37,16 @@ redis = ConnectionPool.new(size: 5, timeout: 5) { Redis.new(host: 'localhost', p
Hanami::Events.build(:redis, redis: redis)
```
#### Custom Adapter
You can use your custom adapters. For this you need to create adapter class and register it in `Hanami::Event::Adapter` class:
```ruby
Hanami::Events::Adapter.register(:kinesis) { Kinesis }
event = Hanami::Events.build(:kinesis)
# => event instance with your kinesis adapter
```
### Broadcaster
```ruby
events = Hanami::Events.build(:memory)
View
@@ -23,6 +23,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "dry-container", "~> 0.6"
spec.add_development_dependency "redis"
spec.add_development_dependency "connection_pool"
spec.add_development_dependency "bundler", "~> 1.14"
@@ -1,16 +1,18 @@
require_relative 'adapter/memory'
require_relative 'adapter/redis'
require 'dry/container'
module Hanami
module Events
class Adapter
def self.build(adapter_name, options)
case adapter_name
when :memory then Memory.new
when :redis then Redis.new(**options)
else
Memory.new
end
extend Dry::Container::Mixin
register(:memory) do
require_relative 'adapter/memory'
Memory
end
register(:redis) do
require_relative 'adapter/redis'
Redis
end
end
end
@@ -4,7 +4,7 @@ class Adapter
class Memory
attr_reader :subscribers
def initialize
def initialize(**)
@subscribers = []
end
@@ -4,7 +4,7 @@ class Base
attr_reader :adapter
def initialize(adapter_name, options)
@adapter = Adapter.build(adapter_name, options)
@adapter = Adapter[adapter_name.to_sym].new(options)
end
def broadcast(event, **payload)
View
@@ -0,0 +1,10 @@
class MockAdapter
def initialize(**)
end
def broadcast(_event_name, _payload)
end
def subscribe(_event_name, &block)
end
end
@@ -1,3 +1,5 @@
require 'hanami/events/adapter/memory'
RSpec.describe Hanami::Events::Adapter::Memory do
let(:adapter) { described_class.new }
@@ -1,3 +1,4 @@
require 'hanami/events/adapter/redis'
require 'connection_pool'
require 'redis'
@@ -1,19 +1,10 @@
RSpec.describe Hanami::Events::Adapter do
context 'with :memory key' do
it 'returns memory adapter' do
expect(Hanami::Events::Adapter.build(:memory, {})).to be_a Hanami::Events::Adapter::Memory
end
describe '.keys' do
it { expect(described_class.keys).to eq %w[memory redis] }
end
context 'with :redis key' do
it 'returns redis adapter' do
expect(Hanami::Events::Adapter.build(:redis, {})).to be_a Hanami::Events::Adapter::Redis
end
end
context 'with invalid key' do
it 'returns default adapter' do
expect(Hanami::Events::Adapter.build(:invalid, {})).to be_a Hanami::Events::Adapter::Memory
end
describe '.[]' do
it { expect(described_class[:memory]).to eq(Hanami::Events::Adapter::Memory) }
it { expect(described_class[:redis]).to eq(Hanami::Events::Adapter::Redis) }
end
end
@@ -1,3 +1,5 @@
require 'support/fixtures'
RSpec.describe Hanami::Events do
let(:event) { Hanami::Events.build(:memory) }
@@ -27,4 +29,11 @@
}.to change { event.adapter.subscribers.count }.by(1)
end
end
it 'allows add custom adapters' do
Hanami::Events::Adapter.register(:mock_adapter) { MockAdapter }
event = Hanami::Events.build(:mock_adapter)
expect(event.adapter).to be_a(MockAdapter)
end
end