Skip to content

Commit

Permalink
Add Type.default_value and use it everywhere for internal
Browse files Browse the repository at this point in the history
For reduce instantiating `Type::Value`.
  • Loading branch information
kamipo committed Aug 25, 2016
1 parent 804f5b3 commit 402852f
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute.rb
Expand Up @@ -187,7 +187,7 @@ def changed_in_place?

class Null < Attribute # :nodoc:
def initialize(name)
super(name, nil, Type::Value.new)
super(name, nil, Type.default_value)
end

def type_cast(*)
Expand Down
Expand Up @@ -437,7 +437,7 @@ def get_oid_type(oid, fmod, column_name, sql_type = "") # :nodoc:

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
}
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/model_schema.rb
Expand Up @@ -268,7 +268,7 @@ def columns

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:
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -312,7 +312,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
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)
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -80,14 +80,14 @@ def bound_attributes
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(
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/result.rb
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -105,7 +103,7 @@ def initialize_copy(other)

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

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/table_metadata.rb
Expand Up @@ -33,7 +33,7 @@ def type(column_name)
if klass
klass.type_for_attribute(column_name.to_s)
else
Type::Value.new
Type.default_value
end
end

Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/type.rb
Expand Up @@ -37,6 +37,10 @@ def lookup(*args, adapter: current_adapter_name, **kwargs) # :nodoc:
registry.lookup(*args, adapter: adapter, **kwargs)
end

def default_value # :nodoc:
@default_value ||= Value.new
end

private

def current_adapter_name
Expand Down
6 changes: 1 addition & 5 deletions activerecord/lib/active_record/type/type_map.rb
Expand Up @@ -11,7 +11,7 @@ def initialize
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)
Expand Down Expand Up @@ -55,10 +55,6 @@ def perform_fetch(lookup_key, *args)
yield lookup_key, *args
end
end

def default_value
@default_value ||= ActiveModel::Type::Value.new
end
end
end
end

0 comments on commit 402852f

Please sign in to comment.