Module: RmagickTidy::Registry

Defined in:
lib/rmagick_tidy/registry.rb

Constant Summary collapse

STACK_KEY =
:rmagick_tidy_stack

Class Method Summary collapse

Class Method Details

.currentObject



32
33
34
# File 'lib/rmagick_tidy/registry.rb', line 32

def current
  stack.last
end

.destroy_safely(img) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rmagick_tidy/registry.rb', line 60

def destroy_safely(img)
  return unless img
  return if img.respond_to?(:destroyed?) && img.destroyed?

  img.destroy!
rescue ::Magick::ImageMagickError
  # Swallow Magick-side destroy errors (e.g. DestroyedImageError on races);
  # the goal is to free what we can without raising from `ensure`. Other
  # exceptions (NoMethodError, ArgumentError, etc.) are intentionally not
  # caught here so unrelated bugs surface early.
  nil
end

.in_scope?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rmagick_tidy/registry.rb', line 36

def in_scope?
  !stack.empty?
end

.pop_and_destroyObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rmagick_tidy/registry.rb', line 44

def pop_and_destroy
  scope = stack.pop
  return unless scope

  seen = {}.compare_by_identity
  scope.images.each do |img|
    next if seen[img]

    seen[img] = true
    next if scope.keep?(img)

    destroy_safely(img)
  end
  nil
end

.pushObject



40
41
42
# File 'lib/rmagick_tidy/registry.rb', line 40

def push
  stack.push(Scope.new)
end

.stackObject



28
29
30
# File 'lib/rmagick_tidy/registry.rb', line 28

def stack
  Thread.current[STACK_KEY] ||= []
end