buzz_logic 1.0.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc4c70ca95e3e3e8eed36b92f72f2668f7b5c05a1b0599b930e8caa84983bbd4
4
- data.tar.gz: e073cd8be81197b5c3f418e0b6a2abaada5c8b3f665268c86e150a65aaed23a9
3
+ metadata.gz: 9a998ef0a6a784ee9eca2d8883000f8850822925cad087ccf920bcd92ae4361f
4
+ data.tar.gz: d44764e06bd79f955b6244bbc64245b02c2d186151975eee6138725260b32e9c
5
5
  SHA512:
6
- metadata.gz: 1ec7200ccb43f8fa26c63b30eb2ca5350d27c7308a7ab0bf9d0b15e829398d4bd1bbc8bfbd22d62f4c26b8ac6ef50f433df08e3c02ea35422f3e4d331b4be954
7
- data.tar.gz: d753ba2cafb0636f904a6930d178de911794c9a14b0ba8c9e94fb2fd8b49c0a6235e27e0a161053f74c52028da5b21501d3060714928152105e144d93988cc4b
6
+ metadata.gz: f0c169e21f1b1ec8d7def2b3f3304b6b0c3ef3323551a8c4ea676bd0286d0a43623395c97b070e5cae8dbea3726272ed9e9e5625d74e6e6f4208c46e728478a9
7
+ data.tar.gz: bc8864a7bbedb646757e4910262b208ccf60494ef07cba62724b1d8e21f22f57c35b6a08ae5e5b8b6d14061c696703b36be40b490210edfa82309f2b63326fa2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Buzz Logic - Change Log
2
2
 
3
+ ## [1.0.2] - 2025-06-17
4
+
5
+ - Limit the models methods to the attributes returned from the `attributes` method
6
+ -
7
+ ## [1.0.1] - 2025-06-16
8
+
9
+ - Fix: Ignore method arity
10
+
3
11
  ## [1.0.0] - 2025-06-16
4
12
 
5
13
  - First version of BuzzLogic, extracted from FutureFund.
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 'buzz_logic'
47
+ require "buzz_logic"
48
48
 
49
49
  # Define some objects to evaluate against
50
- student = OpenStruct.new(grade: 5, has_permission_slip: true)
51
- fundraiser = OpenStruct.new(status: 'active', goal_amount: 500)
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
- 'student' => student,
56
- 'fundraiser' => fundraiser
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.
@@ -145,20 +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
- if object.respond_to?(attribute)
149
- method = object.method(attribute)
150
- if method.arity == 0
151
- return method.call
152
- else
153
- raise EvaluationError, "Access to method '#{attribute}' with arguments is not allowed."
154
- end
148
+ unless object.respond_to?(:attributes)
149
+ raise EvaluationError, "Object of type #{object.class} does not have an attributes method"
155
150
  end
156
151
 
157
- raise EvaluationError, "Cannot access '#{attribute}' on object of type #{object.class}"
158
- rescue NoMethodError
159
- raise EvaluationError, "Object does not have attribute '#{attribute}'"
160
- rescue => e
161
- raise EvaluationError, "An error occurred while accessing attribute '#{attribute}': #{e.message}"
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
162
160
  end
163
161
 
164
162
  def perform_operation(op, left, right)
@@ -1,3 +1,3 @@
1
1
  module BuzzLogic
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buzz_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darian Shimy
@@ -32,6 +32,7 @@ licenses:
32
32
  - MIT
33
33
  metadata:
34
34
  allowed_push_host: https://rubygems.org
35
+ rubygems_mfa_required: 'true'
35
36
  homepage_uri: https://github.com/futurefund/buzz_logic
36
37
  source_code_uri: https://github.com/futurefund/buzz_logic
37
38
  changelog_uri: https://github.com/FutureFund/buzz_logic/CHANGELOG.md