lib/bright_serializer/serializer.rb in bright_serializer-0.4.1 vs lib/bright_serializer/serializer.rb in bright_serializer-0.5.0
- old
+ new
@@ -1,9 +1,8 @@
# frozen_string_literal: true
require 'oj'
-require 'active_support/deprecation'
require_relative 'attribute'
require_relative 'attribute_relation'
require_relative 'inflector'
require_relative 'entity/base'
require_relative 'extensions'
@@ -12,15 +11,10 @@
module Serializer
include Extensions
SUPPORTED_TRANSFORMATION = %i[camel camel_lower dash underscore].freeze
DEFAULT_OJ_OPTIONS = { mode: :compat, time_format: :ruby, use_to_json: true }.freeze
- DEPRECATION_MESSAGE = 'BrightSerializer: Serializing `nil` will stop returning ' \
- "a JSON with all attributes and null values.\n" \
- "To keep the old behaviour use an empty hash `MySerializer.new(object || { }).to_json`.\n" \
- "See: https://github.com/petalmd/bright_serializer/issues/103 for more details about this change.\n"
- private_constant :DEPRECATION_MESSAGE
def self.included(base)
super
base.extend ClassMethods
base.instance_variable_set(:@attributes_to_serialize, [])
@@ -31,11 +25,11 @@
@params = options.delete(:params)
@fields = options.delete(:fields)
end
def serialize(object, attributes_to_serialize)
- ActiveSupport::Deprecation.warn(DEPRECATION_MESSAGE) if object.nil?
+ return nil if @object.nil?
attributes_to_serialize.each_with_object({}) do |attribute, result|
next unless attribute.condition?(object, @params)
result[attribute.transformed_key] = attribute.serialize(self, object, @params)
@@ -66,11 +60,12 @@
subclass.instance_variable_set(:@attributes_to_serialize, []) unless subclass.attributes_to_serialize
subclass.attributes_to_serialize.concat(@attributes_to_serialize)
subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method
end
- def attributes(*attributes, **options, &block)
- attributes.each do |key|
+ def attributes(*args, &block)
+ options = args.extract_options!
+ args.each do |key|
attribute = Attribute.new(key, options[:if], options[:entity], &block)
attribute.transformed_key = run_transform_key(key)
@attributes_to_serialize << attribute
end
end