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
This commit is contained in:
Akira Matsuda 2017-01-13 15:09:56 +09:00
parent be5ddc2508
commit 9360b6be63
21 changed files with 33 additions and 39 deletions

View File

@ -73,7 +73,7 @@ module ActionController
require "mutex_m"
class Options < Struct.new(:name, :format, :include, :exclude, :klass, :model) # :nodoc:
Options = Struct.new(:name, :format, :include, :exclude, :klass, :model) do # :nodoc:
include Mutex_m
def self.from_hash(hash)

View File

@ -5,7 +5,7 @@ module ActionDispatch
ESCAPE_PATH = ->(value) { Router::Utils.escape_path(value) }
ESCAPE_SEGMENT = ->(value) { Router::Utils.escape_segment(value) }
class Parameter < Struct.new(:name, :escaper)
Parameter = Struct.new(:name, :escaper) do
def escape(value); escaper.call value; end
end

View File

@ -1,6 +1,6 @@
require "active_model"
class Customer < Struct.new(:name, :id)
Customer = Struct.new(:name, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion
@ -28,7 +28,7 @@ class Customer < Struct.new(:name, :id)
end
end
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
Post = Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost) do
extend ActiveModel::Naming
include ActiveModel::Conversion
extend ActiveModel::Translation

View File

@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
self.view_paths = File.join(FIXTURE_LOAD_PATH, "actionpack")
end
class Customer < Struct.new(:name, :id)
Customer = Struct.new(:name, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion
@ -39,7 +39,7 @@ end
module Quiz
#Models
class Question < Struct.new(:name, :id)
Question = Struct.new(:name, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion

View File

@ -87,7 +87,7 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
end
end
class Game < Struct.new(:name, :id)
Game = Struct.new(:name, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion
def to_param

View File

@ -1,6 +1,6 @@
require "active_model"
class Customer < Struct.new(:name, :id)
Customer = Struct.new(:name, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion
@ -31,7 +31,7 @@ end
class GoodCustomer < Customer
end
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
Post = Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost) do
extend ActiveModel::Naming
include ActiveModel::Conversion
extend ActiveModel::Translation
@ -163,7 +163,7 @@ module Blog
true
end
class Post < Struct.new(:title, :id)
Post = Struct.new(:title, :id) do
extend ActiveModel::Naming
include ActiveModel::Conversion
@ -183,8 +183,7 @@ class ArelLike
end
end
class Car < Struct.new(:color)
end
Car = Struct.new(:color)
class Plane
attr_reader :to_key

View File

@ -4,7 +4,7 @@ class ActiveModelHelperTest < ActionView::TestCase
tests ActionView::Helpers::ActiveModelHelper
silence_warnings do
class Post < Struct.new(:author_name, :body, :updated_at)
Post = Struct.new(:author_name, :body, :updated_at) do
include ActiveModel::Conversion
include ActiveModel::Validations

View File

@ -1,6 +1,6 @@
require "abstract_unit"
class Scroll < Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at)
Scroll = Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at) do
extend ActiveModel::Naming
include ActiveModel::Conversion

View File

@ -1,7 +1,6 @@
require "abstract_unit"
class Category < Struct.new(:id, :name)
end
Category = Struct.new(:id, :name)
class FormCollectionsHelperTest < ActionView::TestCase
def assert_no_select(selector, value = nil)

View File

@ -32,7 +32,7 @@ module ActiveRecord
@alias_cache[node][column]
end
class Table < Struct.new(:node, :columns) # :nodoc:
Table = Struct.new(:node, :columns) do # :nodoc:
def table
Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
end

View File

@ -3,29 +3,25 @@ module ActiveRecord
# Abstract representation of an index definition on a table. Instances of
# this type are typically created and returned by methods in database
# adapters. e.g. ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#indexes
class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using, :comment) #:nodoc:
end
IndexDefinition = Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using, :comment) #:nodoc:
# Abstract representation of a column definition. Instances of this type
# are typically created by methods in TableDefinition, and added to the
# +columns+ attribute of said TableDefinition object, in order to be used
# for generating a number of table creation or table changing SQL statements.
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :auto_increment, :primary_key, :collation, :sql_type, :comment) #:nodoc:
ColumnDefinition = Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :auto_increment, :primary_key, :collation, :sql_type, :comment) do #:nodoc:
def primary_key?
primary_key || type.to_sym == :primary_key
end
end
class AddColumnDefinition < Struct.new(:column) # :nodoc:
end
AddColumnDefinition = Struct.new(:column) # :nodoc:
class ChangeColumnDefinition < Struct.new(:column, :name) #:nodoc:
end
ChangeColumnDefinition = Struct.new(:column, :name) #:nodoc:
class PrimaryKeyDefinition < Struct.new(:name) # :nodoc:
end
PrimaryKeyDefinition = Struct.new(:name) # :nodoc:
class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
ForeignKeyDefinition = Struct.new(:from_table, :to_table, :options) do #:nodoc:
def name
options[:name]
end

View File

@ -692,7 +692,7 @@ module ActiveRecord
connection.respond_to?(:reverting) && connection.reverting
end
class ReversibleBlockHelper < Struct.new(:reverting) # :nodoc:
ReversibleBlockHelper = Struct.new(:reverting) do # :nodoc:
def up
yield unless reverting
end
@ -938,7 +938,7 @@ module ActiveRecord
# MigrationProxy is used to defer loading of the actual migration classes
# until they are needed
class MigrationProxy < Struct.new(:name, :version, :filename, :scope)
MigrationProxy = Struct.new(:name, :version, :filename, :scope) do
def initialize(name, version, filename, scope)
super
@migration = nil

View File

@ -3,7 +3,7 @@ require "cases/helper"
module ActiveRecord
module AttributeMethods
class ReadTest < ActiveRecord::TestCase
class FakeColumn < Struct.new(:name)
FakeColumn = Struct.new(:name) do
def type; :integer; end
end

View File

@ -3,7 +3,7 @@ require "models/post"
module ActiveRecord
class RelationMutationTest < ActiveRecord::TestCase
class FakeKlass < Struct.new(:table_name, :name)
FakeKlass = Struct.new(:table_name, :name) do
extend ActiveRecord::Delegation::DelegateCache
inherited self

View File

@ -8,7 +8,7 @@ module ActiveRecord
class RelationTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors
class FakeKlass < Struct.new(:table_name, :name)
FakeKlass = Struct.new(:table_name, :name) do
extend ActiveRecord::Delegation::DelegateCache
inherited self

View File

@ -19,14 +19,14 @@ module ActiveSupport
set_callback(:complete, *args, &block)
end
class RunHook < Struct.new(:hook) # :nodoc:
RunHook = Struct.new(:hook) do # :nodoc:
def before(target)
hook_state = target.send(:hook_state)
hook_state[hook] = hook.run
end
end
class CompleteHook < Struct.new(:hook) # :nodoc:
CompleteHook = Struct.new(:hook) do # :nodoc:
def before(target)
hook_state = target.send(:hook_state)
if hook_state.key?(hook)

View File

@ -24,7 +24,7 @@ Somewhere = Struct.new(:street, :city) do
attr_accessor :name
end
class Someone < Struct.new(:name, :place)
Someone = Struct.new(:name, :place) do
delegate :street, :city, :to_f, to: :place
delegate :name=, to: :place, prefix: true
delegate :upcase, to: "place.city"

View File

@ -23,7 +23,7 @@ module JSONTest
end
end
class MyStruct < Struct.new(:name, :value)
MyStruct = Struct.new(:name, :value) do
def initialize(*)
@unused = "unused instance variable"
super

View File

@ -209,7 +209,7 @@ module Rails
!options[:skip_active_record] && options[:database] == "sqlite3"
end
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
GemfileEntry = Struct.new(:name, :version, :comment, :options, :commented_out) do
def initialize(name, version, comment, options = {}, commented_out = false)
super
end

View File

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

View File

@ -20,7 +20,7 @@ module Rails
def development?; false; end
end
class Subscriber < Struct.new(:starts, :finishes)
Subscriber = Struct.new(:starts, :finishes) do
def initialize(starts = [], finishes = [])
super
end