Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47463 from Shopify/configurable-default-column-se…
…rializer Allow to define the default column serializer
- Loading branch information
Showing
18 changed files
with
346 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
activerecord/lib/active_record/coders/column_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module Coders # :nodoc: | ||
class ColumnSerializer # :nodoc: | ||
attr_reader :object_class | ||
attr_reader :coder | ||
|
||
def initialize(attr_name, coder, object_class = Object) | ||
@attr_name = attr_name | ||
@object_class = object_class | ||
@coder = coder | ||
check_arity_of_constructor | ||
end | ||
|
||
def init_with(coder) # :nodoc: | ||
@attr_name = coder["attr_name"] | ||
@object_class = coder["object_class"] | ||
@coder = coder["coder"] | ||
end | ||
|
||
def dump(object) | ||
return if object.nil? | ||
|
||
assert_valid_value(object, action: "dump") | ||
coder.dump(object) | ||
end | ||
|
||
def load(payload) | ||
if payload.nil? | ||
if @object_class != ::Object | ||
return @object_class.new | ||
end | ||
return nil | ||
end | ||
|
||
object = coder.load(payload) | ||
|
||
assert_valid_value(object, action: "load") | ||
object ||= object_class.new if object_class != Object | ||
|
||
object | ||
end | ||
|
||
# Public because it's called by Type::Serialized | ||
def assert_valid_value(object, action:) | ||
unless object.nil? || object_class === object | ||
raise SerializationTypeMismatch, | ||
"can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{object.class}. -- #{object.inspect}" | ||
end | ||
end | ||
|
||
private | ||
def check_arity_of_constructor | ||
load(nil) | ||
rescue ArgumentError | ||
raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor." | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.