Class: RactorRailsShim::DeviseMappingSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor_rails_shim/patches/callables.rb

Overview

Shareable snapshot of a Devise::Mapping. The real Mapping holds an unshareable lambda (failure_app) plus a default-proc Hash (controllers), so it can't be Ractor.make_shareable'd. Request-time code only reads a handful of attributes (name, to/class, router_name, controllers, ...), which are all shareable values. We copy those now (in main) into a frozen Plain Old Object that plays the role of the Mapping in workers.

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ DeviseMappingSnapshot

Returns a new instance of DeviseMappingSnapshot.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ractor_rails_shim/patches/callables.rb', line 56

def initialize(mapping)
  @name         = mapping.name
  @klass        = mapping.to
  @router_name  = mapping.instance_variable_get(:@router_name)
  @singular     = mapping.instance_variable_get(:@singular)
  @scoped_path  = mapping.instance_variable_get(:@scoped_path)
  @path         = mapping.instance_variable_get(:@path)
  @path_prefix  = mapping.instance_variable_get(:@path_prefix)
  @format       = mapping.instance_variable_get(:@format)
  @sign_out_via = mapping.instance_variable_get(:@sign_out_via)
  @modules      = mapping.modules
  @strategies   = mapping.strategies
  @routes       = mapping.routes
  @used_helpers = mapping.used_helpers
  # controllers is a Hash with a default proc (unshareable) — copy the
  # entries into a plain frozen Hash.
  h = {}
  RactorRailsShim._swallow("devise mapping controllers") do
    mapping.controllers.each { |k, v| h[k] = v }
  end
  @controllers = h.freeze
  # failure_app is either Devise::FailureApp (a shareable class) or a
  # lambda (when configured as a String) — keep only the shareable class.
  fa = mapping.instance_variable_get(:@failure_app)
  fa = ::Devise::FailureApp unless fa.is_a?(Class)
  @failure_app = fa
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/ractor_rails_shim/patches/callables.rb', line 111

def method_missing(method, *args)
  s = method.to_s
  if s.end_with?("?") && args.empty?
    @modules.include?(s.chomp("?").to_sym)
  else
    super
  end
end

Instance Method Details

#authenticatable?Boolean

Returns:

  • (Boolean)


100
# File 'lib/ractor_rails_shim/patches/callables.rb', line 100

def authenticatable?; @modules.any? { |m| m.to_s =~ /authenticatable/ }; end

#controllersObject



98
# File 'lib/ractor_rails_shim/patches/callables.rb', line 98

def controllers; @controllers; end

#failure_appObject



99
# File 'lib/ractor_rails_shim/patches/callables.rb', line 99

def failure_app; @failure_app; end

#formatObject



92
# File 'lib/ractor_rails_shim/patches/callables.rb', line 92

def format; @format; end

#fullpathObject



102
# File 'lib/ractor_rails_shim/patches/callables.rb', line 102

def fullpath; "/#{@path_prefix}/#{@path}".squeeze("/"); end

#modulesObject



94
# File 'lib/ractor_rails_shim/patches/callables.rb', line 94

def modules; @modules; end

#nameObject



85
# File 'lib/ractor_rails_shim/patches/callables.rb', line 85

def name; @name; end

#no_input_strategiesObject



101
# File 'lib/ractor_rails_shim/patches/callables.rb', line 101

def no_input_strategies; @strategies & Devise::NO_INPUT; end

#pathObject



90
# File 'lib/ractor_rails_shim/patches/callables.rb', line 90

def path; @path; end

#path_prefixObject



91
# File 'lib/ractor_rails_shim/patches/callables.rb', line 91

def path_prefix; @path_prefix; end

#respond_to_missing?(method, _) ⇒ Boolean

Devise::Mapping defines one x? predicate per Devise module (confirmable?, rememberable?, registerable?, ...) via add_module. Rather than enumerate them, fall back for any x? predicate to checking @modules — matching the generated behaviour.

Returns:

  • (Boolean)


107
108
109
# File 'lib/ractor_rails_shim/patches/callables.rb', line 107

def respond_to_missing?(method, _)
  method.to_s.end_with?("?") || super
end

#router_nameObject



87
# File 'lib/ractor_rails_shim/patches/callables.rb', line 87

def router_name; @router_name; end

#routesObject



96
# File 'lib/ractor_rails_shim/patches/callables.rb', line 96

def routes; @routes; end

#scoped_pathObject



89
# File 'lib/ractor_rails_shim/patches/callables.rb', line 89

def scoped_path; @scoped_path; end

#sign_out_viaObject



93
# File 'lib/ractor_rails_shim/patches/callables.rb', line 93

def sign_out_via; @sign_out_via; end

#singularObject



88
# File 'lib/ractor_rails_shim/patches/callables.rb', line 88

def singular; @singular; end

#strategiesObject



95
# File 'lib/ractor_rails_shim/patches/callables.rb', line 95

def strategies; @strategies; end

#toObject



86
# File 'lib/ractor_rails_shim/patches/callables.rb', line 86

def to; @klass; end

#used_helpersObject



97
# File 'lib/ractor_rails_shim/patches/callables.rb', line 97

def used_helpers; @used_helpers; end