bus 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdeaeec0009f535223bb95e159c4d06e14f80e6c
4
- data.tar.gz: bf5f15509ce8656a13f0cbc6b828fc0a78c0d0a9
3
+ metadata.gz: 7366aa02d87782ec03a599d69d29bf79f8d32f76
4
+ data.tar.gz: 6ca5f0b363b9c14361777b9c5c10d5e3b81712b4
5
5
  SHA512:
6
- metadata.gz: 3a4f387058897f7db471f909c3d2ad94fe702bf737c1da1f0ac9a52b92032f94c0437015c0cadcf3be8ceb7ced78deb97a0f95e9f29870f0fa845f93d34aee39
7
- data.tar.gz: 746dcc764ac880ed98da682c9fe6dfdbb113d581b63191bb342ebfed0b1e3912f47e5026250e8391815c035d0f6f7496e724d631f4969412c7cc69fe319af38b
6
+ metadata.gz: c181e28dbc9ca2341e29682588fb9caddd7342be7144239159a118d98aec8fea7e2d19d75525ce582127800e97c396ceb2a9e890e2180ed8fdb00c973a79e716
7
+ data.tar.gz: d10c540ddc9f5eb6b0e04d7c07a7a0e520dc0603ff948f024c10552143e41e062cef5a17ffae6570f8e0fc4fb5fe76e52f0d6185fdc6a06b06bdd7366471c272
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ before_install: gem install bundler
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
7
+ - ruby-head
8
+ - rbx
9
+ addons:
10
+ code_climate:
11
+ repo_token:
12
+ secure: GIw2N3Nrj6E3eD1fkswv8DD7iDrWVlSPIpf6jWo0DXGo0X9Y7lqk0T8dPcq40UMOhDl38bXBr0m6iK46ZAQst8VT8qUlfX3R3Au/tpkCviO8GlhZR0e1V5X5OsBzS7QAIBqGXEvEYroxhuSe4gxrCjsO40VEs+ykcVfcD0ydC1g=
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in bus.gemspec
4
4
  gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Bus
2
2
 
3
- Extremely lightweight message bus without mixins, globals and mutable state.
3
+ Extremely lightweight message bus without mixins, globals or mutable state.
4
+
5
+ [![Build Status](https://travis-ci.org/minio-sk/bus.png)](https://travis-ci.org/minio-sk/bus) [![Code Climate](https://codeclimate.com/github/minio-sk/bus.png)](https://codeclimate.com/github/minio-sk/bus)
4
6
 
5
7
  ## Installation
6
8
 
@@ -19,7 +21,7 @@ Or install it yourself as:
19
21
  ## Usage
20
22
 
21
23
  ```ruby
22
- # global config or initializer
24
+ # global config or initializer or application controller
23
25
  bus = Bus.new([
24
26
  DonationReceipt,
25
27
  RealtimeViewerUpdate
@@ -30,7 +32,7 @@ bus = Bus.new([
30
32
  class DonationsController # < ActionController::Base
31
33
  def create
32
34
  listener = bus.
33
- on(:donation_created) {|donation| send_user_to_payment_gateway(donation) }.
35
+ on(:donation_created) {|donation| send_user_to_payment_gateway(donation) }. # NOTE the dot!
34
36
  on(:donation_invalid) {|donation| render donation.errors, status: :bad_request }
35
37
 
36
38
  # or
@@ -41,6 +43,13 @@ class DonationsController # < ActionController::Base
41
43
  )
42
44
 
43
45
  DonationService.new.add_donation(current_user, params[:donation], listener)
46
+
47
+ # or
48
+
49
+ DonationService.new.add_donation(current_user, params[:donation], bus.when(
50
+ donation_created: ->(donation) { send_user_to_payment_gateway(donation) },
51
+ donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
52
+ ))
44
53
  end
45
54
 
46
55
  private
@@ -52,14 +61,9 @@ end
52
61
 
53
62
  # service
54
63
  class DonationService
55
- def initialize(options={})
56
- @donation_class = options.fetch(:donation_class) { Donation }
57
- end
58
-
59
64
  def add_donation(donor, donation_attributes, listener)
60
- donation = @donation_class.new(donation_attributes.merge(donor: donor))
61
- if donation.valid?
62
- donation.save!
65
+ donation = donor.donations.build(donation_attributes)
66
+ if donation.save
63
67
  listener.donation_created(donation)
64
68
  else
65
69
  listener.donation_invalid(donation)
data/lib/bus.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'bus/version'
2
2
 
3
3
  class Bus
4
+ class NoListenerRespondedError < RuntimeError; end
5
+
4
6
  def initialize(listeners = [])
5
7
  @listeners = listeners
6
8
  end
@@ -14,10 +16,17 @@ class Bus
14
16
  self.class.new(@listeners + listeners)
15
17
  end
16
18
 
19
+ alias :when :on
20
+
17
21
  def method_missing(method_name, *args)
22
+ responded = false
18
23
  @listeners.each do |listener|
19
- listener.public_send(method_name, *args) if listener.respond_to?(method_name)
24
+ if listener.respond_to?(method_name)
25
+ responded = true
26
+ listener.public_send(method_name, *args)
27
+ end
20
28
  end
29
+ raise NoListenerRespondedError.new("No listener responded to message '#{method_name}'") unless responded
21
30
  end
22
31
 
23
32
  private
@@ -1,3 +1,3 @@
1
1
  class Bus
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -5,7 +5,7 @@ describe Bus do
5
5
  let (:listener) { double }
6
6
 
7
7
  it 'calls attached listener' do
8
- bus = described_class.new.attach(listener)
8
+ bus = subject.attach(listener)
9
9
 
10
10
  expect(listener).to receive(:event_fired)
11
11
 
@@ -21,10 +21,11 @@ describe Bus do
21
21
  end
22
22
 
23
23
  it 'does not call listener if it does not respond to event' do
24
- listener = double(respond_to?: false)
25
- bus = subject.attach(listener)
24
+ non_responding_listener = double(respond_to?: false)
25
+ responding_listener = double.as_null_object
26
+ bus = subject.attach(non_responding_listener).attach(responding_listener)
26
27
 
27
- expect(listener).not_to receive(:event_fired)
28
+ expect(non_responding_listener).not_to receive(:event_fired)
28
29
 
29
30
  bus.event_fired
30
31
  end
@@ -56,4 +57,16 @@ describe Bus do
56
57
 
57
58
  bus.event_fired(:args)
58
59
  end
60
+
61
+ it 'aliases on and when methods' do
62
+ bus = subject.when(:event_fired) {|args| listener.call(args)} # event_fired { do stuff }
63
+
64
+ expect(listener).to receive(:call).with(:args)
65
+
66
+ bus.event_fired(:args)
67
+ end
68
+
69
+ it 'throws an exception when no listener responds' do
70
+ expect { subject.event_fired }.to raise_error(Bus::NoListenerRespondedError, 'No listener responded to message \'event_fired\'')
71
+ end
59
72
  end
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start if ENV['CODECLIMATE_REPO_TOKEN']
3
+
1
4
  # This file was generated by the `rspec --init` command. Conventionally, all
2
5
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
6
  # Require this file using `require "spec_helper"` to ensure that it is only
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Suchal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
11
+ date: 2014-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".travis.yml"
64
65
  - Gemfile
65
66
  - LICENSE.txt
66
67
  - README.md
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  version: '0'
91
92
  requirements: []
92
93
  rubyforge_project:
93
- rubygems_version: 2.2.1
94
+ rubygems_version: 2.2.2
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: Lightweight message bus without mixins, globals or mutable state.