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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3da16ad99e4f0c1397f3b5c45a44f731de0960c116a570f663f2973f0c3c59a5
4
- data.tar.gz: aa46c98615ed142bf4aa5bdc1b38c46f4b27c245c00a9179384749be9891f1d9
3
+ metadata.gz: 9a998ef0a6a784ee9eca2d8883000f8850822925cad087ccf920bcd92ae4361f
4
+ data.tar.gz: d44764e06bd79f955b6244bbc64245b02c2d186151975eee6138725260b32e9c
5
5
  SHA512:
6
- metadata.gz: 41d08fbf2626a6edf9453834dc4e15bdb850a6b119a5cfe113cff15a86d79c6f9b8fc30269641edcde352729cae4c8e8de982c1d797568be6dd9ef576e0a292f
7
- data.tar.gz: 73df6268dbbd07a415ce59d2c6c0f80a860c14d532065af1fd66737346ff374a633dc0325e2a469e463914e62a51639094d1d4c2b30b24916ea5fdcb081e4d23
6
+ metadata.gz: f0c169e21f1b1ec8d7def2b3f3304b6b0c3ef3323551a8c4ea676bd0286d0a43623395c97b070e5cae8dbea3726272ed9e9e5625d74e6e6f4208c46e728478a9
7
+ data.tar.gz: bc8864a7bbedb646757e4910262b208ccf60494ef07cba62724b1d8e21f22f57c35b6a08ae5e5b8b6d14061c696703b36be40b490210edfa82309f2b63326fa2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
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
+ -
3
7
  ## [1.0.1] - 2025-06-16
4
8
 
5
9
  - Fix: Ignore method arity
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,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
- if object.respond_to?(attribute)
149
- method = object.method(attribute)
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
- raise EvaluationError, "Cannot access '#{attribute}' on object of type #{object.class}"
154
- rescue NoMethodError
155
- raise EvaluationError, "Object does not have attribute '#{attribute}'"
156
- rescue => e
157
- 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
158
160
  end
159
161
 
160
162
  def perform_operation(op, left, right)
@@ -1,3 +1,3 @@
1
1
  module BuzzLogic
2
- VERSION = "1.0.1"
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.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darian Shimy