提交 d3599d8a 编写于 作者: E Eugene Kenny

Use index_by and index_with wherever possible

Using `index_by` or `index_with` is more concise than `each_with_object`
and more performant than `map { ... }.to_h` or `Hash[map { ... }]`.
上级 6a1759a0
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActionView
module CollectionCaching # :nodoc:
extend ActiveSupport::Concern
......@@ -88,16 +90,15 @@ def expanded_cache_key(key, view, template, digest_path)
# If the partial is not already cached it will also be
# written back to the underlying cache store.
def fetch_or_cache_partial(cached_partials, template, order_by:)
order_by.each_with_object({}) do |cache_key, hash|
hash[cache_key] =
if content = cached_partials[cache_key]
build_rendered_template(content, template)
else
yield.tap do |rendered_partial|
collection_cache.write(cache_key, rendered_partial.body)
end
end
order_by.index_with do |cache_key|
if content = cached_partials[cache_key]
build_rendered_template(content, template)
else
yield.tap do |rendered_partial|
collection_cache.write(cache_key, rendered_partial.body)
end
end
end
end
end
end
# frozen_string_literal: true
require "abstract_unit"
require "active_support/core_ext/enumerable"
class Map < Hash
def category
......@@ -67,9 +68,9 @@ class << base
ActiveSupport::TimeZone.prepend FakeZones
setup do
ActiveSupport::TimeZone.fake_zones = %w(A B C D E).map do |id|
[ id, FakeZones::FakeZone.new(id) ]
end.to_h
ActiveSupport::TimeZone.fake_zones = %w(A B C D E).index_with do |id|
FakeZones::FakeZone.new(id)
end
@fake_timezones = ActiveSupport::TimeZone.all
end
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActiveRecord
module Associations
# Implements the details of eager loading of Active Record associations.
......@@ -171,8 +173,8 @@ def preloaded_records
end
def records_by_owner
@records_by_owner ||= owners.each_with_object({}) do |owner, result|
result[owner] = Array(owner.association(reflection.name).target)
@records_by_owner ||= owners.index_with do |owner|
Array(owner.association(reflection.name).target)
end
end
......
# frozen_string_literal: true
require "mutex_m"
require "active_support/core_ext/enumerable"
module ActiveRecord
# = Active Record Attribute Methods
......@@ -375,8 +376,8 @@ def attribute_method?(attr_name)
end
def attributes_with_values(attribute_names)
attribute_names.each_with_object({}) do |name, attrs|
attrs[name] = _read_attribute(name)
attribute_names.index_with do |name|
_read_attribute(name)
end
end
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
require "active_support/core_ext/hash/indifferent_access"
require "active_support/core_ext/string/filters"
require "active_support/parameter_filter"
......@@ -208,9 +209,7 @@ def find_by(*args) # :nodoc:
keys = hash.keys
statement = cached_find_by_statement(keys) { |params|
wheres = keys.each_with_object({}) { |param, o|
o[param] = params.bind
}
wheres = keys.index_with { params.bind }
where(wheres).limit(1)
}
begin
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActiveRecord
class InsertAll # :nodoc:
attr_reader :model, :connection, :inserts, :keys
......@@ -179,7 +181,7 @@ def extract_types_from_columns_on(table_name, keys:)
unknown_column = (keys - columns.keys).first
raise UnknownAttributeError.new(model.new, unknown_column) if unknown_column
keys.map { |key| [ key, connection.lookup_cast_type_from_column(columns[key]) ] }.to_h
keys.index_with { |key| connection.lookup_cast_type_from_column(columns[key]) }
end
def format_columns(columns)
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActiveRecord
module Calculations
# Count the records.
......@@ -354,7 +356,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
if association
key_ids = calculated_data.collect { |row| row[group_aliases.first] }
key_records = association.klass.base_class.where(association.klass.base_class.primary_key => key_ids)
key_records = Hash[key_records.map { |r| [r.id, r] }]
key_records = key_records.index_by(&:id)
end
Hash[calculated_data.map do |row|
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActiveRecord
module TestFixtures
extend ActiveSupport::Concern
......@@ -206,8 +208,7 @@ def setup_shared_connection_pool
end
def load_fixtures(config)
fixtures = ActiveRecord::FixtureSet.create_fixtures(fixture_path, fixture_table_names, fixture_class_names, config)
Hash[fixtures.map { |f| [f.name, f] }]
ActiveRecord::FixtureSet.create_fixtures(fixture_path, fixture_table_names, fixture_class_names, config).index_by(&:name)
end
def instantiate_fixtures
......
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
require "date"
module FakeRecord
class Column < Struct.new(:name, :type)
......@@ -24,8 +26,8 @@ def initialize(visitor = nil)
]
}
@columns_hash = {
"users" => Hash[@columns["users"].map { |x| [x.name, x] }],
"products" => Hash[@columns["products"].map { |x| [x.name, x] }]
"users" => @columns["users"].index_by(&:name),
"products" => @columns["products"].index_by(&:name)
}
@primary_keys = {
"users" => "id",
......
# frozen_string_literal: true
require "cases/helper"
require "active_support/core_ext/enumerable"
module ActiveRecord
module AttributeMethods
......@@ -30,9 +31,9 @@ def self.columns
end
def self.columns_hash
Hash[attribute_names.map { |name|
[name, FakeColumn.new(name)]
}]
attribute_names.index_with { |name|
FakeColumn.new(name)
}
end
end
end
......
......@@ -27,6 +27,7 @@
require "models/car"
require "models/bulb"
require "concurrent/atomic/count_down_latch"
require "active_support/core_ext/enumerable"
class FirstAbstractClass < ActiveRecord::Base
self.abstract_class = true
......@@ -732,9 +733,9 @@ def test_attributes_on_dummy_time_with_invalid_time
def test_attributes
category = Category.new(name: "Ruby")
expected_attributes = category.attribute_names.map do |attribute_name|
[attribute_name, category.public_send(attribute_name)]
end.to_h
expected_attributes = category.attribute_names.index_with do |attribute_name|
category.public_send(attribute_name)
end
assert_instance_of Hash, category.attributes
assert_equal expected_attributes, category.attributes
......
......@@ -16,6 +16,7 @@
require "models/subscriber"
require "models/subscription"
require "models/book"
require "active_support/core_ext/enumerable"
class CounterCacheTest < ActiveRecord::TestCase
fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers, :people, :friendships, :subscribers, :subscriptions, :books
......@@ -355,8 +356,8 @@ class ::SpecialReply < ::Reply
private
def assert_touching(record, *attributes)
record.update_columns attributes.map { |attr| [ attr, 5.minutes.ago ] }.to_h
touch_times = attributes.map { |attr| [ attr, record.public_send(attr) ] }.to_h
record.update_columns attributes.index_with(5.minutes.ago)
touch_times = attributes.index_with { |attr| record.public_send(attr) }
yield
......
......@@ -3,6 +3,7 @@
require "zlib"
require "active_support/core_ext/array/extract_options"
require "active_support/core_ext/array/wrap"
require "active_support/core_ext/enumerable"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/numeric/bytes"
require "active_support/core_ext/numeric/time"
......@@ -440,8 +441,8 @@ def fetch_multi(*names)
instrument :read_multi, names, options do |payload|
reads = read_multi_entries(names, **options)
writes = {}
ordered = names.each_with_object({}) do |name, hash|
hash[name] = reads.fetch(name) { writes[name] = yield(name) }
ordered = names.index_with do |name|
reads.fetch(name) { writes[name] = yield(name) }
end
payload[:hits] = reads.keys
......
......@@ -7,6 +7,7 @@
raise e
end
require "active_support/core_ext/enumerable"
require "active_support/core_ext/marshal"
require "active_support/core_ext/array/extract_options"
......@@ -162,7 +163,7 @@ def write_entry(key, entry, **options)
# Reads multiple entries from the cache implementation.
def read_multi_entries(names, **options)
keys_to_names = Hash[names.map { |name| [normalize_key(name, options), name] }]
keys_to_names = names.index_by { |name| normalize_key(name, options) }
raw_values = @data.with { |c| c.get_multi(keys_to_names.keys) }
values = {}
......
# frozen_string_literal: true
require "active_support/callbacks"
require "active_support/core_ext/enumerable"
module ActiveSupport
# Abstract super class that provides a thread-isolated attributes singleton, which resets automatically
......@@ -201,7 +202,7 @@ def assign_attributes(new_attributes)
end
def compute_attributes(keys)
keys.collect { |key| [ key, public_send(key) ] }.to_h
keys.index_with { |key| public_send(key) }
end
end
end
# frozen_string_literal: true
require "active_support/core_ext/enumerable"
module ActiveSupport
module Testing
module Assertions
......@@ -89,7 +91,7 @@ def assert_difference(expression, *args, &block)
else
difference = args[0] || 1
message = args[1]
Hash[Array(expression).map { |e| [e, difference] }]
Array(expression).index_with(difference)
end
exps = expressions.keys.map { |e|
......
......@@ -8,6 +8,7 @@
require "active_support/core_ext/kernel/singleton_class"
require "active_support/core_ext/array/extract_options"
require "active_support/core_ext/enumerable"
require "active_support/core_ext/hash/deep_merge"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/string/indent"
......@@ -252,7 +253,7 @@ def find_by_namespace(name, base = nil, context = nil) #:nodoc:
lookup(lookups)
namespaces = Hash[subclasses.map { |klass| [klass.namespace, klass] }]
namespaces = subclasses.index_by(&:namespace)
lookups.each do |namespace|
klass = namespaces[namespace]
return klass if klass
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册