brule 0.3.1 → 0.4.0

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: 67b697b560f27e984a2ec669e672dee5b1cab777071c88d1b0ea7f1d358e4067
4
- data.tar.gz: 3d9c7e21ca6fecc1a8b3d1b9120e15894dfc7c7e5ecd055d24fb118fb9ee11e2
3
+ metadata.gz: 86b2ca827bc1365eda3e1c2be029086dcbd7777bd70a2ead29469c502d171a13
4
+ data.tar.gz: bc97f46ef11b350b664e6278b9978d75ed85f703681e0e7e896aafe853f55888
5
5
  SHA512:
6
- metadata.gz: b7b7e87db8e466cda35e0330774818f03a75ce5d925f4dcdc394e0bdb1751a5037141bf4490cd602434d3ee56caee16526b40b3fb3fb9d8ddf4e19224a029032
7
- data.tar.gz: 21d8166b5a3aac5cc63ef8cd1414bd2defc05eab1548d85697081d1c3d678ffc42cf9ac960399a61308db6fb99fa60e7e60610a4490ea25d1b33aa8dad13b649
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
- If you use this gem, you'll have a couple of premade elements to build and test
13
- an _engine_ that, given a set of _rules_ and a _context_ will produce a
14
- _result_.
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
+ ![Engine](https://github.com/nicoolas25/brule-rb/blob/master/docs/img/engine.png?raw=true)
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
+ ![Rule](https://github.com/nicoolas25/brule-rb/blob/master/docs/img/rule.png?raw=true)
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
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Brule
4
- autoload :Context, 'brule/context'
5
4
  autoload :Engine, 'brule/engine'
6
5
  autoload :Rule, 'brule/rule'
7
6
  autoload :Utils, 'brule/utils'
data/lib/brule/engine.rb CHANGED
@@ -9,7 +9,7 @@ module Brule
9
9
 
10
10
  def call(context = {})
11
11
  @history = {}
12
- @context = Context.wrap(context)
12
+ @context = context
13
13
  snapshot!(tag: :initial)
14
14
  @rules.each { |rule| apply(rule) }
15
15
  result
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.3.1
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