Skip to content

Commit

Permalink
Eliminate usage of OpenStruct
Browse files Browse the repository at this point in the history
OpenStruct design cause it to have non-local performance impact,
especially with JIT compilers.

See ruby/ostruct#51 for full context,
and it Ruby 3.3 OpenStruct usage raise a performance warnings:

```
gems/state_machines-audit_trail-2.0.2/lib/state_machines/audit_trail/transition_auditing.rb:38:
warning: OpenStruct use is discouraged for performance reasons
```

In this case I don't really see any advantage of OpenStruct
over a simple PORO.
  • Loading branch information
byroot committed Jan 22, 2024
1 parent 00b8318 commit 72a5e26
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/state_machines/audit_trail/transition_auditing.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'ostruct'
#
# This module inserts hooks into the state machine.
# The transition_class is the class (optionally specified with the :class option) for the model which
Expand All @@ -7,6 +6,14 @@
# Multiple `SubscriptionStateTransition`s compose the 'audit_trail'.
#
module StateMachines::AuditTrail::TransitionAuditing
class LogInfo
attr_reader :namespace, :to

def initialize(namespace:, to:)
@namespace = namespace
@to = to
end
end

# Hook for audit_trail inside a a state_machine declaration.
#
Expand Down Expand Up @@ -35,7 +42,7 @@ def audit_trail(options = {})
if state_machine.backend.new_record? object
current_state = object.send(state_machine.attribute)
if !current_state.nil?
state_machine.backend.log(object, OpenStruct.new(namespace: state_machine.namespace, to: current_state))
state_machine.backend.log(object, LogInfo.new(namespace: state_machine.namespace, to: current_state))
end
end
end
Expand Down

0 comments on commit 72a5e26

Please sign in to comment.