abstractivator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -10
- data/Rakefile +10 -1
- data/lib/abstractivator/event.rb +31 -0
- data/lib/abstractivator/version.rb +1 -1
- data/spec/lib/abstractivator/event_spec.rb +95 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0f97f449a6f91eaac8ae39f200348648279e81e
|
4
|
+
data.tar.gz: 13f4f30033ab6cd63d57d837cbea7250ae5e183d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b9565dcf535296a0c0434e3fadc2409a82ca5b8991193b6f5dfec81d3e0c17bb420c59546bedbb6df1ded02ba731e310eaefdcfdd49bb9d5c331c5435d42293
|
7
|
+
data.tar.gz: 642b58f09048ce0c2897c8b6a43dd4ed28db10380e6d9754c1cbf14792351f78e7ae536e459d82aa7c707385713bfd3268a960046bb35f74ec493eddde624047
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Abstractivator
|
2
2
|
|
3
|
-
|
3
|
+
A collection of useful ruby code
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,12 +18,4 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
## Contributing
|
24
|
-
|
25
|
-
1. Fork it ( https://github.com/[my-github-username]/abstractivator/fork )
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create a new Pull Request
|
21
|
+
See specs for examples.
|
data/Rakefile
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Abstractivator
|
2
|
+
module Event
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
module ClassMethods
|
7
|
+
def event(*names)
|
8
|
+
names.each do |name|
|
9
|
+
define_method("on_#{name}") do |&block|
|
10
|
+
__event_hooks[name] << block
|
11
|
+
proc { __event_hooks[name].delete(block) }
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method("do_#{name}") do |*args, **kws, &block|
|
15
|
+
__event_hooks[name].each do |hook|
|
16
|
+
if hook.parameters.map(&:first).include?(:key)
|
17
|
+
hook.call(*args, **kws, &block)
|
18
|
+
else
|
19
|
+
hook.call(*args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def __event_hooks
|
28
|
+
@__event_hooks ||= Hash.new { |h, k| h[k] = [] }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'abstractivator/event'
|
3
|
+
|
4
|
+
describe Abstractivator::Event do
|
5
|
+
|
6
|
+
class EventedObject
|
7
|
+
include Abstractivator::Event
|
8
|
+
event :arrived, :departed
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { EventedObject.new }
|
12
|
+
|
13
|
+
describe 'the "do" method' do
|
14
|
+
context 'when there are no hooks' do
|
15
|
+
it 'does nothing' do
|
16
|
+
subject.do_arrived
|
17
|
+
end
|
18
|
+
end
|
19
|
+
it 'calls the hooks in the order in which they were added' do
|
20
|
+
log = []
|
21
|
+
subject.on_arrived { log << 'hello' }
|
22
|
+
subject.on_arrived { log << 'hello again'}
|
23
|
+
expect(log).to eql []
|
24
|
+
subject.do_arrived
|
25
|
+
expect(log).to eql ['hello', 'hello again']
|
26
|
+
end
|
27
|
+
it 'passes arguments to the hooks' do
|
28
|
+
observed_args = nil
|
29
|
+
subject.on_arrived { |*args| observed_args = args }
|
30
|
+
subject.do_arrived(2, 3)
|
31
|
+
expect(observed_args).to eql [2, 3]
|
32
|
+
end
|
33
|
+
it 'passes keyword arguments to the hooks' do
|
34
|
+
observed_keywords = nil
|
35
|
+
subject.on_arrived { |a: nil, b: nil| observed_keywords = [a, b] }
|
36
|
+
subject.do_arrived(a: 2, b: 3)
|
37
|
+
expect(observed_keywords).to eql [2, 3]
|
38
|
+
end
|
39
|
+
it 'passes the block to the hooks' do
|
40
|
+
manifest = {1 => 'Bob', 2 => 'Fred'}
|
41
|
+
greeting = nil
|
42
|
+
subject.on_arrived { |id, &greet| greet.call(manifest[id]) }
|
43
|
+
subject.do_arrived(1) { |name| greeting = "Hello, #{name}" }
|
44
|
+
expect(greeting).to eql 'Hello, Bob'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'the "on" method' do
|
49
|
+
it 'returns an unhook procedure' do
|
50
|
+
log = []
|
51
|
+
unhook = subject.on_arrived { log << 'hello' }
|
52
|
+
subject.do_arrived
|
53
|
+
expect(log).to eql %w(hello)
|
54
|
+
|
55
|
+
unhook.call
|
56
|
+
subject.do_arrived
|
57
|
+
expect(log).to eql %w(hello)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'the unhook procedure' do
|
62
|
+
it 'returns the initial hook procedure' do
|
63
|
+
log = []
|
64
|
+
unhook = subject.on_arrived { log << 'hello' }
|
65
|
+
subject.do_arrived
|
66
|
+
expect(log).to eql %w(hello)
|
67
|
+
|
68
|
+
hook = unhook.call
|
69
|
+
subject.on_arrived(&hook)
|
70
|
+
subject.do_arrived
|
71
|
+
expect(log).to eql %w(hello hello)
|
72
|
+
end
|
73
|
+
it 'returns nil if called more than once' do
|
74
|
+
unhook = subject.on_arrived { log << 'hello' }
|
75
|
+
expect(unhook.call).to be_a Proc
|
76
|
+
expect(unhook.call).to be nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'hooks are independent' do
|
81
|
+
arrived_list = []
|
82
|
+
departed_list = []
|
83
|
+
subject.on_arrived { |who| arrived_list << who }
|
84
|
+
subject.on_departed { |who| departed_list << who }
|
85
|
+
|
86
|
+
subject.do_arrived('Fred')
|
87
|
+
expect(arrived_list).to eql %w(Fred)
|
88
|
+
expect(departed_list).to eql []
|
89
|
+
|
90
|
+
subject.do_departed('Bob')
|
91
|
+
expect(arrived_list).to eql %w(Fred)
|
92
|
+
expect(departed_list).to eql %w(Bob)
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abstractivator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Winton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/abstractivator/cons.rb
|
101
101
|
- lib/abstractivator/enum.rb
|
102
102
|
- lib/abstractivator/enumerable_ext.rb
|
103
|
+
- lib/abstractivator/event.rb
|
103
104
|
- lib/abstractivator/proc_ext.rb
|
104
105
|
- lib/abstractivator/schedule.rb
|
105
106
|
- lib/abstractivator/schedule/schedule.rb
|
@@ -116,6 +117,7 @@ files:
|
|
116
117
|
- spec/lib/abstractivator/cons_spec.rb
|
117
118
|
- spec/lib/abstractivator/enum_spec.rb
|
118
119
|
- spec/lib/abstractivator/enumerable_ext_spec.rb
|
120
|
+
- spec/lib/abstractivator/event_spec.rb
|
119
121
|
- spec/lib/abstractivator/proc_ext_spec.rb
|
120
122
|
- spec/lib/abstractivator/schedule/em_util.rb
|
121
123
|
- spec/lib/abstractivator/schedule/schedule_runner_spec.rb
|
@@ -154,6 +156,7 @@ test_files:
|
|
154
156
|
- spec/lib/abstractivator/cons_spec.rb
|
155
157
|
- spec/lib/abstractivator/enum_spec.rb
|
156
158
|
- spec/lib/abstractivator/enumerable_ext_spec.rb
|
159
|
+
- spec/lib/abstractivator/event_spec.rb
|
157
160
|
- spec/lib/abstractivator/proc_ext_spec.rb
|
158
161
|
- spec/lib/abstractivator/schedule/em_util.rb
|
159
162
|
- spec/lib/abstractivator/schedule/schedule_runner_spec.rb
|