From 402852fc6d3222a283b65d407ffec3161946db71 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 26 Aug 2016 02:07:44 +0900 Subject: [PATCH] Add `Type.default_value` and use it everywhere for internal For reduce instantiating `Type::Value`. --- activerecord/lib/active_record/attribute.rb | 2 +- .../active_record/connection_adapters/postgresql_adapter.rb | 2 +- activerecord/lib/active_record/model_schema.rb | 2 +- activerecord/lib/active_record/relation/calculations.rb | 2 +- activerecord/lib/active_record/relation/query_methods.rb | 4 ++-- activerecord/lib/active_record/result.rb | 4 +--- activerecord/lib/active_record/table_metadata.rb | 2 +- activerecord/lib/active_record/type.rb | 4 ++++ activerecord/lib/active_record/type/type_map.rb | 6 +----- 9 files changed, 13 insertions(+), 15 deletions(-) diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 380593e809..0b08c2a39b 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -187,7 +187,7 @@ module ActiveRecord class Null < Attribute # :nodoc: def initialize(name) - super(name, nil, Type::Value.new) + super(name, nil, Type.default_value) end def type_cast(*) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index bd53123511..2013f24d74 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -437,7 +437,7 @@ module ActiveRecord type_map.fetch(oid, fmod, sql_type) { warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String." - Type::Value.new.tap do |cast_type| + Type.default_value.tap do |cast_type| type_map.register_type(oid, cast_type) end } diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 5718e7fdd0..4ccc5fbb21 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -268,7 +268,7 @@ module ActiveRecord def attribute_types # :nodoc: load_schema - @attribute_types ||= Hash.new(Type::Value.new) + @attribute_types ||= Hash.new(Type.default_value) end def yaml_encoder # :nodoc: diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index b569abc7a8..a796e35261 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -312,7 +312,7 @@ module ActiveRecord key = group_columns.map { |aliaz, col_name| column = type_for(col_name) do calculated_data.column_types.fetch(aliaz) do - Type::Value.new + Type.default_value end end type_cast_calculated_value(row[aliaz], column) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index bd41653df0..5a31f61d6d 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -80,14 +80,14 @@ module ActiveRecord limit_bind = Attribute.with_cast_value( "LIMIT".freeze, connection.sanitize_limit(limit_value), - Type::Value.new, + Type.default_value, ) end if offset_value offset_bind = Attribute.with_cast_value( "OFFSET".freeze, offset_value.to_i, - Type::Value.new, + Type.default_value, ) end connection.combine_bind_parameters( diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index f5383f4c14..9ed70a9c2b 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -32,8 +32,6 @@ module ActiveRecord class Result include Enumerable - IDENTITY_TYPE = Type::Value.new # :nodoc: - attr_reader :columns, :rows, :column_types def initialize(columns, rows, column_types = {}) @@ -105,7 +103,7 @@ module ActiveRecord def column_type(name, type_overrides = {}) type_overrides.fetch(name) do - column_types.fetch(name, IDENTITY_TYPE) + column_types.fetch(name, Type.default_value) end end diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 0ca880e635..a398e0cb7d 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -33,7 +33,7 @@ module ActiveRecord if klass klass.type_for_attribute(column_name.to_s) else - Type::Value.new + Type.default_value end end diff --git a/activerecord/lib/active_record/type.rb b/activerecord/lib/active_record/type.rb index 1b2fc1b034..0b48d2186a 100644 --- a/activerecord/lib/active_record/type.rb +++ b/activerecord/lib/active_record/type.rb @@ -37,6 +37,10 @@ module ActiveRecord registry.lookup(*args, adapter: adapter, **kwargs) end + def default_value # :nodoc: + @default_value ||= Value.new + end + private def current_adapter_name diff --git a/activerecord/lib/active_record/type/type_map.rb b/activerecord/lib/active_record/type/type_map.rb index 9618ff8787..7bce82a1ff 100644 --- a/activerecord/lib/active_record/type/type_map.rb +++ b/activerecord/lib/active_record/type/type_map.rb @@ -11,7 +11,7 @@ module ActiveRecord end def lookup(lookup_key, *args) - fetch(lookup_key, *args) { default_value } + fetch(lookup_key, *args) { Type.default_value } end def fetch(lookup_key, *args, &block) @@ -55,10 +55,6 @@ module ActiveRecord yield lookup_key, *args end end - - def default_value - @default_value ||= ActiveModel::Type::Value.new - end end end end