jace 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +54 -5
- data/.rubocop.yml +9 -0
- data/Dockerfile +2 -2
- data/README.md +38 -5
- data/config/check_specs.yml +3 -1
- data/config/yardstick.yml +11 -4
- data/jace.gemspec +13 -13
- data/jace.jpg +0 -0
- data/lib/jace/dispatcher.rb +54 -0
- data/lib/jace/executer.rb +126 -0
- data/lib/jace/registry.rb +78 -0
- data/lib/jace/version.rb +2 -2
- data/lib/jace.rb +5 -2
- data/spec/integration/readme/jace/registry_spec.rb +30 -0
- data/spec/integration/yard/jace/registry_spec.rb +46 -0
- data/spec/lib/jace/dispatcher_spec.rb +188 -0
- data/spec/lib/jace/executer_spec.rb +178 -0
- data/spec/lib/jace/registry_spec.rb +246 -0
- data/spec/support/models/context.rb +5 -0
- data/spec/support/models/person.rb +19 -0
- data/spec/support/models/some_context.rb +15 -0
- metadata +41 -31
- data/spec/support/fixture_helpers.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba76568b768021abe49c12d16b285e9f9ac0d3d631074a1aa8e8d1619d8d397f
|
4
|
+
data.tar.gz: 17a86c3dbe926f9e2fd111eb6c67190421c8d455ffc35c1e9c8b4dab40c80070
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f156ad1797a018d5c3d16c114156c1613a21315b6707c398471702d90dc7d692a89cf53b14b66631ed33d80a28557eca9e123413339497cff6b8d441f0a9b9d7
|
7
|
+
data.tar.gz: f16a65b15e85ce825edbe8c105ee2a8bb1575831be2e83faa83d8decf64f2bd11fffea311841c4b82420ce551a208819c3ee231ac0dc32567cfdbbdfc4a8e315
|
data/.circleci/config.yml
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
version: 2
|
2
|
+
workflows:
|
3
|
+
version: 2
|
4
|
+
test-and-build:
|
5
|
+
jobs:
|
6
|
+
- test:
|
7
|
+
filters:
|
8
|
+
tags:
|
9
|
+
only: /.*/
|
10
|
+
- checks:
|
11
|
+
filters:
|
12
|
+
tags:
|
13
|
+
only: /.*/
|
14
|
+
- build-and-release:
|
15
|
+
requires: [test, checks]
|
16
|
+
filters:
|
17
|
+
tags:
|
18
|
+
only: /\d+\.\d+\.\d+/
|
19
|
+
branches:
|
20
|
+
only:
|
21
|
+
- master
|
2
22
|
jobs:
|
3
|
-
|
23
|
+
test:
|
4
24
|
docker:
|
5
|
-
- image: darthjee/
|
25
|
+
- image: darthjee/circleci_ruby_270:1.1.0
|
6
26
|
environment:
|
7
27
|
PROJECT: jace
|
8
28
|
steps:
|
@@ -16,12 +36,22 @@ jobs:
|
|
16
36
|
- run:
|
17
37
|
name: RSpec
|
18
38
|
command: bundle exec rspec
|
19
|
-
- run:
|
20
|
-
name: Rubocop
|
21
|
-
command: rubocop
|
22
39
|
- run:
|
23
40
|
name: Coverage Test Report
|
24
41
|
command: cc-test-reporter after-build --exit-code $?
|
42
|
+
checks:
|
43
|
+
docker:
|
44
|
+
- image: darthjee/circleci_ruby_270:1.1.0
|
45
|
+
environment:
|
46
|
+
PROJECT: jace
|
47
|
+
steps:
|
48
|
+
- checkout
|
49
|
+
- run:
|
50
|
+
name: Bundle Install
|
51
|
+
command: bundle install
|
52
|
+
- run:
|
53
|
+
name: Rubocop
|
54
|
+
command: rubocop
|
25
55
|
- run:
|
26
56
|
name: Yardstick coverage check
|
27
57
|
command: bundle exec rake verify_measurements
|
@@ -34,3 +64,22 @@ jobs:
|
|
34
64
|
- run:
|
35
65
|
name: Check unit tests
|
36
66
|
command: check_specs
|
67
|
+
build-and-release:
|
68
|
+
docker:
|
69
|
+
- image: darthjee/circleci_ruby_270:1.1.0
|
70
|
+
environment:
|
71
|
+
PROJECT: jace
|
72
|
+
steps:
|
73
|
+
- checkout
|
74
|
+
- run:
|
75
|
+
name: Bundle Install
|
76
|
+
command: bundle install
|
77
|
+
- run:
|
78
|
+
name: Signin
|
79
|
+
command: build_gem.sh signin
|
80
|
+
- run:
|
81
|
+
name: Build Gem
|
82
|
+
command: build_gem.sh build
|
83
|
+
- run:
|
84
|
+
name: Push Gem
|
85
|
+
command: build_gem.sh push
|
data/.rubocop.yml
CHANGED
@@ -36,6 +36,15 @@ RSpec/NestedGroups:
|
|
36
36
|
Exclude:
|
37
37
|
- 'spec/integration/yard/**/*.rb'
|
38
38
|
|
39
|
+
RSpec/InstanceVariable:
|
40
|
+
Exclude:
|
41
|
+
- 'spec/lib/jace/executer_spec.rb'
|
42
|
+
- 'spec/lib/jace/dispatcher_spec.rb'
|
43
|
+
|
44
|
+
Naming/MemoizedInstanceVariableName:
|
45
|
+
Exclude:
|
46
|
+
- 'spec/support/models/person.rb'
|
47
|
+
|
39
48
|
Style/HashEachMethods:
|
40
49
|
Enabled: true
|
41
50
|
|
data/Dockerfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Jace
|
2
|
+
====
|
3
3
|
[![Code Climate](https://codeclimate.com/github/darthjee/jace/badges/gpa.svg)](https://codeclimate.com/github/darthjee/jace)
|
4
4
|
[![Test Coverage](https://codeclimate.com/github/darthjee/jace/badges/coverage.svg)](https://codeclimate.com/github/darthjee/jace/coverage)
|
5
5
|
[![Issue Count](https://codeclimate.com/github/darthjee/jace/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/jace)
|
@@ -11,23 +11,56 @@ Sinclair
|
|
11
11
|
|
12
12
|
Yard Documentation
|
13
13
|
-------------------
|
14
|
-
[https://www.rubydoc.info/gems/jace/0.0
|
14
|
+
[https://www.rubydoc.info/gems/jace/0.1.0](https://www.rubydoc.info/gems/jace/0.1.0)
|
15
|
+
|
16
|
+
Jace is designed to have a semi event driven development
|
17
|
+
|
18
|
+
Using `Jace::Registry`, event handlers can be registered to events, and when an event
|
19
|
+
is triggered, the block that triggers it is given to Jace, which will triger, around it,
|
20
|
+
the +before+ and +after+ handlers
|
15
21
|
|
16
22
|
Installation
|
17
23
|
---------------
|
18
24
|
|
19
25
|
- Install it
|
20
26
|
|
21
|
-
```
|
27
|
+
```bash
|
22
28
|
gem install jace
|
23
29
|
```
|
24
30
|
|
25
31
|
- Or add Sinclair to your `Gemfile` and `bundle install`:
|
26
32
|
|
27
|
-
```
|
33
|
+
```bash
|
28
34
|
gem 'jace'
|
29
35
|
```
|
30
36
|
|
31
37
|
```bash
|
32
38
|
bundle install jace
|
33
39
|
```
|
40
|
+
|
41
|
+
Using
|
42
|
+
-----
|
43
|
+
|
44
|
+
Initialize a registry, register event handlers nad trigger events
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class SomeContext
|
48
|
+
def do_something(instant)
|
49
|
+
puts "doing something #{instant}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
registry = described_class.new
|
54
|
+
context = SomeContext.new
|
55
|
+
|
56
|
+
registry.register(:the_event) { do_something(:after) }
|
57
|
+
registry.register(:the_event, :before) { do_something(:before) }
|
58
|
+
|
59
|
+
registry.trigger(:the_event, context) do
|
60
|
+
context.do_something(:middle)
|
61
|
+
end
|
62
|
+
|
63
|
+
# puts 'doing something before',
|
64
|
+
# puts 'doing something middle',
|
65
|
+
# puts 'doing something after'
|
66
|
+
```
|
data/config/check_specs.yml
CHANGED
data/config/yardstick.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
threshold:
|
1
|
+
threshold: 84.6
|
2
2
|
require_exact_threshold: false
|
3
3
|
rules:
|
4
4
|
ApiTag::Presence:
|
@@ -15,13 +15,20 @@ rules:
|
|
15
15
|
exclude: []
|
16
16
|
ExampleTag:
|
17
17
|
enabled: true
|
18
|
-
exclude:
|
18
|
+
exclude:
|
19
|
+
- Jace::Registry#registry
|
20
|
+
- Jace::Registry#events
|
19
21
|
ReturnTag:
|
20
22
|
enabled: true
|
21
|
-
exclude:
|
23
|
+
exclude:
|
24
|
+
- Jace::Executer#before
|
25
|
+
- Jace::Executer#after
|
22
26
|
Summary::Presence:
|
23
27
|
enabled: true
|
24
|
-
exclude:
|
28
|
+
exclude:
|
29
|
+
- Jace::Dispatcher#initialize
|
30
|
+
- Jace::Executer#initialize
|
31
|
+
- Jace::Registry#initialize
|
25
32
|
Summary::Length:
|
26
33
|
enabled: true
|
27
34
|
exclude: []
|
data/jace.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.authors = ['DarthJee']
|
11
11
|
gem.email = ['darthjee@gmail.com']
|
12
12
|
gem.homepage = 'https://github.com/darthjee/jace'
|
13
|
-
gem.description = ''
|
13
|
+
gem.description = 'Gem focused on events for meta-programing'
|
14
14
|
gem.summary = gem.description
|
15
15
|
gem.required_ruby_version = '>= 2.5.0'
|
16
16
|
|
@@ -19,20 +19,20 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.test_files = gem.files.grep(%r{^(test|gem|features)/})
|
20
20
|
gem.require_paths = ['lib']
|
21
21
|
|
22
|
-
gem.add_development_dependency 'bundler', '
|
23
|
-
gem.add_development_dependency 'pry', '0.
|
24
|
-
gem.add_development_dependency 'pry-nav', '0.
|
22
|
+
gem.add_development_dependency 'bundler', '2.3.20'
|
23
|
+
gem.add_development_dependency 'pry', '0.14.1'
|
24
|
+
gem.add_development_dependency 'pry-nav', '1.0.0'
|
25
25
|
gem.add_development_dependency 'rake', '13.0.1'
|
26
|
-
gem.add_development_dependency 'reek', '
|
27
|
-
gem.add_development_dependency 'rspec', '3.
|
28
|
-
gem.add_development_dependency 'rspec-core', '3.
|
29
|
-
gem.add_development_dependency 'rspec-expectations', '3.
|
30
|
-
gem.add_development_dependency 'rspec-mocks', '3.
|
31
|
-
gem.add_development_dependency 'rspec-support', '3.
|
26
|
+
gem.add_development_dependency 'reek', '6.0.3'
|
27
|
+
gem.add_development_dependency 'rspec', '3.11.0'
|
28
|
+
gem.add_development_dependency 'rspec-core', '3.11.0'
|
29
|
+
gem.add_development_dependency 'rspec-expectations', '3.11.0'
|
30
|
+
gem.add_development_dependency 'rspec-mocks', '3.11.1'
|
31
|
+
gem.add_development_dependency 'rspec-support', '3.11.0'
|
32
32
|
gem.add_development_dependency 'rubocop', '0.80.1'
|
33
33
|
gem.add_development_dependency 'rubocop-rspec', '1.38.1'
|
34
|
-
gem.add_development_dependency 'rubycritic', '4.
|
35
|
-
gem.add_development_dependency 'simplecov', '0.
|
36
|
-
gem.add_development_dependency 'yard', '0.9.
|
34
|
+
gem.add_development_dependency 'rubycritic', '4.7.0'
|
35
|
+
gem.add_development_dependency 'simplecov', '0.21.2'
|
36
|
+
gem.add_development_dependency 'yard', '0.9.27'
|
37
37
|
gem.add_development_dependency 'yardstick', '0.9.9'
|
38
38
|
end
|
data/jace.jpg
ADDED
Binary file
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jace
|
4
|
+
# @api private
|
5
|
+
# @author Darthjee
|
6
|
+
#
|
7
|
+
# Class responsible for dispatching the call of events
|
8
|
+
class Dispatcher
|
9
|
+
# @param before [Symbol,Proc,Array] all the methods / proc
|
10
|
+
# to be executed before block call
|
11
|
+
# @param after [Symbol,Proc,Array] all the methods / proc
|
12
|
+
# to be executed after block call
|
13
|
+
def initialize(before: [], after: [])
|
14
|
+
@before = [before].flatten.compact
|
15
|
+
@after = [after].flatten.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
# Dispatch the event call on a context
|
19
|
+
#
|
20
|
+
# @param context [Object] Object where the procs / methods
|
21
|
+
# will be called on
|
22
|
+
# @block [Proc] bloc to be performed between befores and afters
|
23
|
+
#
|
24
|
+
# @return [Object] result of block call
|
25
|
+
def dispatch(context, &block)
|
26
|
+
Executer.call(
|
27
|
+
before: before,
|
28
|
+
after: after,
|
29
|
+
context: context,
|
30
|
+
&block
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :before, :after
|
37
|
+
|
38
|
+
# @method before
|
39
|
+
# @private
|
40
|
+
# @api private
|
41
|
+
#
|
42
|
+
# Contains a list of event handlers to be called before
|
43
|
+
#
|
44
|
+
# @return [Array<Object>] list of handlers
|
45
|
+
|
46
|
+
# @method after
|
47
|
+
# @private
|
48
|
+
# @api private
|
49
|
+
#
|
50
|
+
# Contains a list of event handlers to be called after
|
51
|
+
#
|
52
|
+
# @return [Array<Object>] list of handlers
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jace
|
4
|
+
# @api private
|
5
|
+
# @author Dartjee
|
6
|
+
#
|
7
|
+
# Class responsible for executing a given block and triggering event
|
8
|
+
#
|
9
|
+
# The event trigger has phase before and after so all methods
|
10
|
+
# are called before block execution and after, before returning the result
|
11
|
+
class Executer
|
12
|
+
# Calls the execution
|
13
|
+
#
|
14
|
+
# @param before [Symbol,Proc,Array] all the methods / proc
|
15
|
+
# to be executed before block call
|
16
|
+
# @param after [Symbol,Proc,Array] all the methods / proc
|
17
|
+
# to be executed after block call
|
18
|
+
# @param context [Object] Object where the procs / methods
|
19
|
+
# will be called on
|
20
|
+
# @block [Proc] bloc to be performed between befores and afters
|
21
|
+
#
|
22
|
+
# @return [Object] result of block call
|
23
|
+
def self.call(before: [], after: [], context:, &block)
|
24
|
+
new(before, after, context, &block).call
|
25
|
+
end
|
26
|
+
|
27
|
+
# calls the execution
|
28
|
+
#
|
29
|
+
# @see .call
|
30
|
+
#
|
31
|
+
# @return (see .call)
|
32
|
+
def call
|
33
|
+
execute_actions(before)
|
34
|
+
result = block.call if block
|
35
|
+
execute_actions(after)
|
36
|
+
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# @private
|
43
|
+
#
|
44
|
+
# @param (see .call)
|
45
|
+
def initialize(before, after, context, &block)
|
46
|
+
@before = [before].flatten.compact
|
47
|
+
@after = [after].flatten.compact
|
48
|
+
@context = context
|
49
|
+
@block = block
|
50
|
+
end
|
51
|
+
|
52
|
+
attr_reader :before, :after, :context, :block
|
53
|
+
|
54
|
+
# @method before
|
55
|
+
# @private
|
56
|
+
# @api private
|
57
|
+
#
|
58
|
+
# Contains a list of event handlers to be called before
|
59
|
+
#
|
60
|
+
# @return (see Jace::Dispatcher#before)
|
61
|
+
|
62
|
+
# @method after
|
63
|
+
# @private
|
64
|
+
# @api private
|
65
|
+
#
|
66
|
+
# Contains a list of event handlers to be called after
|
67
|
+
#
|
68
|
+
# @return (see Jace::Dispatcher#after)
|
69
|
+
|
70
|
+
# @method context
|
71
|
+
# @private
|
72
|
+
# @api private
|
73
|
+
#
|
74
|
+
# context where the events handlers will be executed
|
75
|
+
#
|
76
|
+
# all the method calls inside the event handler will be evaluated
|
77
|
+
# from within the context
|
78
|
+
#
|
79
|
+
# @return [Object]
|
80
|
+
|
81
|
+
# @method block
|
82
|
+
# @private
|
83
|
+
# @api private
|
84
|
+
#
|
85
|
+
# block to be executed representing the event
|
86
|
+
#
|
87
|
+
# the block is executed after the +before+ handlers
|
88
|
+
# and before the +after+ handlers
|
89
|
+
#
|
90
|
+
# @return [Proc]
|
91
|
+
|
92
|
+
# @private
|
93
|
+
#
|
94
|
+
# Perform actions from list
|
95
|
+
#
|
96
|
+
# @list [Array<Symbol,Proc>] methods and procs to
|
97
|
+
# be executed
|
98
|
+
#
|
99
|
+
# Execute each proc and call method in the list
|
100
|
+
# within the given context
|
101
|
+
#
|
102
|
+
# @return [Array<Proc>]
|
103
|
+
def execute_actions(list)
|
104
|
+
actions(list).each do |action|
|
105
|
+
context.instance_eval(&action)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# @private
|
110
|
+
#
|
111
|
+
# Transforms the input object into a Proc
|
112
|
+
#
|
113
|
+
# @param list [Symbol,Proc] method or proc to be transformed
|
114
|
+
#
|
115
|
+
# @return [Proc]
|
116
|
+
def actions(list)
|
117
|
+
list.map do |entry|
|
118
|
+
if entry.is_a?(Proc)
|
119
|
+
proc(&entry)
|
120
|
+
else
|
121
|
+
proc { send(entry) }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jace
|
4
|
+
# @api public
|
5
|
+
# @author Darthjee
|
6
|
+
#
|
7
|
+
# Class responsible for registering handlers to events
|
8
|
+
class Registry
|
9
|
+
# Event & Handlers registry
|
10
|
+
#
|
11
|
+
# @return [Hash] map of all events and the registered handlers
|
12
|
+
def registry
|
13
|
+
@registry ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Registered events
|
17
|
+
#
|
18
|
+
# @return [Array<Symbol>]
|
19
|
+
def events
|
20
|
+
registry.keys
|
21
|
+
end
|
22
|
+
|
23
|
+
# Register a handler to an event
|
24
|
+
#
|
25
|
+
# @param event [Symbol,String] event name
|
26
|
+
# @param instant [Symbol] intant where the handler will be ran (before or
|
27
|
+
# after)
|
28
|
+
# @param block [Proc] block to be executed when the event is called
|
29
|
+
#
|
30
|
+
# @return [Array<Proc>]
|
31
|
+
#
|
32
|
+
# @example registering an event
|
33
|
+
# registry = described_class.new
|
34
|
+
#
|
35
|
+
# registry.register(:the_event) do
|
36
|
+
# do_something_after
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# registry.register(:the_event, :before) do
|
40
|
+
# do_something_before
|
41
|
+
# end
|
42
|
+
def register(event, instant = :after, &block)
|
43
|
+
registry[event.to_sym] ||= {}
|
44
|
+
registry[event.to_sym][instant] ||= []
|
45
|
+
registry[event.to_sym][instant] << block
|
46
|
+
end
|
47
|
+
|
48
|
+
# Triggers an event
|
49
|
+
# @param event [Symbol,String] event to be triggered
|
50
|
+
# @param context [Object] context where the events will be ran
|
51
|
+
#
|
52
|
+
# @return [Object] the result of the block call
|
53
|
+
#
|
54
|
+
# @example triggering an event
|
55
|
+
# class SomeContext
|
56
|
+
# def do_something(instant)
|
57
|
+
# puts "doing something #{instant}"
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# registry = described_class.new
|
62
|
+
# context = SomeContext.new
|
63
|
+
#
|
64
|
+
# registry.register(:the_event) { do_something(:after) }
|
65
|
+
# registry.register(:the_event, :before) { do_something(:before) }
|
66
|
+
#
|
67
|
+
# registry.trigger(:the_event, context) do
|
68
|
+
# context.do_something(:middle)
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# # puts 'doing something before',
|
72
|
+
# # puts 'doing something middle',
|
73
|
+
# # puts 'doing something after'
|
74
|
+
def trigger(event, context, &block)
|
75
|
+
Dispatcher.new(registry[event.to_sym] || {}).dispatch(context, &block)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/jace/version.rb
CHANGED
data/lib/jace.rb
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
# @api public
|
4
4
|
# @author darthjee
|
5
|
-
|
6
|
-
autoload :VERSION,
|
5
|
+
module Jace
|
6
|
+
autoload :VERSION, 'jace/version'
|
7
|
+
autoload :Dispatcher, 'jace/dispatcher'
|
8
|
+
autoload :Executer, 'jace/executer'
|
9
|
+
autoload :Registry, 'jace/registry'
|
7
10
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Jace::Registry do
|
6
|
+
describe 'yard' do
|
7
|
+
subject(:registry) { described_class.new }
|
8
|
+
|
9
|
+
let(:context) { SomeContext.new }
|
10
|
+
let(:expected_texts) do
|
11
|
+
[
|
12
|
+
'doing something before',
|
13
|
+
'doing something middle',
|
14
|
+
'doing something after'
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'runs the event handlers' do
|
19
|
+
registry.register(:the_event) { do_something(:after) }
|
20
|
+
registry.register(:the_event, :before) { do_something(:before) }
|
21
|
+
|
22
|
+
registry.trigger(:the_event, context) do
|
23
|
+
context.do_something(:middle)
|
24
|
+
end
|
25
|
+
|
26
|
+
expect(context.text)
|
27
|
+
.to eq(expected_texts)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Jace::Registry do
|
6
|
+
describe 'yard' do
|
7
|
+
subject(:registry) { described_class.new }
|
8
|
+
|
9
|
+
describe '#register' do
|
10
|
+
it 'register a new handler for an event' do
|
11
|
+
registry.register(:the_event) do
|
12
|
+
do_something_after
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'register a new handler for before an event' do
|
17
|
+
registry.register(:the_event, :before) do
|
18
|
+
do_something_before
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#trigger' do
|
24
|
+
let(:context) { SomeContext.new }
|
25
|
+
let(:expected_texts) do
|
26
|
+
[
|
27
|
+
'doing something before',
|
28
|
+
'doing something middle',
|
29
|
+
'doing something after'
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'runs the event handlers' do
|
34
|
+
registry.register(:the_event) { do_something(:after) }
|
35
|
+
registry.register(:the_event, :before) { do_something(:before) }
|
36
|
+
|
37
|
+
registry.trigger(:the_event, context) do
|
38
|
+
context.do_something(:middle)
|
39
|
+
end
|
40
|
+
|
41
|
+
expect(context.text)
|
42
|
+
.to eq(expected_texts)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|