diff --git a/.gitignore b/.gitignore index f87c6ab..07b27ef 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,6 @@ node_modules /docs/build/ +.DS_Store .cache/ .docusaurus/ diff --git a/lib/panko/attribute.rb b/lib/panko/attribute.rb index d83112c..209d19d 100644 --- a/lib/panko/attribute.rb +++ b/lib/panko/attribute.rb @@ -3,10 +3,18 @@ module Panko class Attribute def self.create(name, alias_name: nil) - alias_name = alias_name.to_s unless alias_name.nil? + alias_name = alias_name.nil? ? transform_key(name.to_s) : alias_name.to_s Attribute.new(name.to_s, alias_name) end + def self.transform_key(name) + case Panko.configuration.key_type + when Panko::Configuration::CAMEL_CASE_LOWER then name.camelize(:lower) + when Panko::Configuration::CAMEL_CASE then name.camelize + else name + end + end + def ==(other) return name.to_sym == other if other.is_a? Symbol return name == other.name && alias_name == other.alias_name if other.is_a? Panko::Attribute diff --git a/lib/panko/configuration.rb b/lib/panko/configuration.rb new file mode 100644 index 0000000..83deeb0 --- /dev/null +++ b/lib/panko/configuration.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Panko + class Configuration + InitializeError = Class.new(StandardError) + + CAMEL_CASE_LOWER = "camelCase" + CAMEL_CASE = "CamelCase" + AVAILABLE_KEY_TYPES = [CAMEL_CASE_LOWER, CAMEL_CASE].freeze + + attr_reader :key_type + + def initialize + @key_type = nil + end + + def key_type=(value) + raise InitializeError, "Invalid key type" unless AVAILABLE_KEY_TYPES.include?(key_type) + + @key_type = value + end + end +end diff --git a/lib/panko_serializer.rb b/lib/panko_serializer.rb index 034a023..5ec9d27 100644 --- a/lib/panko_serializer.rb +++ b/lib/panko_serializer.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "panko/version" +require "panko/configuration" require "panko/attribute" require "panko/association" require "panko/serializer" @@ -12,3 +13,22 @@ # C Extension require "oj" require "panko/panko_serializer" + +module Panko + extend self + + # Public: Configure Panko. + # + # Panko.configure do |config| + # config.key_type = 'camelCase' + # end + # + def configure + yield configuration + end + + # Public: Returns Panko::Configuration instance. + def configuration + @configuration ||= Configuration.new + end +end diff --git a/spec/panko/serializer_spec.rb b/spec/panko/serializer_spec.rb index bc3025a..72b22a4 100644 --- a/spec/panko/serializer_spec.rb +++ b/spec/panko/serializer_spec.rb @@ -741,4 +741,95 @@ def created_at end end end + + context "key_type" do + let(:configuration) { Panko::Configuration.new } + let(:data) { {"created_at" => created_at} } + let(:created_at) { "2023-04-18T09:24:41+00:00" } + + before do + allow(Panko).to receive_messages(configuration: configuration) + end + + context "with camelCase" do + before { configuration.key_type = "camelCase" } + + context "with key_type" do + let(:serializer_class) do + Class.new(Panko::Serializer) do + attributes :created_at + end + end + + it "has createdAt" do + expect(data).to serialized_as(serializer_class, + "createdAt" => created_at) + end + end + + context "with key_type + method_fields" do + let(:serializer_class) do + Class.new(Panko::Serializer) do + attributes :created_at + + def created_at + "2023-04-18T09:24:41+00:00" + end + end + end + + it "has createdAt" do + expect(data).to serialized_as(serializer_class, + "createdAt" => created_at) + end + end + + context "with key_type and alias" do + let(:serializer_class) do + Class.new(Panko::Serializer) do + aliases({created_at: :CreatedAt}) + end + end + + it "alias has priority over configuration" do + expect(data).to serialized_as(serializer_class, + "CreatedAt" => created_at) + end + end + end + + context "with CamelCase" do + before { configuration.key_type = "CamelCase" } + + context "with key_type" do + let(:serializer_class) do + Class.new(Panko::Serializer) do + attributes :created_at + end + end + + it "has CreatedAt" do + expect(data).to serialized_as(serializer_class, + "CreatedAt" => created_at) + end + end + + context "with key_type + method_fields" do + let(:serializer_class) do + Class.new(Panko::Serializer) do + attributes :created_at + + def created_at + "2023-04-18T09:24:41+00:00" + end + end + end + + it "has CreatedAt" do + expect(data).to serialized_as(serializer_class, + "CreatedAt" => created_at) + end + end + end + end end