Skip to content

Commit

Permalink
Final Version Wihout Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude committed Nov 13, 2023
1 parent ccb98ee commit 07962fc
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ PLATFORMS
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
42 changes: 42 additions & 0 deletions lib/resol/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Resol
class Configuration < Delegator
DEFAULT_RETURN_ENGINE = ReturnEngine::Catch

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

def return_engine
@return_engine || DEFAULT_RETURN_ENGINE
end

def return_engine=(engine)
@return_engine = engine
end

private

attr_accessor :configurator

def method_missing(meth, *args, &block)
if configurator.respond_to?(target)
configurator.__send__(meth, *args, &block)
elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
::Kernel.instance_method(meth).bind_call(self, *args, &block)
else
super(meth, *args, &block)
end
end

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

module Resol
module ReturnEngine
NOT_EXITED = Object.new.freeze
end
end
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
23 changes: 23 additions & 0 deletions lib/resol/return_engine/return.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Resol
module ReturnEngine
module Return
extend self

DataWrapper = Struct.new(:data)

def wrap_call(_service)
yield
end

def uncaught_call?(return_obj)
!return_obj.is_a?(DataWrapper)
end

def handle_return(_service, data)
DataWrapper.new(data)
end
end
end
end
7 changes: 3 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 Down Expand Up @@ -78,7 +77,7 @@ def fail!(code, data = nil)

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

Expand Down

0 comments on commit 07962fc

Please sign in to comment.