buzz_logic 1.0.1 → 1.0.2
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/CHANGELOG.md +4 -0
- data/README.md +18 -5
- data/lib/buzz_logic/engine.rb +10 -8
- data/lib/buzz_logic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a998ef0a6a784ee9eca2d8883000f8850822925cad087ccf920bcd92ae4361f
|
4
|
+
data.tar.gz: d44764e06bd79f955b6244bbc64245b02c2d186151975eee6138725260b32e9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0c169e21f1b1ec8d7def2b3f3304b6b0c3ef3323551a8c4ea676bd0286d0a43623395c97b070e5cae8dbea3726272ed9e9e5625d74e6e6f4208c46e728478a9
|
7
|
+
data.tar.gz: bc8864a7bbedb646757e4910262b208ccf60494ef07cba62724b1d8e21f22f57c35b6a08ae5e5b8b6d14061c696703b36be40b490210edfa82309f2b63326fa2
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -44,16 +44,25 @@ The primary interface for the engine is the BuzzLogic::RulesEngine.evaluate meth
|
|
44
44
|
### Basic Example
|
45
45
|
|
46
46
|
```ruby
|
47
|
-
require
|
47
|
+
require "buzz_logic"
|
48
48
|
|
49
49
|
# Define some objects to evaluate against
|
50
|
-
|
51
|
-
|
50
|
+
Student = Struct.new(:grade, :has_permission_slip) do
|
51
|
+
def attributes
|
52
|
+
to_h.map { |k, v| [ k.to_s, v ] }.to_h
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Fundraiser = Struct.new(:status, :goal_amount) do
|
57
|
+
def attributes
|
58
|
+
to_h.transform_keys(&:to_s)
|
59
|
+
end
|
60
|
+
end
|
52
61
|
|
53
62
|
# The context maps the names used in the rule to the objects
|
54
63
|
context = {
|
55
|
-
|
56
|
-
|
64
|
+
"student" => Student.new(grade: 5, has_permission_slip: true),
|
65
|
+
"fundraiser" => Fundraiser.new(status: 'active', goal_amount: 500)
|
57
66
|
}
|
58
67
|
|
59
68
|
# Define a rule
|
@@ -86,6 +95,10 @@ puts "Is the student eligible? #{result}" # => true
|
|
86
95
|
|
87
96
|
Parentheses `()` can be used to group expressions and control precedence.
|
88
97
|
|
98
|
+
## Attributes
|
99
|
+
|
100
|
+
The object must respond to the `attributes` method and return a hash with string keys (not symbols).
|
101
|
+
|
89
102
|
### Security
|
90
103
|
|
91
104
|
Security is the queen bee of BuzzLogic's design. Unlike approaches that use eval, BuzzLogic parses the rule into an Abstract Syntax Tree (AST) and then interprets it.
|
data/lib/buzz_logic/engine.rb
CHANGED
@@ -145,16 +145,18 @@ module BuzzLogic
|
|
145
145
|
def resolve_attribute(object, attribute)
|
146
146
|
raise EvaluationError, "Cannot access attribute on nil" if object.nil?
|
147
147
|
|
148
|
-
|
149
|
-
|
150
|
-
return method.call
|
148
|
+
unless object.respond_to?(:attributes)
|
149
|
+
raise EvaluationError, "Object of type #{object.class} does not have an attributes method"
|
151
150
|
end
|
152
151
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
152
|
+
attrs = object.attributes
|
153
|
+
attr_name = attribute.to_s
|
154
|
+
|
155
|
+
if attrs.key?(attr_name)
|
156
|
+
attrs[attr_name]
|
157
|
+
else
|
158
|
+
raise EvaluationError, "Object of type #{object.class} does not have attribute '#{attr_name}'"
|
159
|
+
end
|
158
160
|
end
|
159
161
|
|
160
162
|
def perform_operation(op, left, right)
|
data/lib/buzz_logic/version.rb
CHANGED