torm 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf7d146ecc3d6ebd40de586588fefc52a94d4552
4
- data.tar.gz: c861e3383026631ec4c2e293555e16c30963bb5f
3
+ metadata.gz: 7d836591fc7a86a288f8571e032ee400b2f0f54f
4
+ data.tar.gz: 1b6d4c109b4ec5fb035b87c4187c25e4efc17019
5
5
  SHA512:
6
- metadata.gz: d55a9a923f5fb23af03694b42e01c6bad1c295f4e0529be376832c71b6e9054ab2d4816c4cf99fbe22dfb729ea832c745b74a23e1909da904eee0e493606eb72
7
- data.tar.gz: 43d60da6351014ab2576d994c60333e17749883801fa1aaadc8078166860120abadbc7e45254a649b8130933f23ff747ced466fab8f744a883575af2da4bde93
6
+ metadata.gz: 3944504f6b1263b60382ea19422484372630907caaccebdb64adce3cd3bb13d73d55c96ba32874b14ff4b7c3872e44619a505722f74341776d7e10dca5fd1670
7
+ data.tar.gz: 843de465105451cb798e027ab185b8d6316c9b2b4e805bad5a0acb0fafba6334776404e793e49bea554dd3839ba1cf02adf1db23a70d9fc0c7ea69f1502e1115
data/README.md CHANGED
@@ -29,12 +29,25 @@ Torm.default_rules_file = Rails.root.join('tmp/my_rules.json').to_s
29
29
  # Torm.set_defaults will load an engine if a rules file exists, otherwise you get an empty engine.
30
30
  # Add rules, then after the block it will automatically save the rules file when new rules were changed.
31
31
  Torm.set_defaults do |engine|
32
- # Add a new rule named 'Happy', with a 'default' policy value of true
32
+ # Setup custom priorities. Higher priority rules take precedence over lower ones.
33
+ engine.priorities = [:high, :medium, :low, :default]
34
+
35
+ # Add a new rule named 'Happy', with a value of true as 'default' policy
33
36
  engine.add_rules 'Happy', true, :default do |rule|
34
- # Add a variant on the 'Happy' rule: we're not happy when it rains
35
- rule.variant false, :default, rain: true
36
- # Another variant. Due to the abundance of rain, in Great Britain the law dictates you're still happy when it rains.
37
- rule.variant true, :law, rain: true, country: 'GB'
37
+ # Setup general conditions for the rest of the block
38
+ rule.conditions rain: true do |rule|
39
+ # By default, nobody likes rain...
40
+ rule.variation false, :default
41
+ # ...except for the Brits. They love rain :-)
42
+ rule.variation true, :high, country: 'GB'
43
+
44
+ rule.conditions umbrella: true do |rule|
45
+ # People with an umbrella don't mind rain.
46
+ rule.variation true, :high
47
+ # Red umbrellas still make people grumpy, though.
48
+ rule.variation false, :medium, umbrella_color: :red
49
+ end
50
+ end
38
51
  end
39
52
  end
40
53
 
@@ -23,6 +23,12 @@ module Torm
23
23
 
24
24
  # Add a new rule.
25
25
  # Will mark the engine as dirty when a rules was added.
26
+ #
27
+ # @param [String] name
28
+ # @param [true, false, String, Numeric, Range, Hash] value Either a simple type, or a Range, or a Hash with a :minimum or :maximum key to represent a Range extreme.
29
+ # @param [Symbol] policy The source of the rule and thus how heavy it weighs.
30
+ # @param [Hash] conditions Conditions that must be met before a rule evaluates to return this value.
31
+ #
26
32
  # @return [Torm::RulesEngine] (self) Returns the engine that rules were added to.
27
33
  def add_rule(name, value, policy, conditions={})
28
34
  raise "Illegal policy: #{policy.inspect}, must be one of: #{policies.inspect}" unless policies.include?(policy)
@@ -41,16 +47,41 @@ module Torm
41
47
 
42
48
  # Simple helper class to add the block DSL to add_rules
43
49
  class RuleVariationHelper
44
- def initialize(engine, name)
50
+ def initialize(engine, name, **conditions)
45
51
  @engine = engine
46
52
  @name = name
53
+ @conditions = conditions
47
54
  end
48
55
 
49
56
  def variation(value, policy, **conditions)
50
- @engine.add_rule(@name, value, policy, conditions)
57
+ @engine.add_rule(@name, value, policy, @conditions.merge(conditions))
58
+ nil
59
+ end
60
+
61
+ # @yield [Torm::RulesEngine::RulesVariationHelper]
62
+ def conditions(**conditions)
63
+ engine = self.class.new(@engine, @name, @conditions.merge(conditions))
64
+ yield engine
65
+ nil
51
66
  end
52
67
  end
53
68
 
69
+ # Add multiple rules via the block syntax:
70
+ #
71
+ # @example
72
+ #
73
+ # engine = Torm::RulesEngine.new
74
+ # engine.add_rules 'Happy', true, :default do |rule|
75
+ # rule.variant false, :default, rain: true
76
+ # end
77
+ #
78
+ # @param [String] name
79
+ # @param [true, false, String, Numeric, Range, Hash] value Either a simple type, or a Range, or a Hash with a :minimum or :maximum key to represent a Range extreme.
80
+ # @param [Symbol] policy The source of the rule and thus how heavy it weighs.
81
+ #
82
+ # @yield [Torm::RulesEngine::RuleVariationHelper]
83
+ #
84
+ # @return [Torm::RulesEngine] Returns self
54
85
  def add_rules(name, value, policy)
55
86
  # Add the default rule
56
87
  add_rule(name, value, policy)
@@ -1,3 +1,3 @@
1
1
  module Torm
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -101,8 +101,8 @@ describe Torm::RulesEngine do
101
101
  end
102
102
  end
103
103
 
104
- describe '#add_rules block syntax with #variation' do
105
- it 'should just work' do
104
+ describe '#add_rules block syntax' do
105
+ it 'should yield an object that responds to :variation to add rules' do
106
106
  engine.add_rules 'Happy', true, :default do |rule|
107
107
  # Nobody likes rain...
108
108
  rule.variation false, :default, rain: true
@@ -111,9 +111,34 @@ describe Torm::RulesEngine do
111
111
  end
112
112
 
113
113
  assert engine.decide('Happy')
114
- assert !engine.decide('Happy', rain: true)
114
+ refute engine.decide('Happy', rain: true)
115
115
  assert engine.decide('Happy', rain: true, country: 'GB')
116
116
  end
117
+
118
+ it 'should yield an object that responds to :conditions to yield a block with those conditions applied' do
119
+ engine.add_rules 'Happy', true, :default do |rule|
120
+ # Setup general conditions for the entire block
121
+ rule.conditions rain: true do |rule|
122
+ # Nobody likes rain...
123
+ rule.variation false, :default
124
+ # ...except for the Brits :-)
125
+ rule.variation true, :law, country: 'GB'
126
+
127
+ rule.conditions umbrella: true do |rule|
128
+ # ...or people with an umbrella
129
+ rule.variation true, :law
130
+ # Red umbrellas still make people grumpy, though.
131
+ rule.variation false, :law, umbrella_color: :red
132
+ end
133
+ end
134
+ end
135
+
136
+ assert engine.decide('Happy')
137
+ refute engine.decide('Happy', rain: true)
138
+ assert engine.decide('Happy', rain: true, country: 'GB')
139
+ assert engine.decide('Happy', rain: true, umbrella: true)
140
+ refute engine.decide('Happy', rain: true, umbrella: true, umbrella_color: :red)
141
+ end
117
142
  end
118
143
  end
119
144
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wes Oldenbeuving
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2014-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json