From 1a2d2096383d37640312ec5fdc4b729ee0c00ae9 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Wed, 7 Jul 2021 16:02:06 +0800 Subject: [PATCH] Yield the result of the call from if block is given. --- CHANGELOG.md | 6 ++++++ Gemfile.lock | 6 +++--- README.md | 15 +++++++++++++++ lib/stimpack/functional_object.rb | 8 ++++++-- lib/stimpack/version.rb | 2 +- spec/stimpack/functional_object_spec.rb | 13 ++++++++----- 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c92f5d..661fb54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.8.0 + +### New features + +- Yield the result of the call from `FunctionalObject` if block is given. + ## 0.7.1 ### New features diff --git a/Gemfile.lock b/Gemfile.lock index 20e326d..3abdf00 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,20 +1,20 @@ PATH remote: . specs: - stimpack (0.7.0) + stimpack (0.8.0) activesupport (~> 6.1) GEM remote: https://rubygems.org/ specs: - activesupport (6.1.3.2) + activesupport (6.1.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) ast (2.4.2) - concurrent-ruby (1.1.8) + concurrent-ruby (1.1.9) diff-lcs (1.4.4) i18n (1.8.10) concurrent-ruby (~> 1.0) diff --git a/README.md b/README.md index 97db3fc..0cc1916 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,21 @@ Foo.(bar: "Hello world!") #=> "Hello world!" ``` +You can optionally pass a block to the call which will receive the result of +the method and will execute before returning. + +**Note: The result is still always returned.** + +```ruby +Foo.(bar: "Hello world!") do |result| + if result.successful? + # Do stuff. + else + # Report errors. + end +end +``` + ## OptionsDeclaration A mixin that introduces the concept of an `option`, and lets classes declare diff --git a/lib/stimpack/functional_object.rb b/lib/stimpack/functional_object.rb index a0084a8..3d47345 100644 --- a/lib/stimpack/functional_object.rb +++ b/lib/stimpack/functional_object.rb @@ -11,8 +11,12 @@ module Stimpack # module FunctionalObject module ClassMethods - def call(...) - new(...).() + def call(*arguments, **options) + result = new(*arguments, **options).() + + yield result if block_given? + + result end end diff --git a/lib/stimpack/version.rb b/lib/stimpack/version.rb index 1072409..7fe32d1 100644 --- a/lib/stimpack/version.rb +++ b/lib/stimpack/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Stimpack - VERSION = "0.7.1" + VERSION = "0.8.0" end diff --git a/spec/stimpack/functional_object_spec.rb b/spec/stimpack/functional_object_spec.rb index 7ef8ae1..4ba4220 100644 --- a/spec/stimpack/functional_object_spec.rb +++ b/spec/stimpack/functional_object_spec.rb @@ -21,22 +21,25 @@ Class.new do include Stimpack::FunctionalObject - def initialize(foo, bar:, &block) + def initialize(foo, bar:) @foo = foo @bar = bar - @baz = block.() end - attr_reader :foo, :bar, :baz + attr_reader :foo, :bar def call - [foo, bar, baz] + [foo, bar] end end end it "delegates arguments, options, and block" do - expect(klass.("foo", bar: "bar") { "baz" }).to eq(%w[foo bar baz]) + expect(klass.("foo", bar: "bar") { "baz" }).to eq(%w[foo bar]) + end + + it "yields the return value to the block if given" do + expect { |block| klass.("foo", bar: "bar", &block) }.to yield_with_args(%w[foo bar]) end end end