Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer "inclusion" argument to "in" 🐹 #84

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## unreleased

Features:
- Rename the `in:` option to `inclusion:`.

## v3.3.0

Features:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ end

### Conditions

You can ensure an input is included in a collection by using `in`:
You can ensure an input is included in a collection by using `inclusion`:

```rb
class Pay < Actor
input :currency, in: %w[EUR USD]
input :currency, inclusion: %w[EUR USD]

# …
end
Expand Down
10 changes: 6 additions & 4 deletions lib/service_actor/collectionable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Example:
#
# class Pay < Actor
# input :provider, in: ["MANGOPAY", "PayPal", "Stripe"]
# input :provider, inclusion: ["MANGOPAY", "PayPal", "Stripe"]
# end
module ServiceActor::Collectionable
def self.included(base)
Expand All @@ -16,12 +16,14 @@ def self.included(base)
module PrependedMethods
def _call
self.class.inputs.each do |key, options|
next unless options[:in]
# DEPRECATED: `in` is deprecated in favor of `inclusion`.
inclusion = options[:inclusion] || options[:in]
next unless inclusion

next if options[:in].include?(result[key])
next if inclusion.include?(result[key])

raise ArgumentError,
"Input #{key} must be included in #{options[:in].inspect} " \
"Input #{key} must be included in #{inclusion.inspect} " \
"but instead was #{result[key].inspect}"
end

Expand Down
28 changes: 28 additions & 0 deletions spec/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,34 @@
end
end

context 'when using "inclusion"' do
context "when given a correct value" do
it "returns the message" do
actor = PayWithProviderInclusion.call(provider: "PayPal")
expect(actor.message).to eq("Money transferred to PayPal!")
end
end

context "when given an incorrect value" do
let(:expected_alert) do
"Input provider must be included in " \
'["MANGOPAY", "PayPal", "Stripe"] but instead was "Paypal"'
end

it "fails" do
expect { PayWithProviderInclusion.call(provider: "Paypal") }
.to raise_error(expected_alert)
end
end

context "when it has a default" do
it "uses it" do
actor = PayWithProviderInclusion.call
expect(actor.message).to eq("Money transferred to Stripe!")
end
end
end

context 'when using "in"' do
context "when given a correct value" do
it "returns the message" do
Expand Down
10 changes: 10 additions & 0 deletions spec/examples/pay_with_provider_inclusion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class PayWithProviderInclusion < Actor
input :provider, inclusion: %w[MANGOPAY PayPal Stripe], default: "Stripe"
output :message, type: String

def call
self.message = "Money transferred to #{provider}!"
end
end