-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
attr_internal.rb
49 lines (41 loc) · 1.66 KB
/
attr_internal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true
class Module
# Declares an attribute reader backed by an internally-named instance variable.
def attr_internal_reader(*attrs)
attrs.each { |attr_name| attr_internal_define(attr_name, :reader) }
end
# Declares an attribute writer backed by an internally-named instance variable.
def attr_internal_writer(*attrs)
attrs.each { |attr_name| attr_internal_define(attr_name, :writer) }
end
# Declares an attribute reader and writer backed by an internally-named instance
# variable.
def attr_internal_accessor(*attrs)
attr_internal_reader(*attrs)
attr_internal_writer(*attrs)
end
alias_method :attr_internal, :attr_internal_accessor
class << self
attr_reader :attr_internal_naming_format
def attr_internal_naming_format=(format)
if format.start_with?("@")
ActiveSupport.deprecator.warn <<~MESSAGE
Setting `attr_internal_naming_format` with a `@` prefix is deprecated and will be removed in Rails 7.3.
You can simply replace #{format.inspect} by #{format.delete_prefix("@").inspect}.
MESSAGE
format = format.delete_prefix("@")
end
@attr_internal_naming_format = format
end
end
self.attr_internal_naming_format = "_%s"
private
def attr_internal_define(attr_name, type)
internal_name = Module.attr_internal_naming_format % attr_name
# use native attr_* methods as they are faster on some Ruby implementations
public_send("attr_#{type}", internal_name)
attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
alias_method attr_name, internal_name
remove_method internal_name
end
end