Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console::Filter can work with Ractor. #54

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading