Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add boolean methods to outputs (#59)
- Loading branch information
1 parent
50f1858
commit ff769b7
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,5 +51,28 @@ def [](name) | |
def display | ||
to_h.fetch(:display) | ||
end | ||
|
||
private | ||
|
||
def respond_to_missing?(method_name, include_private = false) | ||
method_name.to_s.end_with?("?") || super | ||
end | ||
|
||
def method_missing(symbol, *args) | ||
attribute = symbol.to_s.chomp("?") | ||
|
||
if symbol.to_s.end_with?("?") && respond_to?(attribute) | ||
define_singleton_method symbol do | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rockwellll
Author
Contributor
|
||
attribute_value = send(attribute.to_sym) | ||
|
||
# Same as ActiveSupport’s #present? | ||
attribute_value.respond_to?(:empty?) ? !attribute_value.empty? : !!attribute_value | ||
end | ||
|
||
return send(symbol) | ||
end | ||
|
||
super symbol, *args | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ServiceActor::Result do | ||
it "defines a method ending with *? suffix for each attribute" do | ||
result = described_class.new | ||
result.name = "Sunny" | ||
|
||
expect(result.respond_to?(:name?)).to eq true | ||
end | ||
|
||
context "when input is String" do | ||
context "when is empty" do | ||
it "returns false" do | ||
result = described_class.new | ||
result.name = "" | ||
|
||
expect(result.name?).to eq false | ||
end | ||
end | ||
|
||
context "when is not empty" do | ||
it "returns true" do | ||
result = described_class.new | ||
result.name = "Actor" | ||
|
||
expect(result.name?).to eq true | ||
end | ||
end | ||
end | ||
|
||
context "when input is Array" do | ||
context "when is empty" do | ||
it "returns false" do | ||
result = described_class.new | ||
result.options = [] | ||
|
||
expect(result.options?).to eq false | ||
end | ||
end | ||
|
||
context "when is not empty" do | ||
it "returns true" do | ||
result = described_class.new | ||
result.options = [1, 2, 3] | ||
|
||
expect(result.options?).to eq true | ||
end | ||
end | ||
end | ||
|
||
context "when input is Hash" do | ||
context "when empty" do | ||
it "returns false" do | ||
result = described_class.new | ||
result.options = {} | ||
|
||
expect(result.options?).to eq false | ||
end | ||
end | ||
|
||
context "when not empty" do | ||
it "returns true" do | ||
result = described_class.new | ||
result.options = { name: "Actor" } | ||
|
||
expect(result.options?).to eq true | ||
end | ||
end | ||
end | ||
|
||
context "when input is NilClass" do | ||
it "returns false" do | ||
result = described_class.new | ||
result.name = nil | ||
|
||
expect(result.name?).to eq false | ||
end | ||
end | ||
|
||
context "when input is TrueClass" do | ||
it "returns true" do | ||
result = described_class.new | ||
result.name = true | ||
|
||
expect(result.name?).to eq true | ||
end | ||
end | ||
|
||
context "when input is FalseClass" do | ||
it "returns true" do | ||
result = described_class.new | ||
result.name = false | ||
|
||
expect(result.name?).to eq false | ||
end | ||
end | ||
end |
looks like a rubocop bug, because the default is
allowed_in_returns
and here it's in the return of adefine_method
https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/DoubleNegation