Skip to content

Commit

Permalink
Merge pull request #1726 from Shopify/andyw8/current-attributes-synth…
Browse files Browse the repository at this point in the history
…etic-include

Use an include for the CurrentAttributes generator
  • Loading branch information
andyw8 authored Dec 5, 2023
2 parents dc1f405 + 6467c61 commit bea6da1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
59 changes: 35 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,29 @@ module Compilers
# # typed: true
#
# class Current
# sig { returns(T.untyped) }
# def self.account; end
# include GeneratedAttributeMethods
#
# sig { returns(T.untyped) }
# def account; end
# class << self
# sig { returns(T.untyped) }
# def account; end
#
# sig { params(account: T.untyped).returns(T.untyped) }
# def self.account=(account); end
# sig { params(account: T.untyped).returns(T.untyped) }
# def account=(account); end
#
# sig { params(account: T.untyped).returns(T.untyped) }
# def account=(account); end
# sig { params(user_id: Integer).void }
# def authenticate(user_id); end
#
# sig { params(user_id: Integer).void }
# def self.authenticate(user_id); end
# sig { returns(T.untyped) }
# def helper; end
# end
#
# module GeneratedAttributeMethods
# sig { returns(T.untyped) }
# def account; end
#
# sig { returns(T.untyped) }
# def self.helper; end
# sig { params(account: T.untyped).returns(T.untyped) }
# def account=(account); end
# end
# end
# ~~~
class ActiveSupportCurrentAttributes < Compiler
Expand All @@ -71,20 +77,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 = "GeneratedAttributeMethods"
current_attributes.create_module(current_attributes_methods_name) do |generated_attribute_methods|
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(generated_attribute_methods, 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
30 changes: 18 additions & 12 deletions manual/compiler_activesupportcurrentattributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ this compiler will produce an RBI file with the following content:
# typed: true

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

sig { returns(T.untyped) }
def account; end
class << self
sig { returns(T.untyped) }
def account; end

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

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

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

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

sig { params(account: T.untyped).returns(T.untyped) }
def account=(account); 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 GeneratedAttributeMethods
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 GeneratedAttributeMethods
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 GeneratedAttributeMethods
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 GeneratedAttributeMethods
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 bea6da1

Please sign in to comment.