Skip to content

Commit

Permalink
Allow to provide any options as strings
Browse files Browse the repository at this point in the history
#161

All strings options will be converted to symbols
  • Loading branch information
aglushkov committed Nov 18, 2024
1 parent cd19c51 commit 6c01660
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

- Allow to provide modifiers and serialization options as strings. Only symbols
were allowed previously.

## [0.20.1] - 2024-02-25

- Fix issue with :if plugin used together with :batch plugin.
Expand Down
16 changes: 12 additions & 4 deletions lib/serega.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def call(object, opts = nil)
modifiers_opts = FROZEN_EMPTY_HASH
serialize_opts = nil
else
opts.transform_keys!(&:to_sym)
serialize_opts = opts.except(*initiate_keys)
modifiers_opts = opts.slice(*initiate_keys)
end
Expand Down Expand Up @@ -296,7 +297,14 @@ module InstanceMethods
# @option opts [Boolean] :validate Validates provided modifiers (Default is true)
#
def initialize(opts = nil)
@opts = (opts.nil? || opts.empty?) ? FROZEN_EMPTY_HASH : parse_modifiers(opts)
@opts =
if opts.nil? || opts.empty?
FROZEN_EMPTY_HASH
else
opts.transform_keys!(&:to_sym)
parse_modifiers(opts)
end

self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params }

@plan = self.class::SeregaPlan.call(@opts)
Expand All @@ -320,10 +328,10 @@ def initialize(opts = nil)
# @return [Hash] Serialization result
#
def call(object, opts = nil)
self.class::CheckSerializeParams.new(opts).validate if opts&.any?
opts ||= {}
opts[:context] ||= {}
opts = opts ? opts.transform_keys!(&:to_sym) : {}
self.class::CheckSerializeParams.new(opts).validate unless opts.empty?

opts[:context] ||= {}
serialize(object, opts)
end

Expand Down
52 changes: 34 additions & 18 deletions spec/serega_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,62 +180,78 @@ def self.plugin_name
end
end

let(:opts) { {except: :except} }
let(:serializer) { serializer_class.new(**opts) }
let(:modifiers) { {except: :except} }
let(:serialize_opts) { {context: {data: "bar"}} }

let(:serializer) { serializer_class.new(modifiers) }

describe "#call" do
it "returns serialized response" do
expect(serializer.call("foo", context: {data: "bar"}))
.to eq({obj: "foo", ctx: "bar"})
expect(serializer.call("foo", serialize_opts)).to eq({obj: "foo", ctx: "bar"})
end

context "with string opts" do
before do
modifiers.transform_keys!(&:to_s)
serialize_opts.transform_keys!(&:to_s)
end

it "returns correct response when options provided with string keys" do
expect(serializer.call("foo", serialize_opts)).to eq({obj: "foo", ctx: "bar"})
end
end
end

describe "#to_h" do
it "returns serialized response same as .call method" do
expect(serializer.to_h("foo", context: {data: "bar"}))
.to eq({obj: "foo", ctx: "bar"})
expect(serializer.to_h("foo", serialize_opts)).to eq({obj: "foo", ctx: "bar"})
end
end

describe "#to_json" do
it "returns serialized to json response" do
expect(serializer.to_json("foo", context: {data: "bar"}))
.to eq('{"obj":"foo","ctx":"bar"}')
expect(serializer.to_json("foo", serialize_opts)).to eq('{"obj":"foo","ctx":"bar"}')
end
end

describe "#as_json" do
it "returns serialized as json response (with JSON compatible types)" do
expect(serializer.as_json("foo", context: {data: "bar"}))
.to eq({"obj" => "foo", "ctx" => "bar"})
expect(serializer.as_json("foo", serialize_opts)).to eq({"obj" => "foo", "ctx" => "bar"})
end
end

describe ".call" do
it "returns serialized to response" do
expect(serializer_class.call("foo", **opts, context: {data: "bar"}))
.to eq({obj: "foo", ctx: "bar"})
expect(serializer_class.call("foo", modifiers.merge(serialize_opts))).to eq({obj: "foo", ctx: "bar"})
end

context "with string opts" do
before do
modifiers.transform_keys!(&:to_s)
serialize_opts.transform_keys!(&:to_s)
end

it "returns correct response when options provided with string keys" do
expect(serializer_class.call("foo", modifiers.merge(serialize_opts))).to eq({obj: "foo", ctx: "bar"})
end
end
end

describe ".to_h" do
it "returns serialized response same as .call method" do
expect(serializer_class.to_h("foo", **opts, context: {data: "bar"}))
.to eq({obj: "foo", ctx: "bar"})
expect(serializer_class.to_h("foo", modifiers.merge(serialize_opts))).to eq({obj: "foo", ctx: "bar"})
end
end

describe ".to_json" do
it "returns serialized to json response" do
expect(serializer_class.to_json("foo", **opts, context: {data: "bar"}))
.to eq('{"obj":"foo","ctx":"bar"}')
expect(serializer_class.to_json("foo", modifiers.merge(serialize_opts))).to eq('{"obj":"foo","ctx":"bar"}')
end
end

describe ".as_json" do
it "returns serialized as json response (with JSON compatible types)" do
expect(serializer_class.as_json("foo", **opts, context: {data: "bar"}))
.to eq({"obj" => "foo", "ctx" => "bar"})
expect(serializer_class.as_json("foo", modifiers.merge(serialize_opts))).to eq({"obj" => "foo", "ctx" => "bar"})
end
end
end
Expand Down

0 comments on commit 6c01660

Please sign in to comment.