jace 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba76568b768021abe49c12d16b285e9f9ac0d3d631074a1aa8e8d1619d8d397f
4
- data.tar.gz: 17a86c3dbe926f9e2fd111eb6c67190421c8d455ffc35c1e9c8b4dab40c80070
3
+ metadata.gz: 9deeac89b6a000553037f4d1b46892a6d2a4382e2bd90631b15a8aebf72ef937
4
+ data.tar.gz: 14f9708ec211705b8cb7c5f34e58b376e69023dc2cabdb3ed99257076499c65f
5
5
  SHA512:
6
- metadata.gz: f156ad1797a018d5c3d16c114156c1613a21315b6707c398471702d90dc7d692a89cf53b14b66631ed33d80a28557eca9e123413339497cff6b8d441f0a9b9d7
7
- data.tar.gz: f16a65b15e85ce825edbe8c105ee2a8bb1575831be2e83faa83d8decf64f2bd11fffea311841c4b82420ce551a208819c3ee231ac0dc32567cfdbbdfc4a8e315
6
+ metadata.gz: 2cb63290e009f73af1079d45f2d2498d1c93b645e64cff7351ca1167da542772e684e61eaa016f46c4ea33577633bcb73e136da2cfc3a814e29e61a5427d6cca
7
+ data.tar.gz: f7bfb72698544a326cca4ae042bd48ddc4e6325839f58ea5516e100f0aef8324dbaced3571b0e44f15c4cd933c2ed15688be92a2adf0808cc0141d8dd8a5e2f4
data/README.md CHANGED
@@ -11,7 +11,7 @@ Jace
11
11
 
12
12
  Yard Documentation
13
13
  -------------------
14
- [https://www.rubydoc.info/gems/jace/0.1.0](https://www.rubydoc.info/gems/jace/0.1.0)
14
+ [https://www.rubydoc.info/gems/jace/0.1.1](https://www.rubydoc.info/gems/jace/0.1.1)
15
15
 
16
16
  Jace is designed to have a semi event driven development
17
17
 
data/lib/jace/executer.rb CHANGED
@@ -108,18 +108,14 @@ module Jace
108
108
 
109
109
  # @private
110
110
  #
111
- # Transforms the input object into a Proc
111
+ # Transforms the input objects into hadlers
112
112
  #
113
- # @param list [Symbol,Proc] method or proc to be transformed
113
+ # @param list [Array<Symbol,Proc>] method or proc to be transformed
114
114
  #
115
- # @return [Proc]
115
+ # @return [Array<Handler>]
116
116
  def actions(list)
117
117
  list.map do |entry|
118
- if entry.is_a?(Proc)
119
- proc(&entry)
120
- else
121
- proc { send(entry) }
122
- end
118
+ Handler.new(entry)
123
119
  end
124
120
  end
125
121
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jace
4
+ # @api private
5
+ # @author Darthjee
6
+ #
7
+ # Handler that will be executed once an event has been triggerd
8
+ #
9
+ # Handler will use the given method name or block to execute some code
10
+ # within the given context
11
+ class Handler
12
+ attr_reader :block
13
+
14
+ # @overload initialize(method_name)
15
+ # @param method_name [Symbol] method to be called in context
16
+ # @overload initialize(block)
17
+ # @param block [Proc] block object to be called within context
18
+ # @overload initialize(&block)
19
+ # @param block [Proc] block to be called within context
20
+ def initialize(method_name = nil, &block)
21
+ if block
22
+ @block = block if block
23
+ elsif method_name.is_a?(Proc)
24
+ @block = method_name
25
+ else
26
+ @block = proc { send(method_name) }
27
+ end
28
+ end
29
+
30
+ def call(context)
31
+ context.instance_eval(&block)
32
+ end
33
+
34
+ def to_proc
35
+ handler = self
36
+ proc { |context| handler.call(context) }
37
+ end
38
+ end
39
+ end
data/lib/jace/registry.rb CHANGED
@@ -40,9 +40,8 @@ module Jace
40
40
  # do_something_before
41
41
  # end
42
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
43
+ registry[event.to_sym] ||= Dispatcher.new
44
+ registry[event.to_sym].send(instant) << block
46
45
  end
47
46
 
48
47
  # Triggers an event
@@ -72,7 +71,13 @@ module Jace
72
71
  # # puts 'doing something middle',
73
72
  # # puts 'doing something after'
74
73
  def trigger(event, context, &block)
75
- Dispatcher.new(registry[event.to_sym] || {}).dispatch(context, &block)
74
+ dispatcher_for(event).dispatch(context, &block)
75
+ end
76
+
77
+ private
78
+
79
+ def dispatcher_for(event)
80
+ registry[event.to_sym] || Dispatcher.new
76
81
  end
77
82
  end
78
83
  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.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/jace.rb CHANGED
@@ -6,5 +6,6 @@ module Jace
6
6
  autoload :VERSION, 'jace/version'
7
7
  autoload :Dispatcher, 'jace/dispatcher'
8
8
  autoload :Executer, 'jace/executer'
9
+ autoload :Handler, 'jace/handler'
9
10
  autoload :Registry, 'jace/registry'
10
11
  end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Jace::Handler do
6
+ let(:context) { instance_double(Context) }
7
+
8
+ describe '#call' do
9
+ context 'when handler is initialized with a block' do
10
+ subject(:handler) { described_class.new { method_call } }
11
+
12
+ before { allow(context).to receive(:method_call) }
13
+
14
+ it 'calls the block' do
15
+ handler.call(context)
16
+
17
+ expect(context).to have_received(:method_call).once
18
+ end
19
+ end
20
+
21
+ context 'when handler is initialized with a proc' do
22
+ subject(:handler) { described_class.new(proc { method_call }) }
23
+
24
+ before { allow(context).to receive(:method_call) }
25
+
26
+ it 'calls the block' do
27
+ handler.call(context)
28
+
29
+ expect(context).to have_received(:method_call).once
30
+ end
31
+ end
32
+
33
+ context 'when handler is initialized with a symbol' do
34
+ subject(:handler) { described_class.new(:method_call) }
35
+
36
+ before { allow(context).to receive(:method_call) }
37
+
38
+ it 'calls the block' do
39
+ handler.call(context)
40
+
41
+ expect(context).to have_received(:method_call).once
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#to_proc' do
47
+ context 'when handler is initialized with a block' do
48
+ subject(:handler) { described_class.new { method_call } }
49
+
50
+ it do
51
+ expect(handler.to_proc).to be_a(Proc)
52
+ end
53
+
54
+ context 'when the proc is called' do
55
+ before { allow(context).to receive(:method_call) }
56
+
57
+ it 'calls the block' do
58
+ context.instance_eval(&handler.to_proc)
59
+
60
+ expect(context).to have_received(:method_call).once
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'when handler is initialized with a proc' do
66
+ subject(:handler) { described_class.new(proc { method_call }) }
67
+
68
+ it do
69
+ expect(handler.to_proc).to be_a(Proc)
70
+ end
71
+
72
+ context 'when the proc is called' do
73
+ before { allow(context).to receive(:method_call) }
74
+
75
+ it 'calls the block' do
76
+ context.instance_eval(&handler.to_proc)
77
+
78
+ expect(context).to have_received(:method_call).once
79
+ end
80
+ end
81
+ end
82
+
83
+ context 'when handler is initialized with a symbol' do
84
+ subject(:handler) { described_class.new(:method_call) }
85
+
86
+ it do
87
+ expect(handler.to_proc).to be_a(Proc)
88
+ end
89
+
90
+ context 'when the proc is called' do
91
+ before { allow(context).to receive(:method_call) }
92
+
93
+ it 'calls the block' do
94
+ context.instance_eval(&handler.to_proc)
95
+
96
+ expect(context).to have_received(:method_call).once
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -8,7 +8,7 @@ describe Jace::Registry do
8
8
  describe '#register' do
9
9
  let(:event_name) { :event_name }
10
10
  let(:expected_registry) do
11
- { event_name: { after: [Proc] } }
11
+ { event_name: Jace::Dispatcher }
12
12
  end
13
13
 
14
14
  context 'when event is a symbol' do
@@ -28,7 +28,7 @@ describe Jace::Registry do
28
28
  context 'when event is a string' do
29
29
  let(:event_name) { 'event_name' }
30
30
  let(:expected_registry) do
31
- { event_name: { after: [Proc] } }
31
+ { event_name: Jace::Dispatcher }
32
32
  end
33
33
 
34
34
  it 'adds even to events registry' do
@@ -45,16 +45,11 @@ describe Jace::Registry do
45
45
  end
46
46
 
47
47
  context 'when the event was already registerd' do
48
- let(:expected_registry) do
49
- { event_name: { after: [Proc, Proc] } }
50
- end
51
-
52
48
  before { registry.register(event_name) {} }
53
49
 
54
- it 'adds even a callback to the event registry' do
50
+ it 'does not repace dispatcher' do
55
51
  expect { registry.register(event_name) {} }
56
- .to change(registry, :registry)
57
- .to(expected_registry)
52
+ .not_to change(registry, :registry)
58
53
  end
59
54
 
60
55
  it 'adds event to event list' do
@@ -66,8 +61,8 @@ describe Jace::Registry do
66
61
  context 'when another event was already registerd' do
67
62
  let(:expected_registry) do
68
63
  {
69
- event_name: { after: [Proc] },
70
- other_event_name: { after: [Proc] }
64
+ event_name: Jace::Dispatcher,
65
+ other_event_name: Jace::Dispatcher
71
66
  }
72
67
  end
73
68
 
@@ -89,7 +84,7 @@ describe Jace::Registry do
89
84
  context 'when registering for another instant' do
90
85
  let(:expected_registry) do
91
86
  {
92
- event_name: { before: [Proc] }
87
+ event_name: Jace::Dispatcher
93
88
  }
94
89
  end
95
90
 
@@ -115,10 +110,9 @@ describe Jace::Registry do
115
110
 
116
111
  before { registry.register(event_name, :after) {} }
117
112
 
118
- it 'adds even a callback to the event registry' do
113
+ it 'does not repace dispatcher' do
119
114
  expect { registry.register(event_name, :before) {} }
120
- .to change(registry, :registry)
121
- .to(expected_registry)
115
+ .not_to change(registry, :registry)
122
116
  end
123
117
 
124
118
  it 'does not add event to event list' do
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-19 00:00:00.000000000 Z
11
+ date: 2022-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -261,6 +261,7 @@ files:
261
261
  - lib/jace.rb
262
262
  - lib/jace/dispatcher.rb
263
263
  - lib/jace/executer.rb
264
+ - lib/jace/handler.rb
264
265
  - lib/jace/registry.rb
265
266
  - lib/jace/version.rb
266
267
  - spec/integration/readme/.keep
@@ -269,6 +270,7 @@ files:
269
270
  - spec/integration/yard/jace/registry_spec.rb
270
271
  - spec/lib/jace/dispatcher_spec.rb
271
272
  - spec/lib/jace/executer_spec.rb
273
+ - spec/lib/jace/handler_spec.rb
272
274
  - spec/lib/jace/registry_spec.rb
273
275
  - spec/spec_helper.rb
274
276
  - spec/support/models/.keep