flirt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b120f2410701ad2d63d29cc453c273021a5a23dc
4
+ data.tar.gz: da528cc1e2678262357854934f86064e723a6568
5
+ SHA512:
6
+ metadata.gz: 8ebacbf7408b4e5d48702204e9b16490c4e1ef1e4810f56ae0940d6e9af7ea3cd03e47f01c56cc67cec8eaf63130dbfa2aa1e5965414719482ae60b30c425949
7
+ data.tar.gz: 00232c11f40b4b5051d679c95af55e4a6f974790befbd511acd99a9777d67b29f52df01136cde92a2cdd4f707d37211f72bcc18f7bce9a81223e05b72881c594
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flirt.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 chemica
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # Flirt
2
+
3
+ This Ruby gem is a brutally simple take on the observer pattern.
4
+
5
+ Flirt acts as a single point to which events can be sent and listeners
6
+ can be registered, in order to promote extreme decoupling between components.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'flirt'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```ruby
20
+ $ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```ruby
26
+ $ gem install flirt
27
+ ```
28
+
29
+
30
+ ## Usage
31
+
32
+ To publish/broadcast an event:
33
+
34
+ ```ruby
35
+ event_data = { fruit: "apple" }
36
+ Flirt.broadcast :picked, event_data
37
+ ```
38
+
39
+ Or:
40
+
41
+ ```ruby
42
+ Flirt.publish :picked, event_data
43
+ ```
44
+
45
+ (These two versions are aliases)
46
+
47
+ To subscribe:
48
+
49
+ ```ruby
50
+ class MyListener
51
+ def initialize
52
+ Flirt.subscribe self, :picked, with: :picked_callback
53
+ # or the alias
54
+ Flirt.listen self, :picked, with: :picked_callback
55
+ end
56
+
57
+ def picked_callback(event_data)
58
+ puts "The #{event_data[:fruit]} has been picked"
59
+ end
60
+ end
61
+ ```
62
+
63
+ Sytactic sugar for subscription has been provided in the form of a module:
64
+
65
+ ```ruby
66
+ class MyListener
67
+ include Flirt::Listener
68
+
69
+ def initialize
70
+ subscribe_to :picked, with: :picked_callback
71
+ # or the alias
72
+ listen_to :picked, with: :picked_callback
73
+ end
74
+
75
+ def picked_callback(event_data)
76
+ puts "The #{event_data[:fruit]} has been picked"
77
+ end
78
+ end
79
+ ```
80
+
81
+ or even:
82
+
83
+ ```ruby
84
+ class MyListener
85
+ extend Flirt::Listener
86
+
87
+ subscribe_to :picked, with: :picked_callback
88
+ # or the alias
89
+ listen_to :picked, with: :picked_callback
90
+
91
+ def self.picked_callback(event_data)
92
+ puts "The #{event_data[:fruit]} has been picked"
93
+ end
94
+ end
95
+ ```
96
+
97
+ Flirt defaults to 'enabled'. Switch Flirt off:
98
+
99
+ ```ruby
100
+ Flirt.disable
101
+ ```
102
+
103
+ And back on again:
104
+
105
+ ```ruby
106
+ Flirt.enable
107
+ ```
108
+
109
+ TODO: Disable only a set of events:
110
+
111
+ ```ruby
112
+ Flirt.disable only: [:pancake_cooked, :picked]
113
+ ```
114
+
115
+ TODO: Enable only a set of events:
116
+
117
+ ```ruby
118
+ Flirt.enable only: [:topping_added, :pancake_flipped]
119
+ ```
120
+
121
+ Enabled status affects broadcast/publish, listeners can still be added and will be
122
+ remembered. No listeners will be removed.
123
+
124
+ Clear all listeners
125
+
126
+ ```ruby
127
+ Flirt.clear
128
+ ```
129
+
130
+ ## Contributing
131
+
132
+ 1. Fork it ( https://github.com/[my-github-username]/flirt/fork )
133
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
134
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
135
+ 4. Push to the branch (`git push origin my-new-feature`)
136
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/flirt.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 'flirt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flirt"
8
+ spec.version = Flirt::VERSION
9
+ spec.authors = ["Benjamin Randles-Dunkley"]
10
+ spec.email = ["ben@chemica.co.uk"]
11
+ spec.summary = %q{ A brutally simple take on the observer pattern. }
12
+ spec.description = %q{ Provides a single point for the publication and subscription of events, promoting extreme decoupling. }
13
+ spec.homepage = "https://github.com/chemica/flirt"
14
+ spec.license = "MIT"
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
@@ -0,0 +1,16 @@
1
+ # Represents a single callback. Contains knowledge of the callback name and object
2
+ # and contains a method for calling the callback.
3
+
4
+ module Flirt
5
+ class Callback
6
+ attr_accessor :object, :callback_name
7
+ def initialize(opts = {})
8
+ self.callback_name = opts.fetch(:callback_name)
9
+ self.object = opts.fetch(:object)
10
+ end
11
+
12
+ def call(event_data)
13
+ object.send callback_name, event_data
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ # Syntactic sugar for adding a listener. Use include or extend to
2
+ # add listen/subscribe behaviour to your class instance
3
+ # or class body.
4
+
5
+ # class MyListener
6
+ # include Flirt::Listener
7
+ #
8
+ # def initialize
9
+ # subscribe_to :picked, with: :picked_callback
10
+ # # or the alias
11
+ # listen_to :picked, with: :picked_callback
12
+ # end
13
+ #
14
+ # def picked_callback(event_data)
15
+ # puts "The #{event_data[:fruit]} has been picked"
16
+ # end
17
+ # end
18
+ #
19
+ # or:
20
+ #
21
+ # class MyListener
22
+ # extend Flirt::Listener
23
+ #
24
+ # subscribe_to :picked, with: :picked_callback
25
+ # # or the alias
26
+ # listen_to :picked, with: :picked_callback
27
+ #
28
+ # def self.picked_callback(event_data)
29
+ # puts "The #{event_data[:fruit]} has been picked"
30
+ # end
31
+ # end
32
+ #
33
+
34
+ module Flirt
35
+ module Listener
36
+ def subscribe_to(event_name, opts = {})
37
+ raise ArgumentError.new("You must pass a callback") unless opts[:with].is_a? Symbol
38
+ Flirt.subscribe self, event_name, opts
39
+ end
40
+ alias_method :listen_to, :subscribe_to
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Flirt
2
+ VERSION = "0.0.1"
3
+ end
data/lib/flirt.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "flirt/version"
2
+ require "flirt/callback"
3
+ require "flirt/listener"
4
+ # The Flirt module provides the main interface for dealing with Flirt.
5
+ #
6
+
7
+ module Flirt
8
+
9
+ class << self
10
+
11
+ def broadcast(event_name, event_data)
12
+ return if disabled
13
+ raise ArgumentError.new("Event name must be a symbol") unless event_name.is_a? Symbol
14
+ (callbacks[event_name] || []).each do |callback|
15
+ callback.call(event_data)
16
+ end
17
+ end
18
+ alias_method :publish, :broadcast
19
+
20
+ def subscribe(object, event_name, options = {})
21
+ raise ArgumentError.new("You must pass a callback") unless options[:with].is_a? Symbol
22
+ raise ArgumentError.new("You must pass an object") if object.nil?
23
+ raise ArgumentError.new("You must pass an event name") unless event_name.is_a? Symbol
24
+ callback = Flirt::Callback.new object: object,
25
+ callback_name: options[:with]
26
+ add_callback(event_name, callback)
27
+ end
28
+ alias_method :listen, :subscribe
29
+
30
+ def enable
31
+ self.disabled = false
32
+ end
33
+
34
+ def disable
35
+ self.disabled = true
36
+ end
37
+
38
+ def clear
39
+ @callbacks = {}
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :callbacks
45
+ attr_accessor :disabled
46
+
47
+ def callbacks
48
+ @callbacks ||= {}
49
+ end
50
+
51
+ def add_callback(event_name, callback)
52
+ callbacks[event_name] ||= []
53
+ callbacks[event_name] << callback
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flirt::Callback do
4
+ before(:each) do
5
+ Flirt.enable
6
+ end
7
+
8
+ describe "when instantiated with an object and callback" do
9
+
10
+ let(:listener) { Object.new }
11
+ let(:callback_name) { :call_me }
12
+
13
+ let(:callback) { Flirt::Callback.new object: listener, callback_name: callback_name }
14
+
15
+ it "sets the object" do
16
+ expect(callback.object).to eq(listener)
17
+ end
18
+
19
+ it "sets the callback" do
20
+ expect(callback.callback_name).to eq(callback_name)
21
+ end
22
+
23
+ describe "when called" do
24
+
25
+ let(:event_data) { {event: :data} }
26
+
27
+ it "calls the callback with the event data" do
28
+ expect(listener).to receive(callback_name).with(event_data)
29
+ callback.call(event_data)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'flirt/flirt_test_classes'
3
+
4
+ describe Flirt::Listener do
5
+ before(:each) do
6
+ Flirt.enable
7
+ end
8
+
9
+ describe "when included in an instance" do
10
+ let(:event) { :grabbed_coin }
11
+ let(:callback_name) { :increase_score }
12
+ let(:event_data) { { value: 100 } }
13
+ let(:instance_subscriber) { TestInstanceSubscriber.new event, callback_name }
14
+ let(:instance_listener) { TestInstanceListener.new event, callback_name }
15
+
16
+ it "subscribes to an event" do
17
+ expect(instance_subscriber).to receive(callback_name).with(event_data)
18
+ Flirt.broadcast event, event_data
19
+ end
20
+
21
+ it "listens to an event" do
22
+ expect(instance_listener).to receive(callback_name).with(event_data)
23
+ Flirt.broadcast event, event_data
24
+ end
25
+ end
26
+
27
+ describe "when included in a class" do
28
+ let(:event) { :event_name }
29
+ let(:callback_name) { :callback_name }
30
+ let(:event_data) { { value: 100 } }
31
+ let(:class_subscriber) { TestClassSubscriber }
32
+ let(:class_listener) { TestClassListener }
33
+
34
+ # Because rspec has to clear the Flirt callbacks for each test,
35
+ # we need to require the test classes after rspec init.
36
+
37
+ it "subscribes to an event" do
38
+ require 'flirt/test_class_subscriber'
39
+ expect(class_subscriber).to receive(callback_name).with(event_data)
40
+ Flirt.broadcast event, event_data
41
+ end
42
+
43
+ it "listens to an event" do
44
+ require 'flirt/test_class_listener'
45
+ expect(class_listener).to receive(callback_name).with(event_data)
46
+ Flirt.broadcast event, event_data
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'flirt/flirt_test_classes'
3
+
4
+ describe Flirt do
5
+
6
+ describe "with a single :pancake_fried subscriber" do
7
+
8
+ let(:response) { { topping: "cream" } }
9
+ let(:event) { :pancake_fried }
10
+ let(:wrong_event) { :spud_baked }
11
+ let!(:listener) { TestListener.new(event) }
12
+
13
+ it "listens to the correct broadcast event" do
14
+ Flirt.broadcast event, response
15
+ expect(listener.responded).to eq(response)
16
+ end
17
+
18
+ it "listens to the correct publish event" do
19
+ Flirt.publish event, response
20
+ expect(listener.responded).to eq(response)
21
+ end
22
+
23
+ it "doesn't listen to the wrong broadcast event" do
24
+ Flirt.broadcast wrong_event, response
25
+ expect(listener.responded).to be_nil
26
+ end
27
+
28
+ it "doesn't listen to the wrong publish event" do
29
+ Flirt.broadcast wrong_event, response
30
+ expect(listener.responded).to be_nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+
2
+ class TestSubscriber
3
+
4
+ attr_accessor :responded
5
+
6
+ def initialize(event)
7
+ Flirt.subscribe self, event, with: :respond
8
+ end
9
+
10
+ def respond(event_data)
11
+ self.responded = event_data
12
+ end
13
+ end
14
+
15
+
16
+ class TestListener
17
+
18
+ attr_accessor :responded
19
+
20
+ def initialize(event)
21
+ Flirt.listen self, event, with: :respond
22
+ end
23
+
24
+ def respond(event_data)
25
+ self.responded = event_data
26
+ end
27
+ end
28
+
29
+ class TestInstanceSubscriber
30
+ include Flirt::Listener
31
+
32
+ def initialize(event, callback)
33
+ subscribe_to event, with: callback
34
+ end
35
+ end
36
+
37
+ class TestInstanceListener
38
+ include Flirt::Listener
39
+
40
+ attr_accessor :responded
41
+
42
+ def initialize(event, callback)
43
+ listen_to event, with: callback
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ class TestClassListener
2
+ extend Flirt::Listener
3
+
4
+ class << self
5
+ attr_accessor :responded
6
+ end
7
+
8
+ listen_to :event_name, with: :callback_name
9
+ end
@@ -0,0 +1,9 @@
1
+ class TestClassSubscriber
2
+ extend Flirt::Listener
3
+
4
+ class << self
5
+ attr_accessor :responded
6
+ end
7
+
8
+ subscribe_to :event_name, with: :callback_name
9
+ end
@@ -0,0 +1,27 @@
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
+
8
+ require 'rubygems'
9
+ require 'bundler/setup'
10
+ Bundler.setup
11
+
12
+ require 'flirt'
13
+
14
+ RSpec.configure do |config|
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ config.before(:each) do
19
+ Flirt.clear
20
+ end
21
+
22
+ # Run specs in random order to surface order dependencies. If you find an
23
+ # order dependency and want to debug it, you can fix the order by providing
24
+ # the seed, which is printed after each run.
25
+ # --seed 1234
26
+ config.order = 'random'
27
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flirt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Randles-Dunkley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-28 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: " Provides a single point for the publication and subscription of events,
56
+ promoting extreme decoupling. "
57
+ email:
58
+ - ben@chemica.co.uk
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - flirt.gemspec
70
+ - lib/flirt.rb
71
+ - lib/flirt/callback.rb
72
+ - lib/flirt/listener.rb
73
+ - lib/flirt/version.rb
74
+ - spec/flirt/flirt_callback_spec.rb
75
+ - spec/flirt/flirt_listener_spec.rb
76
+ - spec/flirt/flirt_spec.rb
77
+ - spec/flirt/flirt_test_classes.rb
78
+ - spec/flirt/test_class_listener.rb
79
+ - spec/flirt/test_class_subscriber.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/chemica/flirt
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A brutally simple take on the observer pattern.
105
+ test_files:
106
+ - spec/flirt/flirt_callback_spec.rb
107
+ - spec/flirt/flirt_listener_spec.rb
108
+ - spec/flirt/flirt_spec.rb
109
+ - spec/flirt/flirt_test_classes.rb
110
+ - spec/flirt/test_class_listener.rb
111
+ - spec/flirt/test_class_subscriber.rb
112
+ - spec/spec_helper.rb