-
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.
- Loading branch information
Showing
6 changed files
with
140 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
module Datacaster | ||
class AndNode < Base | ||
def initialize(left, right) | ||
@left = left | ||
@right = right | ||
def initialize(*casters) | ||
@casters = casters | ||
end | ||
|
||
def cast(object, runtime:) | ||
left_result = @left.with_runtime(runtime).(object) | ||
return left_result unless left_result.valid? | ||
|
||
@right.with_runtime(runtime).(left_result.value) | ||
Datacaster.ValidResult( | ||
@casters.reduce(object) do |result, caster| | ||
caster_result = caster.with_runtime(runtime).(result) | ||
return caster_result unless caster_result.valid? | ||
caster_result.value | ||
end | ||
) | ||
end | ||
|
||
def inspect | ||
"#<Datacaster::AndNode L: #{@left.inspect} R: #{@right.inspect}>" | ||
"#<Datacaster::AndNode casters: #{@casters.inspect}>" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Datacaster | ||
module Transaction | ||
module ClassMethods | ||
def perform(caster) | ||
@_caster = ContextNodes::StructureCleaner.new(caster, :fail) | ||
end | ||
|
||
def perform_partial(caster) | ||
@_caster = ContextNodes::StructureCleaner.new(caster, :pass) | ||
end | ||
|
||
def perform_choosy(caster) | ||
@_caster = ContextNodes::StructureCleaner.new(caster, :remove) | ||
end | ||
|
||
def call(*args, **kwargs) | ||
new.call(*args, **kwargs) | ||
end | ||
end | ||
|
||
def self.included(base) | ||
base.extend Datacaster::Predefined | ||
base.extend ClassMethods | ||
base.include Datacaster::Mixin | ||
end | ||
|
||
def cast(object, runtime:) | ||
self.class.instance_variable_get(:@_caster). | ||
with_runtime(runtime). | ||
(object) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
RSpec.describe Datacaster::Transaction do | ||
include Dry::Monads[:result] | ||
|
||
before(:all) do | ||
@i18n_module = Datacaster::Config.i18n_module | ||
Datacaster::Config.i18n_module = Datacaster::SubstituteI18n | ||
end | ||
|
||
after(:all) do | ||
Datacaster::Config.i18n_module = @i18n_module | ||
end | ||
|
||
it "returns true for Datacaster.instance?" do | ||
tx = | ||
Class.new do | ||
include Datacaster::Transaction | ||
end | ||
|
||
expect(Datacaster.instance?(tx.new)).to eq true | ||
end | ||
|
||
it "performs basic casting with Datacaster::Predefined casters" do | ||
tx = | ||
Class.new do | ||
include Datacaster::Transaction | ||
|
||
perform integer | ||
end | ||
|
||
expect(tx.new.(5).to_dry_result).to eq Success(5) | ||
expect(tx.(5).to_dry_result).to eq Success(5) | ||
end | ||
|
||
it "performs multi-step casting" do | ||
tx = | ||
Class.new do | ||
include Datacaster::Transaction | ||
|
||
unwrap = transform { |x| x.to_h } | ||
typecast = hash_schema(name: string, email: string) | ||
send_email = transform do |email| | ||
{address: email, result: true} | ||
end | ||
|
||
perform steps( | ||
unwrap, | ||
typecast, | ||
transform_to_hash(email: pick(:email) & send_email) | ||
) | ||
end | ||
|
||
expect(tx.(name: 'John', email: '[email protected]').to_dry_result).to eq Success( | ||
name: 'John', | ||
email: {address: '[email protected]', result: true} | ||
) | ||
end | ||
end |