-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ContextNode, refactored with_context, cast_errors and structure…
… cleaning (#3) * Fix merge message keys * Extracted ContextNode * Refactored with_context to new ContextNode * Refactored cast_errors to ContextNode --------- Co-authored-by: Pavel Egorov <[email protected]>
- Loading branch information
Showing
35 changed files
with
635 additions
and
431 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
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
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
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,43 @@ | ||
module Datacaster | ||
class ContextNode < Base | ||
def initialize(base) | ||
@base = base | ||
end | ||
|
||
def cast(object, runtime:) | ||
@runtime = create_runtime(runtime) | ||
result = @base.with_runtime(@runtime).call(object) | ||
transform_result(result) | ||
end | ||
|
||
def inspect | ||
"#<Datacaster::ContextNode base: #{@base.inspect}>" | ||
end | ||
|
||
private | ||
|
||
def create_runtime(parent) | ||
parent | ||
end | ||
|
||
def runtime | ||
@runtime | ||
end | ||
|
||
def transform_result(result) | ||
if result.valid? | ||
Datacaster.ValidResult(transform_success(result.value)) | ||
else | ||
Datacaster.ErrorResult(transform_errors(result.errors)) | ||
end | ||
end | ||
|
||
def transform_success(value) | ||
value | ||
end | ||
|
||
def transform_errors(errors) | ||
errors | ||
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,21 @@ | ||
module Datacaster | ||
module ContextNodes | ||
class ErrorsCaster < Datacaster::ContextNode | ||
def initialize(base, error_caster) | ||
super(base) | ||
@caster = error_caster | ||
end | ||
|
||
private | ||
|
||
def transform_errors(errors) | ||
result = @caster.with_runtime(@runtime).(errors) | ||
if result.valid? | ||
result.value | ||
else | ||
raise RuntimeError.new("Error caster tried to cast these errors: #{errors.inspect}, but didn't return ValidResult: #{result.inspect}") | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.