lib/basic_serializer.rb in basic_serializer-0.1.7 vs lib/basic_serializer.rb in basic_serializer-0.1.8
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
require "oj"
require "yaml"
+require "set"
require "basic_serializer/dsl"
require "basic_serializer/swagger"
require "basic_serializer/struct"
require "basic_serializer/config"
require "basic_serializer/hash"
@@ -26,21 +27,19 @@
end
alias as_json stringified_attributes
def to_json(*_args)
- pretty = self.class.instance_variable_get(:@format)&.dig(:pretty)
-
- Oj.dump(stringified_attributes, **(pretty ? Config::OJ_FORMAT : {}))
+ Oj.dump(stringified_attributes, **json_format_options)
end
def to_yaml
YAML.dump(stringified_attributes)
end
def serialize
- case self.class.instance_variable_get(:@format)&.dig(:name)
+ case self.class.format_settings[:name]
when :json then to_json
when :yaml then to_yaml
else stringified_attributes
end
end
@@ -64,7 +63,11 @@
return @struct if @struct
basic_struct ||= BasicSerializer::Struct.new(*object.keys.map(&:to_sym), keyword_init: true)
@struct ||= basic_struct.new(**object)
+ end
+
+ def json_format_options
+ @json_format_options ||= self.class.format_settings[:pretty] ? Config::OJ_FORMAT : {}
end
end