Skip to content

Commit

Permalink
Add different return flows (#11)
Browse files Browse the repository at this point in the history
* Final Version Wihout Specs

* Fixes and specs

* Fixes for specs

* Update deps

* Move dev deps to the Gemfile

* Increase coverage
  • Loading branch information
AnotherRegularDude committed Dec 11, 2023
1 parent ccb98ee commit 2851014
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 117 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
source "https://rubygems.org"

gemspec

gem "bundler-audit"
gem "ci-helper"
gem "pry"
gem "rake"
gem "rspec"
gem "rubocop-config-umbrellio"
gem "simplecov"
gem "simplecov-lcov"
137 changes: 79 additions & 58 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,121 +1,142 @@
PATH
remote: .
specs:
resol (0.8.0)
resol (0.9.0)
smart_initializer (~> 0.7)

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.2.2)
activesupport (7.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
bundler-audit (0.9.0.1)
base64 (0.2.0)
bigdecimal (3.1.4)
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
ci-helper (0.4.2)
ci-helper (0.5.0)
colorize (~> 0.8)
dry-inflector (~> 0.2)
umbrellio-sequel-plugins (~> 0.4)
coderay (1.1.3)
colorize (0.8.1)
concurrent-ruby (1.1.9)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
diff-lcs (1.5.0)
docile (1.4.0)
dry-inflector (0.2.1)
i18n (1.10.0)
drb (2.2.0)
ruby2_keywords
dry-inflector (0.3.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
method_source (1.0.0)
minitest (5.15.0)
parallel (1.21.0)
parser (3.1.1.0)
minitest (5.20.0)
mutex_m (0.2.0)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
pry (0.14.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
qonfig (0.27.0)
rack (2.2.3)
qonfig (0.28.0)
racc (1.7.3)
rack (3.0.8)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.2.1)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
rake (13.1.0)
regexp_parser (2.8.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.25.1)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.50.2)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.0.0)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.15.1, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.16.0)
parser (>= 3.1.1.0)
rubocop-config-umbrellio (1.25.0.61)
rubocop (~> 1.25.0)
rubocop-performance (~> 1.13.0)
rubocop-rails (~> 2.13.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.19.0)
rubocop (~> 1.41)
rubocop-config-umbrellio (1.50.0.85)
rubocop (~> 1.50.0)
rubocop-performance (~> 1.17.0)
rubocop-rails (~> 2.19.0)
rubocop-rake (~> 0.6.0)
rubocop-rspec (~> 2.7.0)
rubocop-rspec (~> 2.20.0)
rubocop-sequel (~> 0.3.3)
rubocop-performance (1.13.2)
rubocop-performance (1.17.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.13.2)
rubocop-rails (2.19.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.7.0, < 2.0)
rubocop (>= 1.33.0, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.7.0)
rubocop (~> 1.19)
rubocop-sequel (0.3.3)
rubocop-rspec (2.20.0)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-sequel (0.3.4)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
sequel (5.54.0)
simplecov (0.21.2)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sequel (5.74.0)
bigdecimal
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
smart_engine (0.12.0)
smart_initializer (0.9.0)
smart_engine (0.17.0)
smart_initializer (0.11.1)
qonfig (~> 0.24)
smart_engine (~> 0.11)
smart_types (~> 0.4)
smart_types (0.7.0)
smart_engine (~> 0.16)
smart_types (~> 0.8)
smart_types (0.8.0)
smart_engine (~> 0.11)
symbiont-ruby (0.7.0)
thor (1.2.1)
tzinfo (2.0.4)
thor (1.3.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
umbrellio-sequel-plugins (0.5.1.27)
umbrellio-sequel-plugins (0.14.0.189)
sequel
symbiont-ruby
unicode-display_width (2.1.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-21
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand All @@ -130,4 +151,4 @@ DEPENDENCIES
simplecov-lcov

BUNDLED WITH
2.3.8
2.4.10
3 changes: 2 additions & 1 deletion lib/resol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
require "smart_core/initializer"

require_relative "resol/version"
require_relative "resol/return_engine"
require_relative "resol/configuration"
require_relative "resol/service"

module Resol
Configuration = SmartCore::Initializer::Configuration
end
43 changes: 43 additions & 0 deletions lib/resol/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Resol
class Configuration
DEFAULT_RETURN_ENGINE = ReturnEngine::Catch

class << self
def configure
SmartCore::Initializer::Configuration.configure do |c|
self.smartcore_config = c
yield self
self.smartcore_config = nil
end
end

def return_engine
@return_engine || DEFAULT_RETURN_ENGINE
end

def return_engine=(engine)
@return_engine = engine
end

private

attr_accessor :smartcore_config

def method_missing(meth, *args, &block)
# rubocop:disable Style/SafeNavigation
if smartcore_config && smartcore_config.respond_to?(meth)
# rubocop:enable Style/SafeNavigation
smartcore_config.__send__(meth, *args, &block)
else
super(meth, *args, &block)
end
end

def respond_to_missing?(meth, include_private)
smartcore_config.respond_to?(meth, include_private)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/resol/return_engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Resol
module ReturnEngine
NOT_EXITED = Object.new.freeze
DataWrapper = Struct.new(:data)
end
end

require_relative "return_engine/catch"
require_relative "return_engine/return"
24 changes: 24 additions & 0 deletions lib/resol/return_engine/catch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Resol
module ReturnEngine
module Catch
extend self

def wrap_call(service)
catch(service) do
yield
NOT_EXITED
end
end

def uncaught_call?(return_obj)
return_obj == NOT_EXITED
end

def handle_return(service, data)
throw(service, data)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/resol/return_engine/return.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Resol
module ReturnEngine
module Return
extend self

def wrap_call(_service)
yield
end

def uncaught_call?(return_obj)
!return_obj.is_a?(Resol::Service::Result)
end

def handle_return(_service, data)
data
end
end
end
end
11 changes: 7 additions & 4 deletions lib/resol/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ def inherited(klass)
def call(*args, **kwargs, &block)
service = build(*args, **kwargs)

result = catch(service) do
result = return_engine.wrap_call(service) do
service.instance_variable_set(:@__performing__, true)
__run_callbacks__(service)
service.call(&block)
:uncaught
end

if result == :uncaught
if return_engine.uncaught_call?(result)
error_message = "No `#success!` or `#fail!` called in `#call` method in #{service.class}."
raise InvalidCommandImplementation, error_message
else
Expand All @@ -59,6 +58,10 @@ def call(*args, **kwargs, &block)
Resol::Failure(e)
end

def return_engine
Resol::Configuration.return_engine
end

def call!(...)
call(...).value_or { |error| raise error }
end
Expand All @@ -78,7 +81,7 @@ def fail!(code, data = nil)

def success!(data = nil)
check_performing do
throw(self, Result.new(data))
self.class.return_engine.handle_return(self, Result.new(data))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/resol/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Resol
VERSION = "0.8.0"
VERSION = "0.9.0"
end
9 changes: 0 additions & 9 deletions resol.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "smart_initializer", "~> 0.7"

spec.add_development_dependency "bundler-audit"
spec.add_development_dependency "ci-helper"
spec.add_development_dependency "pry"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop-config-umbrellio"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "simplecov-lcov"
end
Loading

0 comments on commit 2851014

Please sign in to comment.