File tree 3 files changed +21
-4
lines changed
3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
### [ 0-9-stable] ( https://github.com/rails-api/active_model_serializers/compare/v0.9.12...0-9-stable )
4
4
5
+ - Perf
6
+ - [ #2471 ] ( https://github.com/rails-api/active_model_serializers/pull/2471 ) Generate better attribute accessors, that also don't trigger warnings when redefined. (@byroot )
7
+
5
8
### [ v0.9.12 (2024-04-11)] ( https://github.com/rails-api/active_model_serializers/compare/v0.9.11...v0.9.12 )
6
9
7
10
- Fix
Original file line number Diff line number Diff line change @@ -72,8 +72,10 @@ group :test do
72
72
platforms ( *( @windows_platforms + [ :ruby ] ) ) do
73
73
if RUBY_VERSION < '2.5' || version < '6'
74
74
gem 'sqlite3' , '~> 1.3.13'
75
+ elsif version >= '8'
76
+ gem 'sqlite3' , '>= 2'
75
77
else
76
- gem 'sqlite3'
78
+ gem 'sqlite3' , '< 2'
77
79
end
78
80
end
79
81
platforms :jruby do
Original file line number Diff line number Diff line change @@ -90,15 +90,27 @@ def root_name
90
90
end
91
91
92
92
def attributes ( *attrs )
93
+ # Use `class_eval` rather than `define_method` for faster access
94
+ # and avoid retaining objects in the closure.
95
+ # Batch all methods in a single `class_eval` for efficiceny,
96
+ # and define all methods on the same line so the eventual backtrace
97
+ # properly maps to the `attributes` call.
98
+ source = [ "# frozen_string_literal: true\n " ]
93
99
attrs . each do |attr |
94
100
striped_attr = strip_attribute attr
95
101
96
102
@_attributes << striped_attr
97
103
98
- define_method striped_attr do
99
- object . read_attribute_for_serialization attr
100
- end unless method_defined? ( attr )
104
+ unless method_defined? ( attr )
105
+ source <<
106
+ "def #{ striped_attr } " <<
107
+ "object.read_attribute_for_serialization(#{ attr . inspect } )" <<
108
+ "end" <<
109
+ "alias_method :#{ striped_attr } , :#{ striped_attr } " # suppress method redefinition warning
110
+ end
101
111
end
112
+ caller = caller_locations ( 1 , 1 ) . first
113
+ class_eval ( source . join ( ";" ) , caller . path , caller . lineno - 1 )
102
114
end
103
115
104
116
def has_one ( *attrs )
You can’t perform that action at this time.
0 commit comments