2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-19 12:45:07 -04:00
|
|
|
module ActiveModel
|
2016-05-31 14:44:38 -04:00
|
|
|
class AttributeSet
|
|
|
|
# Attempts to do more intelligent YAML dumping of an
|
2017-10-19 12:45:07 -04:00
|
|
|
# ActiveModel::AttributeSet to reduce the size of the resulting string
|
2016-06-24 13:15:55 -04:00
|
|
|
class YAMLEncoder # :nodoc:
|
2016-05-31 14:44:38 -04:00
|
|
|
def initialize(default_types)
|
|
|
|
@default_types = default_types
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode(attribute_set, coder)
|
2016-08-06 12:24:04 -04:00
|
|
|
coder["concise_attributes"] = attribute_set.each_value.map do |attr|
|
2016-05-31 14:44:38 -04:00
|
|
|
if attr.type.equal?(default_types[attr.name])
|
|
|
|
attr.with_type(nil)
|
|
|
|
else
|
|
|
|
attr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def decode(coder)
|
2016-08-06 12:24:04 -04:00
|
|
|
if coder["attributes"]
|
|
|
|
coder["attributes"]
|
2016-05-31 14:44:38 -04:00
|
|
|
else
|
2016-08-06 12:24:04 -04:00
|
|
|
attributes_hash = Hash[coder["concise_attributes"].map do |attr|
|
2016-05-31 14:44:38 -04:00
|
|
|
if attr.type.nil?
|
|
|
|
attr = attr.with_type(default_types[attr.name])
|
|
|
|
end
|
|
|
|
[attr.name, attr]
|
|
|
|
end]
|
|
|
|
AttributeSet.new(attributes_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-25 16:14:17 -05:00
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
attr_reader :default_types
|
2016-05-31 14:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|