Skip to content

Commit 8ec5219

Browse files
committed
ActiveSupport::Delegation allow to specify the signature
When delegating known APIs, rather that to let Delegator try to inspect the signature, or to fallback to `...`, we can directly specify it. This is both faster and make for nicer delegators that have the right signature.
1 parent f760ccd commit 8ec5219

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

activesupport/lib/active_support/current_attributes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def attribute(*names, default: nil)
132132
end
133133
end
134134

135-
Delegation.generate(singleton_class, names.flat_map { |name| [name, "#{name}="] }, to: :instance, as: self, nilable: false)
135+
Delegation.generate(singleton_class, names, to: :instance, nilable: false, signature: "")
136+
Delegation.generate(singleton_class, names.map { |n| "#{n}=" }, to: :instance, nilable: false, signature: "value")
136137

137138
self.defaults = defaults.merge(names.index_with { default })
138139
end

activesupport/lib/active_support/delegation.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Delegation # :nodoc:
2020
RESERVED_METHOD_NAMES = (RUBY_RESERVED_KEYWORDS + %w(_ arg args block)).to_set.freeze
2121

2222
class << self
23-
def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil)
23+
def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil, signature: nil)
2424
unless to
2525
raise ArgumentError, "Delegation needs a target. Supply a keyword argument 'to' (e.g. delegate :hello, to: :greeter)."
2626
end
@@ -65,7 +65,9 @@ def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil
6565
# Attribute writer methods only accept one argument. Makes sure []=
6666
# methods still accept two arguments.
6767
definition = \
68-
if /[^\]]=\z/.match?(method)
68+
if signature
69+
signature
70+
elsif /[^\]]=\z/.match?(method)
6971
"arg"
7072
else
7173
method_object = if receiver_class

0 commit comments

Comments
 (0)