jace 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a538f92589b4feb4a08d1f8521f93c34979c1e5e80b14edb7aaeeec6c05e66a0
4
- data.tar.gz: 1de3fa76bdbf09d5488cec3e09ec4a6074377c2134ccd3311591057bd45e1903
3
+ metadata.gz: ba76568b768021abe49c12d16b285e9f9ac0d3d631074a1aa8e8d1619d8d397f
4
+ data.tar.gz: 17a86c3dbe926f9e2fd111eb6c67190421c8d455ffc35c1e9c8b4dab40c80070
5
5
  SHA512:
6
- metadata.gz: 3b6d12aa27caa2ee4db4ef93404b40aea0b1d5cf5a581ab309f588b79a5390aaa1baa1ea399bd9fe8b771ee2bc354f7dc5b53456e5bdf2f1a06092a4c881e913
7
- data.tar.gz: a6a260fefb1cae9ed26d8643961456dd1a7d90186bfddf92aaaa0af86b71c0b21a1edbd8e717fd947f55037f32a1abbd413af9814c1a716eb81afea526ab5f25
6
+ metadata.gz: f156ad1797a018d5c3d16c114156c1613a21315b6707c398471702d90dc7d692a89cf53b14b66631ed33d80a28557eca9e123413339497cff6b8d441f0a9b9d7
7
+ data.tar.gz: f16a65b15e85ce825edbe8c105ee2a8bb1575831be2e83faa83d8decf64f2bd11fffea311841c4b82420ce551a208819c3ee231ac0dc32567cfdbbdfc4a8e315
data/.circleci/config.yml CHANGED
@@ -1,13 +1,28 @@
1
1
  version: 2
2
2
  workflows:
3
3
  version: 2
4
- test:
4
+ test-and-build:
5
5
  jobs:
6
- - build-and-release
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
7
22
  jobs:
8
23
  test:
9
24
  docker:
10
- - image: darthjee/circleci_ruby_gems:0.5.2
25
+ - image: darthjee/circleci_ruby_270:1.1.0
11
26
  environment:
12
27
  PROJECT: jace
13
28
  steps:
@@ -21,12 +36,22 @@ jobs:
21
36
  - run:
22
37
  name: RSpec
23
38
  command: bundle exec rspec
24
- - run:
25
- name: Rubocop
26
- command: rubocop
27
39
  - run:
28
40
  name: Coverage Test Report
29
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
30
55
  - run:
31
56
  name: Yardstick coverage check
32
57
  command: bundle exec rake verify_measurements
@@ -41,20 +66,20 @@ jobs:
41
66
  command: check_specs
42
67
  build-and-release:
43
68
  docker:
44
- - image: darthjee/circleci_ruby_gems:0.5.2
69
+ - image: darthjee/circleci_ruby_270:1.1.0
45
70
  environment:
46
71
  PROJECT: jace
47
72
  steps:
48
73
  - checkout
49
74
  - run:
50
- name: Signin
51
- command: scripts/build.sh signin
75
+ name: Bundle Install
76
+ command: bundle install
52
77
  - run:
53
- name: Degub
54
- command: cat ~/.gem/credentials
78
+ name: Signin
79
+ command: build_gem.sh signin
55
80
  - run:
56
81
  name: Build Gem
57
- command: scripts/build.sh build
82
+ command: build_gem.sh build
58
83
  - run:
59
84
  name: Push Gem
60
- command: scripts/build.sh push
85
+ command: build_gem.sh push
data/Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
- FROM darthjee/scripts:0.1.7 as scripts
1
+ FROM darthjee/scripts:0.3.1 as scripts
2
2
 
3
- FROM darthjee/ruby_gems:0.5.2 as base
3
+ FROM darthjee/ruby_270:1.1.0 as base
4
4
 
5
5
  COPY --chown=app:app ./ /home/app/app/
6
6
 
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- Sinclair
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.3](https://www.rubydoc.info/gems/jace/0.0.3)
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
- ```ruby
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
- ```ruby
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/yardstick.yml CHANGED
@@ -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
@@ -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', '1.16.1'
23
- gem.add_development_dependency 'pry', '0.12.2'
24
- gem.add_development_dependency 'pry-nav', '0.3.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', '5.6.0'
27
- gem.add_development_dependency 'rspec', '3.9.0'
28
- gem.add_development_dependency 'rspec-core', '3.9.1'
29
- gem.add_development_dependency 'rspec-expectations', '3.9.1'
30
- gem.add_development_dependency 'rspec-mocks', '3.9.1'
31
- gem.add_development_dependency 'rspec-support', '3.9.2'
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.4.1'
35
- gem.add_development_dependency 'simplecov', '0.17.1'
36
- gem.add_development_dependency 'yard', '0.9.24'
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
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jace
4
- # @api public
4
+ # @api private
5
5
  # @author Darthjee
6
6
  #
7
7
  # Class responsible for dispatching the call of events
@@ -34,5 +34,21 @@ module Jace
34
34
  private
35
35
 
36
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
37
53
  end
38
54
  end
data/lib/jace/executer.rb CHANGED
@@ -31,7 +31,7 @@ module Jace
31
31
  # @return (see .call)
32
32
  def call
33
33
  execute_actions(before)
34
- result = block.call
34
+ result = block.call if block
35
35
  execute_actions(after)
36
36
 
37
37
  result
@@ -51,6 +51,44 @@ module Jace
51
51
 
52
52
  attr_reader :before, :after, :context, :block
53
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
+
54
92
  # @private
55
93
  #
56
94
  # Perform actions from list
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jace
4
- VERSION = '0.0.3'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/jace.rb CHANGED
@@ -4,6 +4,7 @@
4
4
  # @author darthjee
5
5
  module Jace
6
6
  autoload :VERSION, 'jace/version'
7
- autoload :Executer, 'jace/executer'
8
7
  autoload :Dispatcher, 'jace/dispatcher'
8
+ autoload :Executer, 'jace/executer'
9
+ autoload :Registry, 'jace/registry'
9
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
@@ -153,5 +153,36 @@ describe Jace::Dispatcher do
153
153
  end
154
154
  end
155
155
  end
156
+
157
+ context 'when no block is provided' do
158
+ let(:result) do
159
+ dispatcher.dispatch(person)
160
+ end
161
+
162
+ it do
163
+ expect(result).to be_nil
164
+ end
165
+
166
+ context 'when handlers are given' do
167
+ let(:before) { :init_age }
168
+ let(:after) { :init_height }
169
+
170
+ it do
171
+ expect(result).to be_nil
172
+ end
173
+
174
+ it 'calls before handler' do
175
+ expect { result }
176
+ .to change(person, :age)
177
+ .to(1)
178
+ end
179
+
180
+ it 'calls after handler' do
181
+ expect { result }
182
+ .to change(person, :height)
183
+ .to(178)
184
+ end
185
+ end
186
+ end
156
187
  end
157
188
  end
@@ -140,5 +140,39 @@ describe Jace::Executer do
140
140
  end
141
141
  end
142
142
  end
143
+
144
+ context 'when no block is provided' do
145
+ let(:result) do
146
+ described_class.call(context: person)
147
+ end
148
+
149
+ it do
150
+ expect(result).to be_nil
151
+ end
152
+
153
+ context 'when handlers are given' do
154
+ let(:result) do
155
+ described_class.call(
156
+ before: :init_age, after: :init_height, context: person
157
+ )
158
+ end
159
+
160
+ it do
161
+ expect(result).to be_nil
162
+ end
163
+
164
+ it 'calls before handler' do
165
+ expect { result }
166
+ .to change(person, :age)
167
+ .to(1)
168
+ end
169
+
170
+ it 'calls after handler' do
171
+ expect { result }
172
+ .to change(person, :height)
173
+ .to(178)
174
+ end
175
+ end
176
+ end
143
177
  end
144
178
  end
@@ -0,0 +1,246 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Jace::Registry do
6
+ subject(:registry) { described_class.new }
7
+
8
+ describe '#register' do
9
+ let(:event_name) { :event_name }
10
+ let(:expected_registry) do
11
+ { event_name: { after: [Proc] } }
12
+ end
13
+
14
+ context 'when event is a symbol' do
15
+ it 'adds even to events registry' do
16
+ expect { registry.register(event_name) {} }
17
+ .to change(registry, :registry)
18
+ .to(expected_registry)
19
+ end
20
+
21
+ it 'adds event to event list' do
22
+ expect { registry.register(event_name) {} }
23
+ .to change(registry, :events)
24
+ .by([:event_name])
25
+ end
26
+ end
27
+
28
+ context 'when event is a string' do
29
+ let(:event_name) { 'event_name' }
30
+ let(:expected_registry) do
31
+ { event_name: { after: [Proc] } }
32
+ end
33
+
34
+ it 'adds even to events registry' do
35
+ expect { registry.register(event_name) {} }
36
+ .to change(registry, :registry)
37
+ .to(expected_registry)
38
+ end
39
+
40
+ it 'adds event to event list' do
41
+ expect { registry.register(event_name) {} }
42
+ .to change(registry, :events)
43
+ .by([:event_name])
44
+ end
45
+ end
46
+
47
+ context 'when the event was already registerd' do
48
+ let(:expected_registry) do
49
+ { event_name: { after: [Proc, Proc] } }
50
+ end
51
+
52
+ before { registry.register(event_name) {} }
53
+
54
+ it 'adds even a callback to the event registry' do
55
+ expect { registry.register(event_name) {} }
56
+ .to change(registry, :registry)
57
+ .to(expected_registry)
58
+ end
59
+
60
+ it 'adds event to event list' do
61
+ expect { registry.register(event_name) {} }
62
+ .not_to(change(registry, :events))
63
+ end
64
+ end
65
+
66
+ context 'when another event was already registerd' do
67
+ let(:expected_registry) do
68
+ {
69
+ event_name: { after: [Proc] },
70
+ other_event_name: { after: [Proc] }
71
+ }
72
+ end
73
+
74
+ before { registry.register(:other_event_name) {} }
75
+
76
+ it 'adds even a callback to the event registry' do
77
+ expect { registry.register(event_name) {} }
78
+ .to change(registry, :registry)
79
+ .to(expected_registry)
80
+ end
81
+
82
+ it 'adds event to event list' do
83
+ expect { registry.register(event_name) {} }
84
+ .to change(registry, :events)
85
+ .by([:event_name])
86
+ end
87
+ end
88
+
89
+ context 'when registering for another instant' do
90
+ let(:expected_registry) do
91
+ {
92
+ event_name: { before: [Proc] }
93
+ }
94
+ end
95
+
96
+ it 'adds even a callback to the event registry' do
97
+ expect { registry.register(event_name, :before) {} }
98
+ .to change(registry, :registry)
99
+ .to(expected_registry)
100
+ end
101
+
102
+ it 'adds event to event list' do
103
+ expect { registry.register(event_name) {} }
104
+ .to change(registry, :events)
105
+ .by([:event_name])
106
+ end
107
+ end
108
+
109
+ context 'when registering for another instant of an exiosting event' do
110
+ let(:expected_registry) do
111
+ {
112
+ event_name: { before: [Proc], after: [Proc] }
113
+ }
114
+ end
115
+
116
+ before { registry.register(event_name, :after) {} }
117
+
118
+ it 'adds even a callback to the event registry' do
119
+ expect { registry.register(event_name, :before) {} }
120
+ .to change(registry, :registry)
121
+ .to(expected_registry)
122
+ end
123
+
124
+ it 'does not add event to event list' do
125
+ expect { registry.register(event_name) {} }
126
+ .not_to(change(registry, :events))
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '#trigger' do
132
+ let(:event_name) { :event_name }
133
+ let(:context) { instance_double(Context) }
134
+
135
+ context 'when an event handler has been registered' do
136
+ before do
137
+ allow(context).to receive(:method_call)
138
+ allow(context).to receive(:other_method)
139
+ registry.register(event_name) { method_call }
140
+ end
141
+
142
+ it 'execute the event handler' do
143
+ registry.trigger(event_name, context) {}
144
+ expect(context).to have_received(:method_call)
145
+ end
146
+
147
+ it 'execute the block' do
148
+ registry.trigger(event_name, context) { context.other_method }
149
+ expect(context).to have_received(:other_method)
150
+ end
151
+ end
152
+
153
+ context 'when many event handlers have been registered' do
154
+ before do
155
+ allow(context).to receive(:method_call)
156
+ allow(context).to receive(:another_method_call)
157
+ allow(context).to receive(:other_method)
158
+
159
+ registry.register(event_name) { method_call }
160
+ registry.register(event_name) { another_method_call }
161
+ end
162
+
163
+ it 'execute the event handler' do
164
+ registry.trigger(event_name, context) {}
165
+ expect(context).to have_received(:method_call)
166
+ end
167
+
168
+ it 'execute the other event handler' do
169
+ registry.trigger(event_name, context) {}
170
+ expect(context).to have_received(:another_method_call)
171
+ end
172
+
173
+ it 'execute the block' do
174
+ registry.trigger(event_name, context) { context.other_method }
175
+ expect(context).to have_received(:other_method).once
176
+ end
177
+ end
178
+
179
+ context 'when another event handler has been registered' do
180
+ before do
181
+ allow(context).to receive(:method_call)
182
+ allow(context).to receive(:other_method)
183
+ registry.register(:other_event) { method_call }
184
+ end
185
+
186
+ it 'does not execute the event handler' do
187
+ registry.trigger(event_name, context) {}
188
+ expect(context).not_to have_received(:method_call)
189
+ end
190
+
191
+ it 'execute the block' do
192
+ registry.trigger(event_name, context) { context.other_method }
193
+ expect(context).to have_received(:other_method)
194
+ end
195
+ end
196
+
197
+ context 'when an event handler has not been registered' do
198
+ before do
199
+ allow(context).to receive(:other_method)
200
+ end
201
+
202
+ it 'execute the block' do
203
+ registry.trigger(event_name, context) { context.other_method }
204
+ expect(context).to have_received(:other_method)
205
+ end
206
+ end
207
+
208
+ context 'when no block is given' do
209
+ before do
210
+ allow(context).to receive(:method_call)
211
+
212
+ registry.register(event_name) { method_call }
213
+ end
214
+
215
+ it 'execute the event handler' do
216
+ registry.trigger(event_name, context)
217
+
218
+ expect(context).to have_received(:method_call)
219
+ end
220
+ end
221
+
222
+ describe 'order execution' do
223
+ let(:context) { SomeContext.new }
224
+ let(:expected_texts) do
225
+ [
226
+ 'doing something before',
227
+ 'doing something middle',
228
+ 'doing something after'
229
+ ]
230
+ end
231
+
232
+ before do
233
+ registry.register(:the_event) { do_something(:after) }
234
+ registry.register(:the_event, :before) { do_something(:before) }
235
+ end
236
+
237
+ it 'runs the event handlers in order' do
238
+ registry.trigger(:the_event, context) do
239
+ context.do_something(:middle)
240
+ end
241
+
242
+ expect(context.text).to eq(expected_texts)
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Context
4
+ attr_reader :method_call, :other_method, :another_method_call
5
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Person
4
- attr_accessor :age
4
+ attr_accessor :age, :height
5
5
 
6
6
  def brothers
7
7
  @brothers ||= 0
@@ -9,6 +9,10 @@ class Person
9
9
 
10
10
  private
11
11
 
12
+ def init_height
13
+ @height = 178
14
+ end
15
+
12
16
  def init_age
13
17
  @age ||= 1
14
18
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SomeContext
4
+ def do_something(instant)
5
+ puts "doing something #{instant}"
6
+ end
7
+
8
+ def text
9
+ @text ||= []
10
+ end
11
+
12
+ def puts(string)
13
+ text << string
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-12 00:00:00.000000000 Z
11
+ date: 2022-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.16.1
19
+ version: 2.3.20
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.16.1
26
+ version: 2.3.20
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 0.12.2
33
+ version: 0.14.1
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 0.12.2
40
+ version: 0.14.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry-nav
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 0.3.0
47
+ version: 1.0.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 0.3.0
54
+ version: 1.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,84 +72,84 @@ dependencies:
72
72
  requirements:
73
73
  - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 5.6.0
75
+ version: 6.0.3
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 5.6.0
82
+ version: 6.0.3
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 3.9.0
89
+ version: 3.11.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 3.9.0
96
+ version: 3.11.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec-core
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - '='
102
102
  - !ruby/object:Gem::Version
103
- version: 3.9.1
103
+ version: 3.11.0
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - '='
109
109
  - !ruby/object:Gem::Version
110
- version: 3.9.1
110
+ version: 3.11.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec-expectations
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - '='
116
116
  - !ruby/object:Gem::Version
117
- version: 3.9.1
117
+ version: 3.11.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - '='
123
123
  - !ruby/object:Gem::Version
124
- version: 3.9.1
124
+ version: 3.11.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec-mocks
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - '='
130
130
  - !ruby/object:Gem::Version
131
- version: 3.9.1
131
+ version: 3.11.1
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - '='
137
137
  - !ruby/object:Gem::Version
138
- version: 3.9.1
138
+ version: 3.11.1
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: rspec-support
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - '='
144
144
  - !ruby/object:Gem::Version
145
- version: 3.9.2
145
+ version: 3.11.0
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - '='
151
151
  - !ruby/object:Gem::Version
152
- version: 3.9.2
152
+ version: 3.11.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: rubocop
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -184,42 +184,42 @@ dependencies:
184
184
  requirements:
185
185
  - - '='
186
186
  - !ruby/object:Gem::Version
187
- version: 4.4.1
187
+ version: 4.7.0
188
188
  type: :development
189
189
  prerelease: false
190
190
  version_requirements: !ruby/object:Gem::Requirement
191
191
  requirements:
192
192
  - - '='
193
193
  - !ruby/object:Gem::Version
194
- version: 4.4.1
194
+ version: 4.7.0
195
195
  - !ruby/object:Gem::Dependency
196
196
  name: simplecov
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - '='
200
200
  - !ruby/object:Gem::Version
201
- version: 0.17.1
201
+ version: 0.21.2
202
202
  type: :development
203
203
  prerelease: false
204
204
  version_requirements: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - '='
207
207
  - !ruby/object:Gem::Version
208
- version: 0.17.1
208
+ version: 0.21.2
209
209
  - !ruby/object:Gem::Dependency
210
210
  name: yard
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
213
  - - '='
214
214
  - !ruby/object:Gem::Version
215
- version: 0.9.24
215
+ version: 0.9.27
216
216
  type: :development
217
217
  prerelease: false
218
218
  version_requirements: !ruby/object:Gem::Requirement
219
219
  requirements:
220
220
  - - '='
221
221
  - !ruby/object:Gem::Version
222
- version: 0.9.24
222
+ version: 0.9.27
223
223
  - !ruby/object:Gem::Dependency
224
224
  name: yardstick
225
225
  requirement: !ruby/object:Gem::Requirement
@@ -261,15 +261,20 @@ files:
261
261
  - lib/jace.rb
262
262
  - lib/jace/dispatcher.rb
263
263
  - lib/jace/executer.rb
264
+ - lib/jace/registry.rb
264
265
  - lib/jace/version.rb
265
- - scripts/build.sh
266
266
  - spec/integration/readme/.keep
267
+ - spec/integration/readme/jace/registry_spec.rb
267
268
  - spec/integration/yard/.keep
269
+ - spec/integration/yard/jace/registry_spec.rb
268
270
  - spec/lib/jace/dispatcher_spec.rb
269
271
  - spec/lib/jace/executer_spec.rb
272
+ - spec/lib/jace/registry_spec.rb
270
273
  - spec/spec_helper.rb
271
274
  - spec/support/models/.keep
275
+ - spec/support/models/context.rb
272
276
  - spec/support/models/person.rb
277
+ - spec/support/models/some_context.rb
273
278
  - spec/support/shared_examples/.keep
274
279
  homepage: https://github.com/darthjee/jace
275
280
  licenses: []
@@ -289,8 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
289
294
  - !ruby/object:Gem::Version
290
295
  version: '0'
291
296
  requirements: []
292
- rubyforge_project:
293
- rubygems_version: 2.7.6
297
+ rubygems_version: 3.3.20
294
298
  signing_key:
295
299
  specification_version: 4
296
300
  summary: Gem focused on events for meta-programing
data/scripts/build.sh DELETED
@@ -1,47 +0,0 @@
1
- #!/bin/bash
2
-
3
- function version() {
4
- echo $(cat lib/$PROJECT/version.rb | grep VERSION | sed -e "s/.*'\\(.*\\)'.*/\\1/g")
5
- }
6
-
7
- function isTagged() {
8
- VERSION=$(version)
9
- TAG=$(git tag | tail -n 1)
10
-
11
- if [ $VERSION = $TAG ]; then
12
- return 0
13
- else
14
- return 1
15
- fi
16
- }
17
-
18
- ACTION=$1
19
-
20
- case $ACTION in
21
- "signin")
22
- mkdir ~/.gem
23
- echo "---" > ~/.gem/credentials
24
- echo ":rubygems_api_key: $RUBY_GEMS_API_KEY" >> ~/.gem/credentials
25
- chmod 600 ~/.gem/credentials
26
- ;;
27
- "build")
28
- if $(isTagged); then
29
- rake build
30
- else
31
- echo version did not change
32
- fi
33
- ;;
34
- "push")
35
- if $(isTagged); then
36
- VERSION=$(version)
37
- gem push "pkg/$PROJECT-$VERSION.gem"
38
- else
39
- echo version did not change
40
- fi
41
- ;;
42
- *)
43
- echo Usage:
44
- echo "$0 build # builds gem"
45
- echo "$0 push # pushes gem"
46
- ;;
47
- esac