onuro 0.1.3 → 0.1.4

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: ba55460305728147a6a6902162062d320bb2c8f3324b7001b2177b46ef5d297f
4
- data.tar.gz: 426b556b04838c980ee44fee95210322ed89eeef0e522de578e3358183b0e88d
3
+ metadata.gz: 69886eaf6e1ecb4acd854b3a6fdc261b768f1072b5983bacc3599654598850fd
4
+ data.tar.gz: 58774fa0bda58c4d715d22ca498a49d9dc97cdde01f07fbfed7e127b6ea4f308
5
5
  SHA512:
6
- metadata.gz: 8e74ec30d7458cfb9f27d451181a1abd5272e4e7c0f8e715a1e080fd672f0d22b773cd48a36cca5b1047af3a0400b2b6b8eea5cbe7cbb572c32ebeb18c77cb45
7
- data.tar.gz: 1c8c9b00aabdb5138095427d48eee6c7965fd69a17da7d52d616f6d758f3a1f3eafd726f146d9121364e9e354803b2e197d20c8ccabf7b251ee1643ec27b782f
6
+ metadata.gz: 885e050ca969163f3bdf208b09befe7977bce2e9cd8366f6fc2426a1c4f9ec17d0d5bd550e01389dc0f76da15d02213448dae56962cde5bacd08c0913e7b9906
7
+ data.tar.gz: 129db0caa43e8311da60330df7cbe4b71708fb28689b2d1dfeb25072d37c867e27b8cc23ba038be03aa37f077ae27fb338bba150fa92981e5a7e5693b3b979c9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- onuro (0.1.3)
4
+ onuro (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,14 +1,12 @@
1
1
  # Onuro
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/onuro`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Workflow Engine based in events that execute a collection of rules defined in the event configuration definition
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
- ```ruby
9
+ ```bash
12
10
  gem 'onuro'
13
11
  ```
14
12
 
@@ -22,15 +20,17 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- The easiest way to load Event + Rules configuration in the **Engine** is using the configuration blocks. Its important to mention these global pre-configured events, would be changed when in runt-time an **engine instance** is begin fetched with new events.
23
+ The easiest way to load *Events + Rules* configuration in the **Engine** is using the configuration blocks. Its important to mention these global pre-configured events, would not be changed when in runt-time an **engine instance** is begin fetched with new events.
26
24
 
27
25
  ```ruby
28
26
  class Rule1 < Onuro::BaseRule; end
29
27
  class Rule2 < Onuro::BaseRule; end
30
28
  class Rule3 < Onuro::BaseRule; end
31
- class MyCustomEventStrategy < Onuro::DefaultEventStrategy; end
32
-
29
+ class MyCustomEventStrategy < Onuro::DefaultEventStrategy
30
+ # override here the methods to execute your custom logic before/after each rule
31
+ end
33
32
 
33
+ # Onuro::Engine configuration block
34
34
  Onuro::Engine.configure do |config|
35
35
  config.add_event(:event_one) do |event|
36
36
  event.add_ruleset_stage [
@@ -54,23 +54,107 @@ Onuro::Engine.configure do |config|
54
54
  end
55
55
 
56
56
  config.add_event(:event_four) do |event|
57
- event.add_rule_stage Onuro::RuleStage.default_ruleset_stage_factory([Rule1, Rule2, Rule3])
57
+ event.add_ruleset_stage Onuro::RuleStage.default_ruleset_stage_factory([Rule1, Rule2, Rule3])
58
58
  end
59
59
  end
60
+
61
+ # When the *engine object is created, will fetch the pre-configured events for instant use,
62
+ # saving the boilerplate object setup for your common events and rule sets.
63
+ exec_result = Onuro::Engine.new.execute(:event_four)
64
+ ```
65
+
66
+ ### Context
67
+
68
+ This is the instance object that will be passed through all the event execution process. Here you can pass key/value params that will be help you while the **Rules** are being executed. Things like *member_id*, *current_date* (you name it) will be passed from the engine to all the way down until the end of the execution. Since is a reference object, you can add new key/value states during the *ruleset* execution.
69
+
70
+ One way:
71
+ ```ruby
72
+ Onuro::Engine.new.execute(:my_event) do |context|
73
+ context.add(:member_id, 1)
74
+ context.add(:current_date, '01/01/2019')
75
+ end
60
76
  ```
61
77
 
78
+ Another way:
79
+ ```ruby
80
+ context = Onuro::ContextBuilder.build do |context|
81
+ context.add(:member_id, 1)
82
+ context.add(:current_date, '01/01/2019')
83
+ end
84
+
85
+ Onuro::Engine.new.execute(:my_event, context)
86
+ ```
87
+
88
+ Another way with *double splat* operators in the *Context* constructor:
89
+ ```ruby
90
+ context = Onuro::Context.new(member_id: 1, current_date: '01/01/2019')
91
+
92
+ Onuro::Engine.new.execute(:my_event, context)
93
+ ```
94
+
95
+ **Note:** If you do not provide a context, an empty one will be created.
96
+
62
97
  ### Engine
63
98
 
64
- ### Rule
99
+ The **engine** is the object that can hold a list of **events**, where the desired **event** is executed and additionally, you can provide a **context** object with helpful key/value data that will help you in your **rule** processing.
100
+
101
+ ```ruby
102
+ context = Onuro::Context.new(...your data..)
103
+ engine = Onuro::Engine.new
104
+
105
+ engine.add_events([event1, event2])
106
+ engine.new.execute(:my_event, context)
107
+ ```
108
+
109
+ ### Rules
110
+
111
+ Are the foundation building blocks of the *rulesets* and this is where you can place your **logic** divided in segments(**rules**).
112
+
113
+ All the rules processed by the engine is recommended to inherith from **Onuro::BaseRule** since this base class give you access to the **logger** support and easy access to the **ExecutionResult** constants used by the engine. Basically you need to override/implement **execute** method accepting as input an **context** object. The **rule** need to return and **ExecutionResult** constant. (More about extending this later in this README)
114
+
115
+ A collection of *rules* is holded by an **event** object
116
+
117
+ ```ruby
118
+ class MyRule < Onuro::BaseRule
119
+ def execute(context)
120
+ context[:mul_result] = context[:first_operand] * context[:second_operand]
121
+ SUCCESSFUL
122
+ end
123
+ end
124
+
125
+ ruleset_stage = Onuro::RuleStage.default_ruleset_stage_factory([MyRule])
126
+ event.add_ruleset_stage(ruleset_stage)
127
+ ```
128
+
129
+ Also, you can execute a single rule without using the full **Onuro::Engine**
130
+
131
+ ```ruby
132
+ MyRule.new.execute(context)
133
+ ```
65
134
 
66
135
  ### RuleStage
67
136
 
137
+ Since *Onuro* allows the rule execution, you can diseable or add a particular **order** of execution to all the rules in a rulseset, the **RuleStage** class help us to do that. You cannot add the **Rule** object instances directly in the event, without adding first each rule into a **RuleStage** object.
138
+
139
+ ```ruby
140
+ event.add_ruleset_stage [
141
+ Onuro::RuleStage.new(rule: Rule1, enabled: true, order: 1),
142
+ Onuro::RuleStage.new(rule: Rule3, enabled: true, order: 2)
143
+ ]
144
+ ```
145
+
146
+ If you want to add all the rules enabled by default, and the execution order will be based in the order that you define the ruleset collection, you can use a **RuleStage** factory to save all these object setup:
147
+
148
+ ```ruby
149
+ event.add_ruleset_stage Onuro::RuleStage.default_ruleset_stage_factory([Rule1, Rule2, Rule3])
150
+ ```
151
+
68
152
  ### Events
69
153
 
70
154
  Events are the component that hold an *rulset stage* composed of a list of rules and their settings for execution. The easiest way to define and event is creating the instance object always with the event name required. In order to create complex events with more settings, take a look to the *Event Builder* section.
71
155
 
72
156
  ```ruby
73
- event = Event.new(:my_event)
157
+ event = Onuro::Event.new(:my_event)
74
158
  engine.add_event(event)
75
159
  engine.execute(:my_event)
76
160
  ```
@@ -80,7 +164,7 @@ engine.execute(:my_event)
80
164
  An easier way to create an event, is using the **EventBuilder** class based on the *builder pattern*. This gives a lot of flexibility when you are creating the events and avoid having a lot or parameters in the constructor and assembling all the event internals, and easy to to be plugged in you **Engine** class instance.
81
165
 
82
166
  ```ruby
83
- event = EventBuilder.build(:test_ruleset) do |builder|
167
+ event = Onuro::EventBuilder.build(:test_ruleset) do |builder|
84
168
  builder.add_ruleset_stage([rule1, rule2, rule3])
85
169
  builder.add_event_strategy(MyCustomEventStrategy.new)
86
170
  builder.exec_order(:desc)
@@ -113,6 +197,18 @@ You only need to implement the methods **before_rule_exec** and **after_rule_exe
113
197
 
114
198
  If **before_rule_exec** returns false, skip the current rule execution and move to the next one in the ruleset list.
115
199
 
200
+ ### ExecutionResult
201
+
202
+ TBD
203
+
204
+ ### Determining succcess or failure of the event processing
205
+
206
+ TBD
207
+
208
+ ### Logging
209
+
210
+ TBD
211
+
116
212
  ## Development
117
213
 
118
214
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -4,6 +4,8 @@ require 'onuro/version'
4
4
  require 'onuro/execution_result'
5
5
  require 'onuro/logging'
6
6
  require 'onuro/engine_configuration'
7
+ require 'onuro/context_builder'
8
+ require 'onuro/context'
7
9
  require 'onuro/engine'
8
10
  require 'onuro/default_event_strategy'
9
11
  require 'onuro/event_builder'
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Onuro
4
+ class Context
5
+ attr_reader :data
6
+
7
+ def initialize(**options)
8
+ @data = Hash.new(0)
9
+ @data.merge!(options)
10
+ end
11
+
12
+ def add(key, value)
13
+ data[key] = value
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Onuro
4
+ class ContextBuilder
5
+ attr_accessor :context
6
+
7
+ def initialize
8
+ self.context = Context.new
9
+ end
10
+
11
+ def add(key, value)
12
+ context.add(key, value)
13
+ end
14
+
15
+ def self.build
16
+ builder = new
17
+ yield builder if block_given?
18
+ builder.context
19
+ end
20
+ end
21
+ end
@@ -67,9 +67,10 @@ module Onuro
67
67
  raise InvalidEventNameException unless result
68
68
  end
69
69
 
70
- def execute(event_name, context = {})
70
+ def execute(event_name, context = Context.new)
71
71
  raise InvalidEventNameException unless event?(event_name)
72
72
 
73
+ context.data.merge! ContextBuilder.build.data
73
74
  events[event_name].execute(context)
74
75
  end
75
76
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Onuro
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onuro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Reyes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2019-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -157,6 +157,8 @@ files:
157
157
  - bin/setup
158
158
  - lib/onuro.rb
159
159
  - lib/onuro/base_rule.rb
160
+ - lib/onuro/context.rb
161
+ - lib/onuro/context_builder.rb
160
162
  - lib/onuro/default_event_strategy.rb
161
163
  - lib/onuro/engine.rb
162
164
  - lib/onuro/engine_configuration.rb