jace 0.0.2 → 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: e48246a546ecbb574464b1e411d4d72c30369eda539c121309d02c9f6d47978e
4
- data.tar.gz: 42b96e64f9a04f1e4b0d6eb3430044890ea18b48648bde0c9189be07867c2630
3
+ metadata.gz: 9deeac89b6a000553037f4d1b46892a6d2a4382e2bd90631b15a8aebf72ef937
4
+ data.tar.gz: 14f9708ec211705b8cb7c5f34e58b376e69023dc2cabdb3ed99257076499c65f
5
5
  SHA512:
6
- metadata.gz: 70c38cdaaa607c953d94ce21ad5acead8692e33a383d01d2cb5301fa7ace4dfb7e48c9cd4ec5179c1f1a008277379107374d48931dd604ba9589398ac4973312
7
- data.tar.gz: 70785d17966a037cabf6f8f092b23df4215b129265de4fd57f141511bd926596ff1fd9e31340db464d6d0a7e95ba283ff7ef12c3fd4b6fbd0fa5e7006f30fb6f
6
+ metadata.gz: 2cb63290e009f73af1079d45f2d2498d1c93b645e64cff7351ca1167da542772e684e61eaa016f46c4ea33577633bcb73e136da2cfc3a814e29e61a5427d6cca
7
+ data.tar.gz: f7bfb72698544a326cca4ae042bd48ddc4e6325839f58ea5516e100f0aef8324dbaced3571b0e44f15c4cd933c2ed15688be92a2adf0808cc0141d8dd8a5e2f4
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
- build:
23
+ test:
4
24
  docker:
5
- - image: darthjee/circleci_ruby_gems:0.5.2
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
@@ -39,6 +39,7 @@ RSpec/NestedGroups:
39
39
  RSpec/InstanceVariable:
40
40
  Exclude:
41
41
  - 'spec/lib/jace/executer_spec.rb'
42
+ - 'spec/lib/jace/dispatcher_spec.rb'
42
43
 
43
44
  Naming/MemoizedInstanceVariableName:
44
45
  Exclude:
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.2](https://www.rubydoc.info/gems/jace/0.0.2)
14
+ [https://www.rubydoc.info/gems/jace/0.1.1](https://www.rubydoc.info/gems/jace/0.1.1)
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
+ ```
@@ -1,2 +1,3 @@
1
1
  ignore:
2
2
  - lib/jace/version.rb
3
+ - lib/jace.rb
data/config/yardstick.yml CHANGED
@@ -1,4 +1,4 @@
1
- threshold: 90
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
@@ -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
@@ -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
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
@@ -70,18 +108,14 @@ module Jace
70
108
 
71
109
  # @private
72
110
  #
73
- # Transforms the input object into a Proc
111
+ # Transforms the input objects into hadlers
74
112
  #
75
- # @param list [Symbol,Proc] method or proc to be transformed
113
+ # @param list [Array<Symbol,Proc>] method or proc to be transformed
76
114
  #
77
- # @return [Proc]
115
+ # @return [Array<Handler>]
78
116
  def actions(list)
79
117
  list.map do |entry|
80
- if entry.is_a?(Proc)
81
- proc(&entry)
82
- else
83
- proc { send(entry) }
84
- end
118
+ Handler.new(entry)
85
119
  end
86
120
  end
87
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
@@ -0,0 +1,83 @@
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] ||= Dispatcher.new
44
+ registry[event.to_sym].send(instant) << block
45
+ end
46
+
47
+ # Triggers an event
48
+ # @param event [Symbol,String] event to be triggered
49
+ # @param context [Object] context where the events will be ran
50
+ #
51
+ # @return [Object] the result of the block call
52
+ #
53
+ # @example triggering an event
54
+ # class SomeContext
55
+ # def do_something(instant)
56
+ # puts "doing something #{instant}"
57
+ # end
58
+ # end
59
+ #
60
+ # registry = described_class.new
61
+ # context = SomeContext.new
62
+ #
63
+ # registry.register(:the_event) { do_something(:after) }
64
+ # registry.register(:the_event, :before) { do_something(:before) }
65
+ #
66
+ # registry.trigger(:the_event, context) do
67
+ # context.do_something(:middle)
68
+ # end
69
+ #
70
+ # # puts 'doing something before',
71
+ # # puts 'doing something middle',
72
+ # # puts 'doing something after'
73
+ def trigger(event, 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
81
+ end
82
+ end
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.0.2'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/jace.rb CHANGED
@@ -3,6 +3,9 @@
3
3
  # @api public
4
4
  # @author darthjee
5
5
  module Jace
6
- autoload :VERSION, 'jace/version'
7
- autoload :Executer, 'jace/executer'
6
+ autoload :VERSION, 'jace/version'
7
+ autoload :Dispatcher, 'jace/dispatcher'
8
+ autoload :Executer, 'jace/executer'
9
+ autoload :Handler, 'jace/handler'
10
+ autoload :Registry, 'jace/registry'
8
11
  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