Skip to content

Commit

Permalink
Fix for transform to instances
Browse files Browse the repository at this point in the history
  • Loading branch information
EugZol committed Oct 8, 2024
1 parent bc516dc commit 64de628
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/datacaster/predefined.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def pick(*keys)
end
end

must_be(Enumerable) & transform { |input|
must_be(Enumerable) & cast { |input|
result =
keys.map do |key|
Array(key).reduce(input) do |result, k|
Expand All @@ -198,7 +198,8 @@ def pick(*keys)
result
end
end
keys.length == 1 ? result.first : result
result = keys.length == 1 ? result.first : result
Datacaster::ValidResult(result)
}
end

Expand Down
4 changes: 4 additions & 0 deletions lib/datacaster/runtimes/structure_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def will_check!
@should_check_stack[-1] = true
end

def will_not_check!
@should_check_stack[-1] = false
end

# Notify current runtime that some child runtime has built schema,
# child runtime's schema is passed as the argument
def checked_schema!(schema)
Expand Down
4 changes: 2 additions & 2 deletions lib/datacaster/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize(&block)

def cast(object, runtime:)
result = Runtimes::Base.(runtime, @transform, object)
if runtime.respond_to?(:checked_key!) && result.is_a?(Hash)
result.keys.each { |key| runtime.checked_key!(key) }
if runtime.respond_to?(:will_not_check!)
runtime.will_not_check!
end
Datacaster::ValidResult(result)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/structure_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
expect(caster.(a: 'value').to_dry_result).to eq Success(b: 'value')
end

it 'transform can cast to classes' do
i = Class.new.new

caster =
Datacaster.schema do
hash_schema(a: string) & transform { i }
end
expect(caster.(a: 'value').to_dry_result).to eq Success(i)
end

it 'defaults marks hashes as cleaned' do
caster =
Datacaster.schema do
Expand Down

0 comments on commit 64de628

Please sign in to comment.