jace 0.0.1 → 0.0.2

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: 358835f18e54f117ab1043133ddf2374a5da2cc96ff7ce9c67032adad17abdeb
4
- data.tar.gz: ba47115126440c6f78833e3c32444c08bd603eef3dacb74523475a96f1a10f6c
3
+ metadata.gz: e48246a546ecbb574464b1e411d4d72c30369eda539c121309d02c9f6d47978e
4
+ data.tar.gz: 42b96e64f9a04f1e4b0d6eb3430044890ea18b48648bde0c9189be07867c2630
5
5
  SHA512:
6
- metadata.gz: 3f1dedd86ee3e4a1171b6f09b95874bf70e5e2761d989b4d6f72fd916164977994f271ea311ea82858a0e7544c19874683de5c8311b606d50cc58e69fbb8a08a
7
- data.tar.gz: 98b468f58c0a46d3c7c1b73df95eb64c856c00f0ed314c5935828ac9024223d328bf0f572620c6921ffc88ee4d8f8f9a3513791e3d2f52804385a9716da1daf0
6
+ metadata.gz: 70c38cdaaa607c953d94ce21ad5acead8692e33a383d01d2cb5301fa7ace4dfb7e48c9cd4ec5179c1f1a008277379107374d48931dd604ba9589398ac4973312
7
+ data.tar.gz: 70785d17966a037cabf6f8f092b23df4215b129265de4fd57f141511bd926596ff1fd9e31340db464d6d0a7e95ba283ff7ef12c3fd4b6fbd0fa5e7006f30fb6f
data/.circleci/config.yml CHANGED
@@ -2,7 +2,7 @@ version: 2
2
2
  jobs:
3
3
  build:
4
4
  docker:
5
- - image: darthjee/circleci_ruby_gems:0.5.0
5
+ - image: darthjee/circleci_ruby_gems:0.5.2
6
6
  environment:
7
7
  PROJECT: jace
8
8
  steps:
data/.rubocop.yml CHANGED
@@ -36,6 +36,14 @@ 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
+
43
+ Naming/MemoizedInstanceVariableName:
44
+ Exclude:
45
+ - 'spec/support/models/person.rb'
46
+
39
47
  Style/HashEachMethods:
40
48
  Enabled: true
41
49
 
data/README.md CHANGED
@@ -11,7 +11,7 @@ Sinclair
11
11
 
12
12
  Yard Documentation
13
13
  -------------------
14
- [https://www.rubydoc.info/gems/jace/0.0.1](https://www.rubydoc.info/gems/jace/0.0.1)
14
+ [https://www.rubydoc.info/gems/jace/0.0.2](https://www.rubydoc.info/gems/jace/0.0.2)
15
15
 
16
16
  Installation
17
17
  ---------------
@@ -1 +1,2 @@
1
- ignore: []
1
+ ignore:
2
+ - lib/jace/version.rb
data/config/yardstick.yml CHANGED
@@ -1,4 +1,4 @@
1
- threshold: 100
1
+ threshold: 90
2
2
  require_exact_threshold: false
3
3
  rules:
4
4
  ApiTag::Presence:
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
 
data/jace.jpg ADDED
Binary file
@@ -0,0 +1,88 @@
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
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
+ # @private
55
+ #
56
+ # Perform actions from list
57
+ #
58
+ # @list [Array<Symbol,Proc>] methods and procs to
59
+ # be executed
60
+ #
61
+ # Execute each proc and call method in the list
62
+ # within the given context
63
+ #
64
+ # @return [Array<Proc>]
65
+ def execute_actions(list)
66
+ actions(list).each do |action|
67
+ context.instance_eval(&action)
68
+ end
69
+ end
70
+
71
+ # @private
72
+ #
73
+ # Transforms the input object into a Proc
74
+ #
75
+ # @param list [Symbol,Proc] method or proc to be transformed
76
+ #
77
+ # @return [Proc]
78
+ def actions(list)
79
+ list.map do |entry|
80
+ if entry.is_a?(Proc)
81
+ proc(&entry)
82
+ else
83
+ proc { send(entry) }
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
data/lib/jace/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Jace
4
- VERSION = '0.0.1'
3
+ module Jace
4
+ VERSION = '0.0.2'
5
5
  end
data/lib/jace.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # @api public
4
4
  # @author darthjee
5
- class Jace
6
- autoload :VERSION, 'jace/version'
5
+ module Jace
6
+ autoload :VERSION, 'jace/version'
7
+ autoload :Executer, 'jace/executer'
7
8
  end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Jace::Executer do
6
+ let(:person) { Person.new }
7
+
8
+ describe '.call' do
9
+ context 'without before or after' do
10
+ let(:result) do
11
+ described_class.call(context: person) { 10 }
12
+ end
13
+
14
+ it 'returns re result of block call' do
15
+ expect(result).to eq(10)
16
+ end
17
+
18
+ it 'executes in current context ignoring given context' do
19
+ expect(described_class.call(context: person) { self })
20
+ .to eq(self)
21
+ end
22
+ end
23
+
24
+ context 'with before option' do
25
+ context 'with symbol' do
26
+ let(:result) do
27
+ described_class.call(before: :init_age, context: person) do
28
+ person.age
29
+ end
30
+ end
31
+
32
+ it 'returns result after running before code' do
33
+ expect(result).to eq(1)
34
+ end
35
+
36
+ it 'changes state of context object' do
37
+ expect { result }.to change(person, :age)
38
+ .from(nil).to(1)
39
+ end
40
+ end
41
+
42
+ context 'with block' do
43
+ let(:block) { proc { @age = 10 } }
44
+
45
+ let(:result) do
46
+ described_class.call(before: block, context: person) { person.age }
47
+ end
48
+
49
+ it 'returns result after running before code' do
50
+ expect(result).to eq(10)
51
+ end
52
+
53
+ it 'changes state of context object' do
54
+ expect { result }.to change(person, :age)
55
+ .from(nil).to(10)
56
+ end
57
+ end
58
+
59
+ context 'with array' do
60
+ let(:block) { proc { @brothers = @age ? 2 : 10 } }
61
+ let(:before) { [:init_age, block] }
62
+
63
+ let(:result) do
64
+ described_class.call(before: before, context: person) do
65
+ person.age + person.brothers
66
+ end
67
+ end
68
+
69
+ it 'returns result after running all before code' do
70
+ expect(result).to eq(3)
71
+ end
72
+
73
+ it 'changes state of context object by block call' do
74
+ expect { result }.to change(person, :brothers)
75
+ .from(0).to(2)
76
+ end
77
+
78
+ it 'changes state of context object by method call' do
79
+ expect { result }.to change(person, :age)
80
+ .from(nil).to(1)
81
+ end
82
+ end
83
+ end
84
+
85
+ context 'with after option' do
86
+ context 'with symbol' do
87
+ let(:result) do
88
+ described_class.call(after: :init_age, context: person) { person.age }
89
+ end
90
+
91
+ it 'returns result before running after code' do
92
+ expect(result).to be_nil
93
+ end
94
+
95
+ it 'changes state of context object' do
96
+ expect { result }.to change(person, :age)
97
+ .from(nil).to(1)
98
+ end
99
+ end
100
+
101
+ context 'with block' do
102
+ let(:block) { proc { @age = 10 } }
103
+
104
+ let(:result) do
105
+ described_class.call(after: block, context: person) { person.age }
106
+ end
107
+
108
+ it 'returns result before running after code' do
109
+ expect(result).to be_nil
110
+ end
111
+
112
+ it 'changes state of context object' do
113
+ expect { result }.to change(person, :age)
114
+ .from(nil).to(10)
115
+ end
116
+ end
117
+
118
+ context 'with array' do
119
+ let(:block) { proc { @brothers = @age ? 2 : 10 } }
120
+ let(:after) { [:init_age, block] }
121
+
122
+ let(:result) do
123
+ described_class.call(after: after, context: person) do
124
+ person.age.to_i + person.brothers
125
+ end
126
+ end
127
+
128
+ it 'returns result before running all after code' do
129
+ expect(result).to eq(0)
130
+ end
131
+
132
+ it 'changes state of context object by block call' do
133
+ expect { result }.to change(person, :brothers)
134
+ .from(0).to(2)
135
+ end
136
+
137
+ it 'changes state of context object by method call' do
138
+ expect { result }.to change(person, :age)
139
+ .from(nil).to(1)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Person
4
+ attr_accessor :age
5
+
6
+ def brothers
7
+ @brothers ||= 0
8
+ end
9
+
10
+ private
11
+
12
+ def init_age
13
+ @age ||= 1
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.1
4
+ version: 0.0.2
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-11 00:00:00.000000000 Z
11
+ date: 2020-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -234,7 +234,7 @@ dependencies:
234
234
  - - '='
235
235
  - !ruby/object:Gem::Version
236
236
  version: 0.9.9
237
- description: ''
237
+ description: Gem focused on events for meta-programing
238
238
  email:
239
239
  - darthjee@gmail.com
240
240
  executables: []
@@ -257,13 +257,16 @@ files:
257
257
  - config/yardstick.yml
258
258
  - docker-compose.yml
259
259
  - jace.gemspec
260
+ - jace.jpg
260
261
  - lib/jace.rb
262
+ - lib/jace/executer.rb
261
263
  - lib/jace/version.rb
262
264
  - spec/integration/readme/.keep
263
265
  - spec/integration/yard/.keep
266
+ - spec/lib/jace/executer_spec.rb
264
267
  - spec/spec_helper.rb
265
- - spec/support/fixture_helpers.rb
266
268
  - spec/support/models/.keep
269
+ - spec/support/models/person.rb
267
270
  - spec/support/shared_examples/.keep
268
271
  homepage: https://github.com/darthjee/jace
269
272
  licenses: []
@@ -287,5 +290,5 @@ rubyforge_project:
287
290
  rubygems_version: 2.7.6
288
291
  signing_key:
289
292
  specification_version: 4
290
- summary: ''
293
+ summary: Gem focused on events for meta-programing
291
294
  test_files: []
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support'
4
-
5
- module FixtureHelpers
6
- def load_fixture_file(filename)
7
- File.read(['./spec/', 'fixtures', filename].join('/'))
8
- end
9
-
10
- def load_json_fixture_file(filename)
11
- cached_json_fixture_file(filename)
12
- end
13
-
14
- private
15
-
16
- def cached_json_fixture_file(filename)
17
- ActiveSupport::JSON.decode(load_fixture_file(filename))
18
- end
19
- end
20
-
21
- RSpec.configuration.include FixtureHelpers