brule 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -12
- data/lib/brule.rb +0 -1
- data/lib/brule/engine.rb +1 -1
- metadata +1 -2
- data/lib/brule/context.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86b2ca827bc1365eda3e1c2be029086dcbd7777bd70a2ead29469c502d171a13
|
4
|
+
data.tar.gz: bc97f46ef11b350b664e6278b9978d75ed85f703681e0e7e896aafe853f55888
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3369d8945f69fe5334338550207943dd4a200bcbf3ddae031cbed2fa1101dbecacb5f06c38d9999d80d9fbd0d2c5f08da9ce5ad5e3bcec6ea3ddef844c5b31ff
|
7
|
+
data.tar.gz: 5d74c2c6bb63d900b8cff010d5a8266dc7226ab614820c827f808d3c0a6370c297f8e3188f9d9bdde3186cb280dd627aac2dda253cd643ab09c82899dd0bd87c
|
data/README.md
CHANGED
@@ -9,23 +9,26 @@ business rules. It helps when:
|
|
9
9
|
|
10
10
|
## How does it work
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
The idea is very similar to function composition or Rack's middlewares. It is a
|
13
|
+
layering abstraction where each layer works for the next layers in order for the
|
14
|
+
whole to produce a single result.
|
15
|
+
|
16
|
+
An _engine_ respond to `#call`, taking a `context` in argument. It produces a
|
17
|
+
_result_ that is extracted from the _context_ by the `#result` method. Before
|
18
|
+
doing that, the engine apply its _rules_.
|
19
|
+
|
20
|
+

|
21
|
+
|
22
|
+
Each rule have two methods: `#trigger?` and `#apply`. `#apply` runs only when
|
23
|
+
`trigger?` is true. `#apply` writes stuff to the context for other rules and
|
24
|
+
for the engine to produce the result.
|
25
|
+
|
26
|
+

|
15
27
|
|
16
28
|
A typical usage for this kind of engine is to use it to compute the price of a
|
17
29
|
service or a good. But, this is not limited to that use-case as an engine
|
18
30
|
would be able to produce any kind of results.
|
19
31
|
|
20
|
-
An _engine_ orchestrate _rules_ in a way that they would produce enough
|
21
|
-
information for the engine to assemble a _result_. The rules are arranged in an
|
22
|
-
ordered sequence and are picked depending on the _context_. Each rule will write
|
23
|
-
in the context too.
|
24
|
-
|
25
|
-
The idea is very similar to function composition or Rack's middlewares. It is a
|
26
|
-
layering abstraction where each layer works for the next layers in order for the
|
27
|
-
stack to produce a single value.
|
28
|
-
|
29
32
|
## How does it look
|
30
33
|
|
31
34
|
Here is an example from the [Elephant Carpaccio][elephant] kata. The specs are:
|
data/lib/brule.rb
CHANGED
data/lib/brule/engine.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brule
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Zermati
|
@@ -46,7 +46,6 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- README.md
|
48
48
|
- lib/brule.rb
|
49
|
-
- lib/brule/context.rb
|
50
49
|
- lib/brule/engine.rb
|
51
50
|
- lib/brule/rule.rb
|
52
51
|
- lib/brule/utils.rb
|
data/lib/brule/context.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
|
-
module Brule
|
4
|
-
class Context
|
5
|
-
extend Forwardable
|
6
|
-
|
7
|
-
def_delegators :@content, :[], :fetch, :fetch_values, :key?, :[]=, :merge!
|
8
|
-
|
9
|
-
def initialize(hash = {})
|
10
|
-
@content = hash
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize_copy(orig)
|
14
|
-
super
|
15
|
-
@content = orig.instance_variable_get(:@content).dup
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.wrap(context)
|
19
|
-
context.is_a?(self) ? context : new(context)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|