From e8fd32769d9051deee1c7a8b7ae6a713fe8fd393 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 6 Mar 2024 17:23:07 +1300 Subject: [PATCH] `Console::Filter` can work with Ractor. (#54) --- lib/console/filter.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/console/filter.rb b/lib/console/filter.rb index 94630ca..18218f0 100644 --- a/lib/console/filter.rb +++ b/lib/console/filter.rb @@ -12,6 +12,17 @@ module Console UNKNOWN = 'unknown' class Filter + if Object.const_defined?(:Ractor) + def self.define_immutable_method(name, &block) + block = Ractor.make_shareable(block) + self.define_method(name, &block) + end + else + def self.define_immutable_method(name, &block) + define_method(name, &block) + end + end + def self.[] **levels klass = Class.new(self) minimum_level, maximum_level = levels.values.minmax @@ -24,17 +35,17 @@ def self.[] **levels levels.each do |name, level| const_set(name.to_s.upcase, level) - define_method(name) do |subject = nil, *arguments, **options, &block| + define_immutable_method(name) do |subject = nil, *arguments, **options, &block| if self.enabled?(subject, level) self.call(subject, *arguments, severity: name, **options, **@options, &block) end end - define_method("#{name}!") do + define_immutable_method("#{name}!") do @level = level end - define_method("#{name}?") do + define_immutable_method("#{name}?") do @level <= level end end