bus 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 746fd6530bc6f82afc053b09cd9ebd089a7df142
4
- data.tar.gz: 997acd122b159aeb6f13a189ed3d368ef671bf1a
3
+ metadata.gz: fdeaeec0009f535223bb95e159c4d06e14f80e6c
4
+ data.tar.gz: bf5f15509ce8656a13f0cbc6b828fc0a78c0d0a9
5
5
  SHA512:
6
- metadata.gz: e9a57a9a0096d57bbdcf7583fbc919375b95425cdbd7118eef7c51391b8199e0af63fe386cd5cc0c51c95cf8e914cee8573d5235d97e4951a29bc5003114793d
7
- data.tar.gz: 4a95013e2e893d14fc2476ed4d210275eac2880678cb490aefeb6547d9386a29467506e80f496ceb32604ce4856bdc6514dae7cc0736e4405a10cf31f176dc16
6
+ metadata.gz: 3a4f387058897f7db471f909c3d2ad94fe702bf737c1da1f0ac9a52b92032f94c0437015c0cadcf3be8ceb7ced78deb97a0f95e9f29870f0fa845f93d34aee39
7
+ data.tar.gz: 746dcc764ac880ed98da682c9fe6dfdbb113d581b63191bb342ebfed0b1e3912f47e5026250e8391815c035d0f6f7496e724d631f4969412c7cc69fe319af38b
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Bus
2
2
 
3
- TODO: Write a gem description
3
+ Extremely lightweight message bus without mixins, globals and mutable state.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,11 +18,72 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ # global config or initializer
23
+ bus = Bus.new([
24
+ DonationReceipt,
25
+ RealtimeViewerUpdate
26
+ ])
27
+
28
+
29
+ # controller
30
+ class DonationsController # < ActionController::Base
31
+ def create
32
+ listener = bus.
33
+ on(:donation_created) {|donation| send_user_to_payment_gateway(donation) }.
34
+ on(:donation_invalid) {|donation| render donation.errors, status: :bad_request }
35
+
36
+ # or
37
+
38
+ listener = bus.on(
39
+ donation_created: ->(donation) { send_user_to_payment_gateway(donation) },
40
+ donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
41
+ )
42
+
43
+ DonationService.new.add_donation(current_user, params[:donation], listener)
44
+ end
45
+
46
+ private
47
+ def send_user_to_payment_gateway(donation)
48
+ #...
49
+ end
50
+ end
51
+
52
+
53
+ # service
54
+ class DonationService
55
+ def initialize(options={})
56
+ @donation_class = options.fetch(:donation_class) { Donation }
57
+ end
58
+
59
+ def add_donation(donor, donation_attributes, listener)
60
+ donation = @donation_class.new(donation_attributes.merge(donor: donor))
61
+ if donation.valid?
62
+ donation.save!
63
+ listener.donation_created(donation)
64
+ else
65
+ listener.donation_invalid(donation)
66
+ end
67
+ end
68
+ end
69
+
70
+ # listeners
71
+ class DonationReceipt
72
+ def donation_created(donation)
73
+ Mailer.delay.send_donation_receipt(donation)
74
+ end
75
+ end
76
+
77
+ class RealtimeViewerUpdate
78
+ def donation_created(donation)
79
+ #..
80
+ end
81
+ end
82
+ ```
22
83
 
23
84
  ## Contributing
24
85
 
25
- 1. Fork it ( http://github.com/<my-github-username>/bus/fork )
86
+ 1. Fork it ( http://github.com/minio-sk/bus/fork )
26
87
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
88
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
89
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
data/bus.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["johno@jsmf.net"]
11
11
  spec.summary = %q{Lightweight message bus without mixins, globals or mutable state.}
12
12
  spec.description = %q{}
13
- spec.homepage = ""
13
+ spec.homepage = "http://github.com/minio-sk/bus"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
data/lib/bus.rb CHANGED
@@ -1,5 +1,39 @@
1
- require "bus/version"
1
+ require 'bus/version'
2
2
 
3
- module Bus
4
- # Your code goes here...
3
+ class Bus
4
+ def initialize(listeners = [])
5
+ @listeners = listeners
6
+ end
7
+
8
+ def attach(listener)
9
+ self.class.new(@listeners + [listener])
10
+ end
11
+
12
+ def on(event_or_hash, &block)
13
+ listeners = block_given? ? [CallableListener.new(event_or_hash, block)] : build_listeners_from_hash(event_or_hash)
14
+ self.class.new(@listeners + listeners)
15
+ end
16
+
17
+ def method_missing(method_name, *args)
18
+ @listeners.each do |listener|
19
+ listener.public_send(method_name, *args) if listener.respond_to?(method_name)
20
+ end
21
+ end
22
+
23
+ private
24
+ def build_listeners_from_hash(event_or_hash)
25
+ event_or_hash.map do |event, callable|
26
+ CallableListener.new(event, callable)
27
+ end
28
+ end
29
+
30
+ class CallableListener < Struct.new(:event, :callable)
31
+ def method_missing(_, *args)
32
+ callable.call(*args)
33
+ end
34
+
35
+ def respond_to?(method_name)
36
+ event == method_name
37
+ end
38
+ end
5
39
  end
data/lib/bus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Bus
2
- VERSION = "0.0.1"
1
+ class Bus
2
+ VERSION = '0.0.2'
3
3
  end
data/spec/bus_spec.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'bus'
3
+
4
+ describe Bus do
5
+ let (:listener) { double }
6
+
7
+ it 'calls attached listener' do
8
+ bus = described_class.new.attach(listener)
9
+
10
+ expect(listener).to receive(:event_fired)
11
+
12
+ bus.event_fired
13
+ end
14
+
15
+ it 'calls attached listener with params' do
16
+ bus = subject.attach(listener)
17
+
18
+ expect(listener).to receive(:event_fired).with(1, 2, 3)
19
+
20
+ bus.event_fired(1, 2, 3)
21
+ end
22
+
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)
26
+
27
+ expect(listener).not_to receive(:event_fired)
28
+
29
+ bus.event_fired
30
+ end
31
+
32
+ it 'calls multiple listeners' do
33
+ listener1 = double
34
+ listener2 = double
35
+
36
+ bus = subject.attach(listener1).attach(listener2)
37
+
38
+ expect(listener1).to receive(:event_fired)
39
+ expect(listener2).to receive(:event_fired)
40
+
41
+ bus.event_fired
42
+ end
43
+
44
+ it 'calls lightweight listener defined from hash' do
45
+ bus = subject.on(event_fired: listener) # event_fired: ->(args) { do stuff }
46
+
47
+ expect(listener).to receive(:call).with(:args)
48
+
49
+ bus.event_fired(:args)
50
+ end
51
+
52
+ it 'calls lightweight listener defined from block' do
53
+ bus = subject.on(:event_fired) {|args| listener.call(args)} # event_fired { do stuff }
54
+
55
+ expect(listener).to receive(:call).with(:args)
56
+
57
+ bus.event_fired(:args)
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ # Limit the spec run to only specs with the focus metadata. If no specs have
9
+ # the filtering metadata and `run_all_when_everything_filtered = true` then
10
+ # all specs will run.
11
+ #config.filter_run :focus
12
+
13
+ # Run all specs when none match the provided filter. This works well in
14
+ # conjunction with `config.filter_run :focus`, as it will run the entire
15
+ # suite when no specs have `:filter` metadata.
16
+ #config.run_all_when_everything_filtered = true
17
+
18
+ # Run specs in random order to surface order dependencies. If you find an
19
+ # order dependency and want to debug it, you can fix the order by providing
20
+ # the seed, which is printed after each run.
21
+ # --seed 1234
22
+ #config.order = 'random'
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Suchal
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: ''
42
56
  email:
43
57
  - johno@jsmf.net
@@ -46,6 +60,7 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
63
+ - ".rspec"
49
64
  - Gemfile
50
65
  - LICENSE.txt
51
66
  - README.md
@@ -53,7 +68,9 @@ files:
53
68
  - bus.gemspec
54
69
  - lib/bus.rb
55
70
  - lib/bus/version.rb
56
- homepage: ''
71
+ - spec/bus_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/minio-sk/bus
57
74
  licenses:
58
75
  - MIT
59
76
  metadata: {}
@@ -77,4 +94,6 @@ rubygems_version: 2.2.1
77
94
  signing_key:
78
95
  specification_version: 4
79
96
  summary: Lightweight message bus without mixins, globals or mutable state.
80
- test_files: []
97
+ test_files:
98
+ - spec/bus_spec.rb
99
+ - spec/spec_helper.rb