-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,347 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
module CallableAndThen | ||
require_relative 'callable_and_then/error' | ||
require_relative 'callable_and_then/config' | ||
require_relative 'callable_and_then/caller' | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
class CallableAndThen::Caller | ||
def self.call(source, value:, injected_value:, method_name:) | ||
method = callable_method(source, method_name) | ||
|
||
Transitions.tracking.record_and_then(method, injected_value, source) do | ||
result = | ||
if source.is_a?(::Proc) | ||
call_proc!(source, value, injected_value) | ||
else | ||
call_method!(source, method, value, injected_value) | ||
end | ||
|
||
ensure_result_object(source, value, result) | ||
end | ||
end | ||
|
||
def self.call_proc!(source, value, injected_value) | ||
case source.arity | ||
when 1 then source.call(value) | ||
when 2 then source.call(value, injected_value) | ||
else raise CallableAndThen::Error::InvalidArity.build(source: source, method: :call, arity: '1..2') | ||
end | ||
end | ||
|
||
def self.call_method!(source, method, value, injected_value) | ||
case method.arity | ||
when 1 then source.send(method.name, value) | ||
when 2 then source.send(method.name, value, injected_value) | ||
else raise CallableAndThen::Error::InvalidArity.build(source: source, method: method.name, arity: '1..2') | ||
end | ||
end | ||
|
||
def self.callable_method(source, method_name) | ||
source.method(method_name || Config.instance.and_then!.default_method_name_to_call) | ||
end | ||
|
||
def self.ensure_result_object(source, _value, result) | ||
return result if result.is_a?(::BCDD::Result) | ||
|
||
raise Error::UnexpectedOutcome.build(outcome: result, origin: source) | ||
end | ||
|
||
private_class_method :new, :allocate | ||
private_class_method :call_proc!, :call_method!, :callable_method, :ensure_result_object | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
class CallableAndThen::Config | ||
attr_accessor :default_method_name_to_call | ||
|
||
def initialize | ||
self.default_method_name_to_call = :call | ||
end | ||
|
||
def options | ||
{ default_method_name_to_call: default_method_name_to_call } | ||
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
class CallableAndThen::Error < Error | ||
class InvalidArity < self | ||
def self.build(source:, method:, arity:) | ||
new("Invalid arity for #{source.class}##{method} method. Expected arity: #{arity}") | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result | ||
module Context::CallableAndThen | ||
class Caller < CallableAndThen::Caller | ||
module KeyArgs | ||
def self.parameters?(source) | ||
parameters = source.parameters.map(&:first) | ||
|
||
!parameters.empty? && parameters.all?(/\Akey/) | ||
end | ||
|
||
def self.invalid_arity(source, method) | ||
CallableAndThen::Error::InvalidArity.build(source: source, method: method, arity: 'only keyword args') | ||
end | ||
end | ||
|
||
def self.call_proc!(source, value, _injected_value) | ||
return source.call(**value) if KeyArgs.parameters?(source) | ||
|
||
raise KeyArgs.invalid_arity(source, :call) | ||
end | ||
|
||
def self.call_method!(source, method, value, _injected_value) | ||
return source.send(method.name, **value) if KeyArgs.parameters?(method) | ||
|
||
raise KeyArgs.invalid_arity(source, method.name) | ||
end | ||
|
||
def self.ensure_result_object(source, value, result) | ||
return result.tap { result.send(:acc).then { _1.merge!(value.merge(_1)) } } if result.is_a?(Context) | ||
|
||
raise Error::UnexpectedOutcome.build(outcome: result, origin: source, expected: Context::EXPECTED_OUTCOME) | ||
end | ||
|
||
private_class_method :call_proc!, :call_method! | ||
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class BCDD::Result::Context::Success < BCDD::Result::Context | ||
include ::BCDD::Result::Success::Methods | ||
class BCDD::Result | ||
class Context::Success < Context | ||
include ::BCDD::Result::Success::Methods | ||
|
||
def and_expose(type, keys, terminal: true) | ||
unless keys.is_a?(::Array) && !keys.empty? && keys.all?(::Symbol) | ||
raise ::ArgumentError, 'keys must be an Array of Symbols' | ||
end | ||
def and_expose(type, keys, terminal: true) | ||
unless keys.is_a?(::Array) && !keys.empty? && keys.all?(::Symbol) | ||
raise ::ArgumentError, 'keys must be an Array of Symbols' | ||
end | ||
|
||
Transitions.tracking.reset_and_then! | ||
|
||
exposed_value = acc.merge(value).slice(*keys) | ||
exposed_value = acc.merge(value).slice(*keys) | ||
|
||
self.class.new(type: type, value: exposed_value, source: source, terminal: terminal) | ||
self.class.new(type: type, value: exposed_value, source: source, terminal: terminal) | ||
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
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
Oops, something went wrong.