-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(has_j_send_result_short_syntax): allow to pass message and code …
…to short form of success
- Loading branch information
Showing
9 changed files
with
336 additions
and
106 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
lib/convenient_service/service/plugins/has_j_send_result_short_syntax/success/commands.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "commands/refute_kwargs_contain_data_and_extra_keys" | ||
require_relative "commands/refute_kwargs_contain_j_send_and_extra_keys" |
29 changes: 0 additions & 29 deletions
29
..._j_send_result_short_syntax/success/commands/refute_kwargs_contain_data_and_extra_keys.rb
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
..._send_result_short_syntax/success/commands/refute_kwargs_contain_j_send_and_extra_keys.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Service | ||
module Plugins | ||
module HasJSendResultShortSyntax | ||
module Success | ||
module Commands | ||
class RefuteKwargsContainJSendAndExtraKeys < Support::Command | ||
## | ||
# @attribute [r] kwargs | ||
# @return [Hash{Symbol => Object}] | ||
# | ||
attr_reader :kwargs | ||
|
||
## | ||
# @param kwargs [Hash{Symbol => Object}] | ||
# @return [void] | ||
# | ||
def initialize(kwargs:) | ||
@kwargs = kwargs | ||
end | ||
|
||
## | ||
# @return [void] | ||
# @raise [ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys] | ||
# | ||
# @example `kwargs.keys.difference([:data, :message, :code]).none?`. | ||
# | ||
# {}.keys.difference([:data, :message, :code]).none? | ||
# # => true | ||
# | ||
# {a: 1}.keys.difference([:data, :message, :code]).none? | ||
# # => false | ||
# | ||
# {data: {}}.keys.difference([:data, :message, :code]).none? | ||
# # => true | ||
# | ||
# {data: {}, a: 1}.keys.difference([:data, :message, :code]).none? | ||
# # => false | ||
# | ||
# {data: {}, message: ""}.keys.difference([:data, :message, :code]).none? | ||
# # => true | ||
# | ||
# {data: {}, message: "", a: 1}.keys.difference([:data, :message, :code]).none? | ||
# # => false | ||
# | ||
# {data: {}, message: "", code: :""}.keys.difference([:data, :message, :code]).none? | ||
# # => true | ||
# | ||
# {data: {}, message: "", code: :"", a: 1}.keys.difference([:data, :message, :code]).none? | ||
# # => false | ||
# | ||
def call | ||
return if [:data, :message, :code].none? { |key| kwargs.has_key?(key) } | ||
|
||
return if kwargs.keys.difference([:data, :message, :code]).none? | ||
|
||
raise Exceptions::KwargsContainJSendAndExtraKeys.new | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
...nd_result_short_syntax/success/commands/refute_kwargs_contain_data_and_extra_keys_spec.rb
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
..._result_short_syntax/success/commands/refute_kwargs_contain_j_send_and_extra_keys_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
require "convenient_service" | ||
|
||
# rubocop:disable RSpec/NestedGroups | ||
RSpec.describe ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Commands::RefuteKwargsContainJSendAndExtraKeys do | ||
include ConvenientService::RSpec::Matchers::Results | ||
|
||
example_group "class methods" do | ||
describe ".call" do | ||
subject(:command_result) { described_class.call(kwargs: kwargs) } | ||
|
||
context "when `kwargs` do NOT contain `:data`, `:message` and `code` keys" do | ||
let(:kwargs) { {foo: :bar} } | ||
|
||
it "does NOT raise" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when kwargs contain `:data` key" do | ||
context "when kwargs do NOT contain extra keys" do | ||
let(:kwargs) { {data: {foo: :bar}} } | ||
|
||
it "does NOT raise" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when kwargs contain extra keys" do | ||
let(:kwargs) { {data: {foo: :bar}, extra_key: anything} } | ||
|
||
let(:exception_message) do | ||
<<~TEXT | ||
`kwargs` passed to `success` method contain JSend keys and extra keys. That's NOT allowed. | ||
Please, consider something like: | ||
# Shorter form. Assumes that all kwargs are `data`. | ||
success(foo: :bar) | ||
# Longer form. More explicit. | ||
success(data: {foo: :bar}) | ||
# (Advanced) Longer form also supports any variation of `data`, `message` and `code`. | ||
success(data: {foo: :bar}, message: "foo") | ||
success(data: {foo: :bar}, code: :foo) | ||
success(message: "foo", code: :foo) | ||
success(data: {foo: :bar}, message: "foo", code: :foo) | ||
TEXT | ||
end | ||
|
||
it "raises `ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys`" do | ||
expect { command_result } | ||
.to raise_error(ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys) | ||
.with_message(exception_message) | ||
end | ||
end | ||
end | ||
|
||
context "when kwargs contain `:message` key" do | ||
context "when kwargs do NOT contain extra keys" do | ||
let(:kwargs) { {message: "foo"} } | ||
|
||
it "does NOT raise" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when kwargs contain extra keys" do | ||
let(:kwargs) { {message: "foo", extra_key: anything} } | ||
|
||
let(:exception_message) do | ||
<<~TEXT | ||
`kwargs` passed to `success` method contain JSend keys and extra keys. That's NOT allowed. | ||
Please, consider something like: | ||
# Shorter form. Assumes that all kwargs are `data`. | ||
success(foo: :bar) | ||
# Longer form. More explicit. | ||
success(data: {foo: :bar}) | ||
# (Advanced) Longer form also supports any variation of `data`, `message` and `code`. | ||
success(data: {foo: :bar}, message: "foo") | ||
success(data: {foo: :bar}, code: :foo) | ||
success(message: "foo", code: :foo) | ||
success(data: {foo: :bar}, message: "foo", code: :foo) | ||
TEXT | ||
end | ||
|
||
it "raises `ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys`" do | ||
expect { command_result } | ||
.to raise_error(ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys) | ||
.with_message(exception_message) | ||
end | ||
end | ||
end | ||
|
||
context "when kwargs contain `:code` key" do | ||
context "when kwargs do NOT contain extra keys" do | ||
let(:kwargs) { {code: :foo} } | ||
|
||
it "does NOT raise" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when kwargs contain extra keys" do | ||
let(:kwargs) { {code: :foo, extra_key: anything} } | ||
|
||
let(:exception_message) do | ||
<<~TEXT | ||
`kwargs` passed to `success` method contain JSend keys and extra keys. That's NOT allowed. | ||
Please, consider something like: | ||
# Shorter form. Assumes that all kwargs are `data`. | ||
success(foo: :bar) | ||
# Longer form. More explicit. | ||
success(data: {foo: :bar}) | ||
# (Advanced) Longer form also supports any variation of `data`, `message` and `code`. | ||
success(data: {foo: :bar}, message: "foo") | ||
success(data: {foo: :bar}, code: :foo) | ||
success(message: "foo", code: :foo) | ||
success(data: {foo: :bar}, message: "foo", code: :foo) | ||
TEXT | ||
end | ||
|
||
it "raises `ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys`" do | ||
expect { command_result } | ||
.to raise_error(ConvenientService::Service::Plugins::HasJSendResultShortSyntax::Success::Exceptions::KwargsContainJSendAndExtraKeys) | ||
.with_message(exception_message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable RSpec/NestedGroups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.