Skip to content

Commit 9360b6b

Browse files
committed
class Foo < Struct.new(:x) creates an extra unneeded anonymous class
because Struct.new returns a Class, we just can give it a name and use it directly without inheriting from it
1 parent be5ddc2 commit 9360b6b

File tree

21 files changed

+33
-39
lines changed

21 files changed

+33
-39
lines changed

actionpack/lib/action_controller/metal/params_wrapper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module ParamsWrapper
7373

7474
require "mutex_m"
7575

76-
class Options < Struct.new(:name, :format, :include, :exclude, :klass, :model) # :nodoc:
76+
Options = Struct.new(:name, :format, :include, :exclude, :klass, :model) do # :nodoc:
7777
include Mutex_m
7878

7979
def self.from_hash(hash)

actionpack/lib/action_dispatch/journey/visitors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Format
55
ESCAPE_PATH = ->(value) { Router::Utils.escape_path(value) }
66
ESCAPE_SEGMENT = ->(value) { Router::Utils.escape_segment(value) }
77

8-
class Parameter < Struct.new(:name, :escaper)
8+
Parameter = Struct.new(:name, :escaper) do
99
def escape(value); escaper.call value; end
1010
end
1111

actionpack/test/lib/controller/fake_models.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "active_model"
22

3-
class Customer < Struct.new(:name, :id)
3+
Customer = Struct.new(:name, :id) do
44
extend ActiveModel::Naming
55
include ActiveModel::Conversion
66

@@ -28,7 +28,7 @@ def persisted?
2828
end
2929
end
3030

31-
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
31+
Post = Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost) do
3232
extend ActiveModel::Naming
3333
include ActiveModel::Conversion
3434
extend ActiveModel::Translation

actionview/test/actionpack/controller/render_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
55
self.view_paths = File.join(FIXTURE_LOAD_PATH, "actionpack")
66
end
77

8-
class Customer < Struct.new(:name, :id)
8+
Customer = Struct.new(:name, :id) do
99
extend ActiveModel::Naming
1010
include ActiveModel::Conversion
1111

@@ -39,7 +39,7 @@ def cache_key
3939

4040
module Quiz
4141
#Models
42-
class Question < Struct.new(:name, :id)
42+
Question = Struct.new(:name, :id) do
4343
extend ActiveModel::Naming
4444
include ActiveModel::Conversion
4545

actionview/test/activerecord/render_partial_with_record_identification_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_rendering_partial_with_has_one_association
8787
end
8888
end
8989

90-
class Game < Struct.new(:name, :id)
90+
Game = Struct.new(:name, :id) do
9191
extend ActiveModel::Naming
9292
include ActiveModel::Conversion
9393
def to_param

actionview/test/lib/controller/fake_models.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "active_model"
22

3-
class Customer < Struct.new(:name, :id)
3+
Customer = Struct.new(:name, :id) do
44
extend ActiveModel::Naming
55
include ActiveModel::Conversion
66

@@ -31,7 +31,7 @@ def persisted?
3131
class GoodCustomer < Customer
3232
end
3333

34-
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
34+
Post = Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost) do
3535
extend ActiveModel::Naming
3636
include ActiveModel::Conversion
3737
extend ActiveModel::Translation
@@ -163,7 +163,7 @@ def self.use_relative_model_naming?
163163
true
164164
end
165165

166-
class Post < Struct.new(:title, :id)
166+
Post = Struct.new(:title, :id) do
167167
extend ActiveModel::Naming
168168
include ActiveModel::Conversion
169169

@@ -183,8 +183,7 @@ def each
183183
end
184184
end
185185

186-
class Car < Struct.new(:color)
187-
end
186+
Car = Struct.new(:color)
188187

189188
class Plane
190189
attr_reader :to_key

actionview/test/template/active_model_helper_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ActiveModelHelperTest < ActionView::TestCase
44
tests ActionView::Helpers::ActiveModelHelper
55

66
silence_warnings do
7-
class Post < Struct.new(:author_name, :body, :updated_at)
7+
Post = Struct.new(:author_name, :body, :updated_at) do
88
include ActiveModel::Conversion
99
include ActiveModel::Validations
1010

actionview/test/template/atom_feed_helper_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "abstract_unit"
22

3-
class Scroll < Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at)
3+
Scroll = Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at) do
44
extend ActiveModel::Naming
55
include ActiveModel::Conversion
66

actionview/test/template/form_collections_helper_test.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "abstract_unit"
22

3-
class Category < Struct.new(:id, :name)
4-
end
3+
Category = Struct.new(:id, :name)
54

65
class FormCollectionsHelperTest < ActionView::TestCase
76
def assert_no_select(selector, value = nil)

activerecord/lib/active_record/associations/join_dependency.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def column_alias(node, column)
3232
@alias_cache[node][column]
3333
end
3434

35-
class Table < Struct.new(:node, :columns) # :nodoc:
35+
Table = Struct.new(:node, :columns) do # :nodoc:
3636
def table
3737
Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
3838
end

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,25 @@ module ConnectionAdapters #:nodoc:
33
# Abstract representation of an index definition on a table. Instances of
44
# this type are typically created and returned by methods in database
55
# adapters. e.g. ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#indexes
6-
class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using, :comment) #:nodoc:
7-
end
6+
IndexDefinition = Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using, :comment) #:nodoc:
87

98
# Abstract representation of a column definition. Instances of this type
109
# are typically created by methods in TableDefinition, and added to the
1110
# +columns+ attribute of said TableDefinition object, in order to be used
1211
# for generating a number of table creation or table changing SQL statements.
13-
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :auto_increment, :primary_key, :collation, :sql_type, :comment) #:nodoc:
12+
ColumnDefinition = Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :auto_increment, :primary_key, :collation, :sql_type, :comment) do #:nodoc:
1413
def primary_key?
1514
primary_key || type.to_sym == :primary_key
1615
end
1716
end
1817

19-
class AddColumnDefinition < Struct.new(:column) # :nodoc:
20-
end
18+
AddColumnDefinition = Struct.new(:column) # :nodoc:
2119

22-
class ChangeColumnDefinition < Struct.new(:column, :name) #:nodoc:
23-
end
20+
ChangeColumnDefinition = Struct.new(:column, :name) #:nodoc:
2421

25-
class PrimaryKeyDefinition < Struct.new(:name) # :nodoc:
26-
end
22+
PrimaryKeyDefinition = Struct.new(:name) # :nodoc:
2723

28-
class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
24+
ForeignKeyDefinition = Struct.new(:from_table, :to_table, :options) do #:nodoc:
2925
def name
3026
options[:name]
3127
end

activerecord/lib/active_record/migration.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def reverting?
692692
connection.respond_to?(:reverting) && connection.reverting
693693
end
694694

695-
class ReversibleBlockHelper < Struct.new(:reverting) # :nodoc:
695+
ReversibleBlockHelper = Struct.new(:reverting) do # :nodoc:
696696
def up
697697
yield unless reverting
698698
end
@@ -938,7 +938,7 @@ def execute_block
938938

939939
# MigrationProxy is used to defer loading of the actual migration classes
940940
# until they are needed
941-
class MigrationProxy < Struct.new(:name, :version, :filename, :scope)
941+
MigrationProxy = Struct.new(:name, :version, :filename, :scope) do
942942
def initialize(name, version, filename, scope)
943943
super
944944
@migration = nil

activerecord/test/cases/attribute_methods/read_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module ActiveRecord
44
module AttributeMethods
55
class ReadTest < ActiveRecord::TestCase
6-
class FakeColumn < Struct.new(:name)
6+
FakeColumn = Struct.new(:name) do
77
def type; :integer; end
88
end
99

activerecord/test/cases/relation/mutation_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module ActiveRecord
55
class RelationMutationTest < ActiveRecord::TestCase
6-
class FakeKlass < Struct.new(:table_name, :name)
6+
FakeKlass = Struct.new(:table_name, :name) do
77
extend ActiveRecord::Delegation::DelegateCache
88
inherited self
99

activerecord/test/cases/relation_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module ActiveRecord
88
class RelationTest < ActiveRecord::TestCase
99
fixtures :posts, :comments, :authors
1010

11-
class FakeKlass < Struct.new(:table_name, :name)
11+
FakeKlass = Struct.new(:table_name, :name) do
1212
extend ActiveRecord::Delegation::DelegateCache
1313

1414
inherited self

activesupport/lib/active_support/execution_wrapper.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def self.to_complete(*args, &block)
1919
set_callback(:complete, *args, &block)
2020
end
2121

22-
class RunHook < Struct.new(:hook) # :nodoc:
22+
RunHook = Struct.new(:hook) do # :nodoc:
2323
def before(target)
2424
hook_state = target.send(:hook_state)
2525
hook_state[hook] = hook.run
2626
end
2727
end
2828

29-
class CompleteHook < Struct.new(:hook) # :nodoc:
29+
CompleteHook = Struct.new(:hook) do # :nodoc:
3030
def before(target)
3131
hook_state = target.send(:hook_state)
3232
if hook_state.key?(hook)

activesupport/test/core_ext/module_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Cd
2424
attr_accessor :name
2525
end
2626

27-
class Someone < Struct.new(:name, :place)
27+
Someone = Struct.new(:name, :place) do
2828
delegate :street, :city, :to_f, to: :place
2929
delegate :name=, to: :place, prefix: true
3030
delegate :upcase, to: "place.city"

activesupport/test/json/encoding_test_cases.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def as_json(options = nil)
2323
end
2424
end
2525

26-
class MyStruct < Struct.new(:name, :value)
26+
MyStruct = Struct.new(:name, :value) do
2727
def initialize(*)
2828
@unused = "unused instance variable"
2929
super

railties/lib/rails/generators/app_base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def sqlite3? # :doc:
209209
!options[:skip_active_record] && options[:database] == "sqlite3"
210210
end
211211

212-
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
212+
GemfileEntry = Struct.new(:name, :version, :comment, :options, :commented_out) do
213213
def initialize(name, version, comment, options = {}, commented_out = false)
214214
super
215215
end

railties/lib/rails/source_annotation_extractor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# start with the tag optionally followed by a colon. Everything up to the end
1414
# of the line (or closing ERB comment tag) is considered to be their text.
1515
class SourceAnnotationExtractor
16-
class Annotation < Struct.new(:line, :tag, :text)
16+
Annotation = Struct.new(:line, :tag, :text) do
1717
def self.directories
1818
@@directories ||= %w(app config db lib test) + (ENV["SOURCE_ANNOTATION_DIRECTORIES"] || "").split(",")
1919
end

railties/test/rack_logger_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(logger = NULL, taggers = nil, &block)
2020
def development?; false; end
2121
end
2222

23-
class Subscriber < Struct.new(:starts, :finishes)
23+
Subscriber = Struct.new(:starts, :finishes) do
2424
def initialize(starts = [], finishes = [])
2525
super
2626
end

0 commit comments

Comments
 (0)