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

Autogenerate *? methods for outputs. #59

Merged
merged 17 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ result = BuildGreeting.call
result.greeting # => "Have a wonderful day!"
```

For each output a method is generated ending with a `?`, which is useful when the output is used for
conditional control flow, like if-else statements.

```rb
if result.greeting?
puts "Greetings is a non empty value"
else
puts "Greetings is empty"
end
```

The generated method will return the truthy or falsey values of variables.

### Defaults

Inputs can be marked as optional by providing a default:
Expand Down
26 changes: 26 additions & 0 deletions lib/service_actor/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,31 @@ def [](name)
def display
to_h.fetch(:display)
end

def respond_to?(method_name, include_private = false)
!!method_missing(method_name) || super
end

private

def method_missing(symbol, *args)
attribute = symbol.to_s.chomp("?")

if symbol.end_with?("?") && respond_to?(attribute)
define_singleton_method symbol do
attribute_value = send(attribute.to_sym)

blank_proc = ->(value) { value.respond_to?(:empty?) ? !!value.empty? : !value }

# ActiveSupport ships with native #present?
# Delegate to ActiveSupport's Object#present? if ActiveSupport is present
attribute_value.respond_to?(:present?) ? attribute_value.present? : !blank_proc.call(attribute_value)
rockwellll marked this conversation as resolved.
Show resolved Hide resolved
end

return send(symbol)
end

super symbol, *args
end
end
end
97 changes: 97 additions & 0 deletions spec/result_spec.rb
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"
rockwellll marked this conversation as resolved.
Show resolved Hide resolved

expect(result.respond_to?(:name?)).to eq true
end

context "String" do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow the RSpec/ContextWording RuboCop convention, contexts should start with when, with or without.

Suggested change
context "String" do
context "when input is a String" do

context "is empty" do
it "returns false" do
result = described_class.new
result.name = ""

expect(result.name?).to eq false
end
end

context "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 "Array" do
context "is empty" do
it "returns false" do
result = described_class.new
result.array = []

expect(result.array?).to eq false
end
end

context "is not empty" do
it "returns true" do
result = described_class.new
result.array = [1, 2, 3]

expect(result.array?).to eq true
end
end
end

context "Hash" do
context "is empty" do
it "returns false" do
result = described_class.new
result.hash = {}

expect(result.hash?).to eq false
end
end

context "is not empty" do
it "returns true" do
result = described_class.new
result.hash = { name: "Actor" }

expect(result.hash?).to eq true
end
end
end

context "NilClass" do
it "returns false" do
result = described_class.new
result.name = nil

expect(result.name?).to eq false
end
end

context "TrueClass" do
it "returns true" do
result = described_class.new
result.name = true

expect(result.name?).to eq true
end
end

context "FalseClass" do
it "returns true" do
result = described_class.new
result.name = false

expect(result.name?).to eq false
end
end
end