Skip to content

Commit

Permalink
Simplify CurrentAttribute.instance delegation
Browse files Browse the repository at this point in the history
Follow-up to [#50676][]

Instead of relying on code generation, call a corresponding [delegate][]
method on the `.singleton_class`.

[#50676]: #50676
[delegate]: https://edgeapi.rubyonrails.org/classes/Module.html#method-i-delegate

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
  • Loading branch information
seanpdoyle and byroot committed Jan 9, 2024
1 parent ba2603a commit 2cd4abc
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions activesupport/lib/active_support/current_attributes.rb
Expand Up @@ -125,22 +125,7 @@ def attribute(*names)
end
end

ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner|
names.each do |name|
owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch|
batch <<
"def #{name}" <<
"instance.#{name}" <<
"end"
end
owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch|
batch <<
"def #{name}=(value)" <<
"instance.#{name} = value" <<
"end"
end
end
end
singleton_class.delegate(*names.flat_map { |name| [name, "#{name}="] }, to: :instance, as: self)
end

# Calls this callback before #reset is called on the instance. Used for resetting external collaborators that depend on current values.
Expand Down

0 comments on commit 2cd4abc

Please sign in to comment.