Skip to content

Commit

Permalink
Use synthetic include for CurrentAttributes generator
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Dec 5, 2023
1 parent dc1f405 commit 5fb4c91
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
57 changes: 33 additions & 24 deletions lib/tapioca/dsl/compilers/active_support_current_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,27 @@ module Compilers
# # typed: true
#
# class Current
# sig { returns(T.untyped) }
# def self.account; end
# include CurrentAttributesMethods
#
# sig { returns(T.untyped) }
# def account; end
# module CurrentAttributesMethods
# sig { returns(T.untyped) }
# def self.account; end
#
# sig { params(account: T.untyped).returns(T.untyped) }
# def self.account=(account); end
# sig { returns(T.untyped) }
# def account; end
#
# sig { params(account: T.untyped).returns(T.untyped) }
# def account=(account); end
# sig { params(account: T.untyped).returns(T.untyped) }
# def self.account=(account); end
#
# sig { params(user_id: Integer).void }
# def self.authenticate(user_id); end
# sig { params(account: T.untyped).returns(T.untyped) }
# def account=(account); end
#
# sig { params(user_id: Integer).void }
# def self.authenticate(user_id); end
#
# sig { returns(T.untyped) }
# def self.helper; end
# sig { returns(T.untyped) }
# def self.helper; end
# end
# end
# ~~~
class ActiveSupportCurrentAttributes < Compiler
Expand All @@ -71,20 +75,25 @@ def decorate
return if dynamic_methods.empty? && instance_methods.empty?

root.create_path(constant) do |current_attributes|
dynamic_methods.each do |method|
method = method.to_s
# We want to generate each method both on the class
generate_method(current_attributes, method, class_method: true)
# and on the instance
generate_method(current_attributes, method, class_method: false)
end
current_attributes_methods_name = "CurrentAttributesMethods"
current_attributes.create_module(current_attributes_methods_name) do |mod|
dynamic_methods.each do |method|
method = method.to_s
# We want to generate each method both on the class
generate_method(current_attributes, method, class_method: true)
# and on the instance
generate_method(mod, method, class_method: false)
end

instance_methods.each do |method|
# instance methods are only elevated to class methods
# no need to add separate instance methods for them
method = constant.instance_method(method)
create_method_from_def(current_attributes, method, class_method: true)
instance_methods.each do |method|
# instance methods are only elevated to class methods
# no need to add separate instance methods for them
method = constant.instance_method(method)
create_method_from_def(current_attributes, method, class_method: true)
end
end

current_attributes.create_include(current_attributes_methods_name)
end
end

Expand Down
28 changes: 16 additions & 12 deletions manual/compiler_activesupportcurrentattributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ this compiler will produce an RBI file with the following content:
# typed: true

class Current
sig { returns(T.untyped) }
def self.account; end
include CurrentAttributesMethods

sig { returns(T.untyped) }
def account; end
module CurrentAttributesMethods
sig { returns(T.untyped) }
def self.account; end

sig { params(account: T.untyped).returns(T.untyped) }
def self.account=(account); end
sig { returns(T.untyped) }
def account; end

sig { params(account: T.untyped).returns(T.untyped) }
def account=(account); end
sig { params(account: T.untyped).returns(T.untyped) }
def self.account=(account); end

sig { params(user_id: Integer).void }
def self.authenticate(user_id); end
sig { params(account: T.untyped).returns(T.untyped) }
def account=(account); end

sig { params(user_id: Integer).void }
def self.authenticate(user_id); end

sig { returns(T.untyped) }
def self.helper; end
sig { returns(T.untyped) }
def self.helper; end
end
end
~~~
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,23 @@ class Current < ActiveSupport::CurrentAttributes
# typed: strong
class Current
sig { returns(T.untyped) }
def account; end
include CurrentAttributesMethods
sig { params(value: T.untyped).returns(T.untyped) }
def account=(value); end
class << self
sig { returns(T.untyped) }
def account; end
sig { returns(T.untyped) }
def user; end
sig { params(value: T.untyped).returns(T.untyped) }
def account=(value); end
sig { params(value: T.untyped).returns(T.untyped) }
def user=(value); end
sig { returns(T.untyped) }
def user; end
class << self
sig { params(value: T.untyped).returns(T.untyped) }
def user=(value); end
end
module CurrentAttributesMethods
sig { returns(T.untyped) }
def account; end
Expand Down Expand Up @@ -104,11 +108,7 @@ def authenticate(user_id, &block)
# typed: strong
class Current
sig { returns(T.untyped) }
def account; end
sig { params(value: T.untyped).returns(T.untyped) }
def account=(value); end
include CurrentAttributesMethods
class << self
sig { returns(T.untyped) }
Expand All @@ -123,6 +123,14 @@ def authenticate(user_id, &block); end
sig { returns(T.untyped) }
def helper; end
end
module CurrentAttributesMethods
sig { returns(T.untyped) }
def account; end
sig { params(value: T.untyped).returns(T.untyped) }
def account=(value); end
end
end
RBI

Expand Down

0 comments on commit 5fb4c91

Please sign in to comment.