mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
7834363bbf
Redundant `to_s` has a few overhead. Especially private methods are not intend to be passed user input directly so it should be passed always string. Removing redundant `to_s` makes attribute methods about 10% faster. ```ruby ActiveRecord::Schema.define do create_table :users, force: true do |t| end end class User < ActiveRecord::Base def fast_read_attribute(attr_name, &block) @attributes.fetch_value(attr_name, &block) end end user = User.create! Benchmark.ips do |x| x.report("user._read_attribute('id')") { user._read_attribute("id") } x.report("user.fast_read_attribute('id')") { user.fast_read_attribute("id") } end ``` ``` Warming up -------------------------------------- user._read_attribute('id') 272.151k i/100ms user.fast_read_attribute('id') 283.518k i/100ms Calculating ------------------------------------- user._read_attribute('id') 2.699M (± 1.3%) i/s - 13.608M in 5.042846s user.fast_read_attribute('id') 2.988M (± 1.2%) i/s - 15.026M in 5.029056s ```
148 lines
4 KiB
Ruby
148 lines
4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "active_model/attribute_set"
|
|
require "active_model/attribute/user_provided_default"
|
|
|
|
module ActiveModel
|
|
module Attributes #:nodoc:
|
|
extend ActiveSupport::Concern
|
|
include ActiveModel::AttributeMethods
|
|
|
|
included do
|
|
attribute_method_suffix "="
|
|
class_attribute :attribute_types, :_default_attributes, instance_accessor: false
|
|
self.attribute_types = Hash.new(Type.default_value)
|
|
self._default_attributes = AttributeSet.new({})
|
|
end
|
|
|
|
module ClassMethods
|
|
def attribute(name, type = Type::Value.new, **options)
|
|
name = name.to_s
|
|
if type.is_a?(Symbol)
|
|
type = ActiveModel::Type.lookup(type, **options.except(:default))
|
|
end
|
|
self.attribute_types = attribute_types.merge(name => type)
|
|
define_default_attribute(name, options.fetch(:default, NO_DEFAULT_PROVIDED), type)
|
|
define_attribute_method(name)
|
|
end
|
|
|
|
# Returns an array of attribute names as strings
|
|
#
|
|
# class Person
|
|
# include ActiveModel::Attributes
|
|
#
|
|
# attribute :name, :string
|
|
# attribute :age, :integer
|
|
# end
|
|
#
|
|
# Person.attribute_names
|
|
# # => ["name", "age"]
|
|
def attribute_names
|
|
attribute_types.keys
|
|
end
|
|
|
|
private
|
|
def define_method_attribute=(name, owner:)
|
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
|
owner, name, writer: true,
|
|
) do |temp_method_name, attr_name_expr|
|
|
owner <<
|
|
"def #{temp_method_name}(value)" <<
|
|
" write_attribute(#{attr_name_expr}, value)" <<
|
|
"end"
|
|
end
|
|
end
|
|
|
|
NO_DEFAULT_PROVIDED = Object.new # :nodoc:
|
|
private_constant :NO_DEFAULT_PROVIDED
|
|
|
|
def define_default_attribute(name, value, type)
|
|
self._default_attributes = _default_attributes.deep_dup
|
|
if value == NO_DEFAULT_PROVIDED
|
|
default_attribute = _default_attributes[name].with_type(type)
|
|
else
|
|
default_attribute = Attribute::UserProvidedDefault.new(
|
|
name,
|
|
value,
|
|
type,
|
|
_default_attributes.fetch(name.to_s) { nil },
|
|
)
|
|
end
|
|
_default_attributes[name] = default_attribute
|
|
end
|
|
end
|
|
|
|
def initialize(*)
|
|
@attributes = self.class._default_attributes.deep_dup
|
|
super
|
|
end
|
|
|
|
def initialize_dup(other) # :nodoc:
|
|
@attributes = @attributes.deep_dup
|
|
super
|
|
end
|
|
|
|
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
|
|
#
|
|
# class Person
|
|
# include ActiveModel::Attributes
|
|
#
|
|
# attribute :name, :string
|
|
# attribute :age, :integer
|
|
# end
|
|
#
|
|
# person = Person.new(name: 'Francesco', age: 22)
|
|
# person.attributes
|
|
# # => {"name"=>"Francesco", "age"=>22}
|
|
def attributes
|
|
@attributes.to_hash
|
|
end
|
|
|
|
# Returns an array of attribute names as strings
|
|
#
|
|
# class Person
|
|
# include ActiveModel::Attributes
|
|
#
|
|
# attribute :name, :string
|
|
# attribute :age, :integer
|
|
# end
|
|
#
|
|
# person = Person.new
|
|
# person.attribute_names
|
|
# # => ["name", "age"]
|
|
def attribute_names
|
|
@attributes.keys
|
|
end
|
|
|
|
def freeze
|
|
@attributes = @attributes.clone.freeze
|
|
super
|
|
end
|
|
|
|
private
|
|
def write_attribute(attr_name, value)
|
|
name = attr_name.to_s
|
|
name = self.class.attribute_aliases[name] || name
|
|
|
|
_write_attribute(name, value)
|
|
end
|
|
|
|
def _write_attribute(attr_name, value)
|
|
@attributes.write_from_user(attr_name, value)
|
|
value
|
|
end
|
|
alias :attribute= :_write_attribute
|
|
|
|
def read_attribute(attr_name)
|
|
name = attr_name.to_s
|
|
name = self.class.attribute_aliases[name] || name
|
|
|
|
_read_attribute(name)
|
|
end
|
|
|
|
def _read_attribute(attr_name)
|
|
@attributes.fetch_value(attr_name)
|
|
end
|
|
alias :attribute :_read_attribute
|
|
end
|
|
end
|