spine-hub 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0ccc25b91940f8d3ad1cfe542b9b6d3fa648503
4
+ data.tar.gz: 2a4047e93926e7913f9b8d7236132e652e1222ed
5
+ SHA512:
6
+ metadata.gz: 83d1fa219bca4ffeff20fa24d4650ad77807be5a5791cd5f359f0978d5df5672610233d882f5f7c1d039a0509fcd062e217df320174921db90eece4f1d3cd0c2
7
+ data.tar.gz: 5598470942c02544ea13b5fc9144650778e2791f6cece05fb11f68dc35b4f9523c05f3b8d8c6bb83c38fa88676c7a7de4d1e58f892745099e6a5102f19b9c4ce
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /bin/
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require config/default
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spine-hub.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spine-hub (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.4.2)
11
+ rspec (3.2.0)
12
+ rspec-core (~> 3.2.0)
13
+ rspec-expectations (~> 3.2.0)
14
+ rspec-mocks (~> 3.2.0)
15
+ rspec-core (3.2.3)
16
+ rspec-support (~> 3.2.0)
17
+ rspec-expectations (3.2.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.2.0)
20
+ rspec-mocks (3.2.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.2.0)
23
+ rspec-support (3.2.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.7)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.2)
32
+ spine-hub!
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2015, TOGGL LLC
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
10
+ 3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Spine::Hub
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/spine-hub.svg)](http://badge.fury.io/rb/spine-hub)
4
+ [![Dependency Status](https://gemnasium.com/rspine/hub.svg)](https://gemnasium.com/rspine/hub)
5
+ [![Code Climate](https://codeclimate.com/github/rspine/hub/badges/gpa.svg)](https://codeclimate.com/github/rspine/hub)
6
+ [![Codeship Status for rspine/hub](https://codeship.com/projects/951d2de0-e105-0132-721b-5eabae96dc8d/status?branch=master)](https://codeship.com/projects/81031)
7
+
8
+ Provides Publish/Subscribe pattern.
9
+
10
+ ## Installation
11
+
12
+ To install it, add the gem to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'spine-hub'
16
+ ```
17
+
18
+ Then run `bundle`. If you're not using Bundler, just `gem install spine-hub`.
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ class Service
24
+ include Spine::Hub::Publisher
25
+
26
+ def action
27
+ publish(:action_executing)
28
+ # ...
29
+ publish(:action_executed, 'OK')
30
+ end
31
+ end
32
+
33
+ class Listener
34
+ include Spine::Hub::Subscriber
35
+
36
+ def on_action_executing
37
+ puts 'Executing action'
38
+ end
39
+ end
40
+
41
+ service = Service.new
42
+ service.subscribe(Listener)
43
+ service.on(:action_executed) do |status|
44
+ puts "Action executed: #{ status }"
45
+ end
46
+ service.action
47
+ ```
48
+
49
+ There is also `Spine::Hub::Repeater`, which repeats subscribed events. If you
50
+ need to get more control over subscriber event handling, implement notify
51
+ method:
52
+
53
+ ```ruby
54
+ class MySubscriber
55
+ def notify(event, *arguments)
56
+ # Handle events
57
+ end
58
+ end
59
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/spine-hub.rb ADDED
@@ -0,0 +1 @@
1
+ require 'spine/hub'
data/lib/spine/hub.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Spine
2
+ module Hub
3
+ autoload :Publisher, 'spine/hub/publisher'
4
+ autoload :Subscriber, 'spine/hub/subscriber'
5
+ autoload :Repeater, 'spine/hub/repeater'
6
+
7
+ module Subscriptions
8
+ autoload :Closure, 'spine/hub/subscriptions/closure'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,42 @@
1
+ module Spine
2
+ module Hub
3
+ module Publisher
4
+ def subscribers
5
+ @subscribers ||= []
6
+ end
7
+
8
+ # Adds subscribers.
9
+ #
10
+ # ==== Attributes
11
+ # * +subscribers+ - List of addable subscribers.
12
+ def subscribe(*subscribers)
13
+ @subscribers ||= []
14
+ @subscribers += subscribers
15
+ end
16
+
17
+ # Publishes event to subscribers.
18
+ #
19
+ # ==== Attributes
20
+ # * +event+ - Name of event.
21
+ # * +arguments+ - List of event arguments.
22
+ def publish(event, *arguments)
23
+ subscribers.each do |subscriber|
24
+ if subscriber.respond_to?(:notify)
25
+ subscriber.notify(event, *arguments)
26
+ end
27
+ end
28
+ end
29
+
30
+ # Adds block subscriber.
31
+ #
32
+ # ==== Arguments
33
+ # * +event+ - Subscriber triggering event.
34
+ def on(event, &block)
35
+ if block_given?
36
+ subscribe(Subscriptions::Closure.new(event, block))
37
+ end
38
+ self
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ module Spine
2
+ module Hub
3
+ module Repeater
4
+ include Publisher
5
+
6
+ # Republishes the event.
7
+ #
8
+ # ==== Attributes
9
+ # * +event+ - Name of event.
10
+ # * +arguments+ - Arguments for event.
11
+ def notify(event, *arguments)
12
+ publish(event, *arguments)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Spine
2
+ module Hub
3
+ module Subscriber
4
+ # Notifies subscriber about event. It delegates processing that event
5
+ # to on_<event> method with specified arguments.
6
+ #
7
+ # ==== Attributes
8
+ # * +event+ - Name of event.
9
+ # * +arguments+ - Arguments for event.
10
+ def notify(event, *arguments)
11
+ method_name = "on_#{ event }".to_sym
12
+ send(method_name, *arguments) if respond_to?(method_name)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ module Spine
2
+ module Hub
3
+ module Subscriptions
4
+ # Subscriber for blocks.
5
+ #
6
+ # == Requirements
7
+ # * +event+ - Subscriber triggering event.
8
+ # * +action+ - Block to execute.
9
+ class Closure
10
+ attr_reader :trigger, :action
11
+
12
+ def initialize(event, action)
13
+ @trigger = event
14
+ @action = action
15
+ end
16
+
17
+ # Notifies subscriber about event. When event matches with triggering
18
+ # event it executes action.
19
+ #
20
+ # ==== Attributes
21
+ # * +event+ - Name of event.
22
+ # * +arguments+ - Arguments for event.
23
+ def notify(event, *arguments)
24
+ action.call(*arguments) if trigger?(event)
25
+ end
26
+
27
+ # Returns true when subscriber is triggered by specified event.
28
+ #
29
+ # ==== Attributes
30
+ # * +event+ - Name of event.
31
+ def trigger?(event)
32
+ trigger == event
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Spine
2
+ module Hub
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ RSpec.configure do |config|
2
+ config.filter_run :focus
3
+ config.run_all_when_everything_filtered = true
4
+
5
+ if config.files_to_run.one?
6
+ config.default_formatter = 'doc'
7
+ end
8
+
9
+ config.order = :random
10
+
11
+ Kernel.srand config.seed
12
+
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.syntax = :expect
15
+ end
16
+
17
+ config.mock_with :rspec do |mocks|
18
+ mocks.syntax = :expect
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+ require 'spine/hub'
2
+
3
+ module Spine
4
+ module Hub
5
+ describe Publisher do
6
+ subject { double.extend(Publisher) }
7
+
8
+ let(:subscriber) { double }
9
+
10
+ it 'has subscribers' do
11
+ expect(subject.subscribers).to eq([])
12
+ end
13
+
14
+ it 'adds subscriber' do
15
+ subject.subscribe(subscriber)
16
+ expect(subject.subscribers).to include(subscriber)
17
+ end
18
+
19
+ it 'publishes event to subscriber' do
20
+ expect(subscriber).to receive(:notify).with(:test, 1)
21
+
22
+ subject.subscribe(subscriber)
23
+ subject.publish(:test, 1)
24
+ end
25
+
26
+ it 'published event without subscribers' do
27
+ subject.publish(:test, 1)
28
+ end
29
+
30
+ context 'when subscribing to event with closure' do
31
+ it 'adds closure subscription' do
32
+ subject.on(:test) do
33
+ end
34
+ expect(subject.subscribers.size).to eq(1)
35
+ end
36
+
37
+ it 'skips adding empty closure subscription' do
38
+ subject.on(:test)
39
+ expect(subject.subscribers.size).to eq(0)
40
+ end
41
+
42
+ it 'publishes event to closure subscription' do
43
+ published_value = nil
44
+ subject.on(:test) do |value|
45
+ published_value = value
46
+ end
47
+
48
+ subject.publish(:test, 1)
49
+ expect(published_value).to eq(1)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,14 @@
1
+ require 'spine/hub'
2
+
3
+ module Spine
4
+ module Hub
5
+ describe Subscriber do
6
+ subject { double.extend(Subscriber) }
7
+
8
+ it 'delegates notification to event handler' do
9
+ expect(subject).to receive(:on_test).with(1)
10
+ subject.notify(:test, 1)
11
+ end
12
+ end
13
+ end
14
+ end
data/spine-hub.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spine/hub/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spine-hub"
8
+ spec.version = Spine::Hub::VERSION
9
+ spec.authors = ["TOGGL LLC"]
10
+ spec.email = ["support@toggl.com"]
11
+ spec.summary = 'Spine hub for Ruby applications.'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/rspine/hub'
14
+ spec.license = 'BSD-3-Clause'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', "~> 1.7"
22
+ spec.add_development_dependency 'rake', "~> 10.0"
23
+ spec.add_development_dependency 'rspec', "~> 3.2"
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spine-hub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOGGL LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: ''
56
+ email:
57
+ - support@toggl.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - lib/spine-hub.rb
70
+ - lib/spine/hub.rb
71
+ - lib/spine/hub/publisher.rb
72
+ - lib/spine/hub/repeater.rb
73
+ - lib/spine/hub/subscriber.rb
74
+ - lib/spine/hub/subscriptions/closure.rb
75
+ - lib/spine/hub/version.rb
76
+ - spec/config/default.rb
77
+ - spec/spine/hub/publisher_spec.rb
78
+ - spec/spine/hub/subscriber_spec.rb
79
+ - spine-hub.gemspec
80
+ homepage: https://github.com/rspine/hub
81
+ licenses:
82
+ - BSD-3-Clause
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.5
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Spine hub for Ruby applications.
104
+ test_files:
105
+ - spec/config/default.rb
106
+ - spec/spine/hub/publisher_spec.rb
107
+ - spec/spine/hub/subscriber_spec.rb