Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions hanami-events.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 11 additions & 9 deletions lib/hanami/events/adapter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/events/adapter/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Adapter
class Memory
attr_reader :subscribers

def initialize
def initialize(**)
@subscribers = []
end

Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions spec/support/fixtures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class MockAdapter
def initialize(**)
end

def broadcast(_event_name, _payload)
end

def subscribe(_event_name, &block)
end
end
2 changes: 2 additions & 0 deletions spec/unit/hanami/events/adapter/memory_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'hanami/events/adapter/memory'

RSpec.describe Hanami::Events::Adapter::Memory do
let(:adapter) { described_class.new }

Expand Down
1 change: 1 addition & 0 deletions spec/unit/hanami/events/adapter/redis_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'hanami/events/adapter/redis'
require 'connection_pool'
require 'redis'

Expand Down
19 changes: 5 additions & 14 deletions spec/unit/hanami/events/adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/unit/hanami/events_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'support/fixtures'

RSpec.describe Hanami::Events do
let(:event) { Hanami::Events.build(:memory) }

Expand Down Expand Up @@ -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