Skip to content

Commit

Permalink
Console::Filter can work with Ractor. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Mar 6, 2024
1 parent 99763e6 commit e8fd327
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/console/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e8fd327

Please sign in to comment.