Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce has_key? for user context #24

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,18 @@ schema.with_context(five: 15).(nil)
# => Datacaster::ValidResult(nil)
```

Method `has_key?` could be used to determine whether key is available in the context

```ruby
schema =
Datacaster.schema do
check { context.has_key?(:five) }
end

schema.with_context(five: 15).(nil)
# => Datacaster::ValidResult(nil)
```

**Note**

`context` can be accesed only in casters' blocks. It can't be used in schema definition itself:
Expand Down
15 changes: 11 additions & 4 deletions lib/datacaster/runtimes/user_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Datacaster
module Runtimes
class UserContext < Base
class ContextStruct
def self.context_has_key?(context, key)
context.respond_to?(:key?) && context.key?(key) || context.to_h.key?(key.to_sym)
end

def initialize(context, node)
@context = context
@node = node
Expand All @@ -14,10 +18,7 @@ def method_missing(m, *args)
return super
end

key_present = @context.respond_to?(:key?) && @context.key?(m) ||
@context.to_h.key?(m.to_sym)

if key_present && args.empty?
if self.class.context_has_key?(@context, m) && args.empty?
return @context[m]
end

Expand All @@ -31,6 +32,12 @@ def method_missing(m, *args)
raise NoMethodError.new("Key #{m.inspect} is not found in the context")
end
end

def has_key?(key)
self.class.context_has_key?(@context, key) || @node.class.send_to_parent(@node, :context).has_key?(key)
rescue NoMethodError
false
end
end

def initialize(parent, user_context)
Expand Down
2 changes: 1 addition & 1 deletion lib/datacaster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Datacaster
VERSION = "4.0.1"
VERSION = "4.1.0"
end
25 changes: 25 additions & 0 deletions spec/user_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@
expect(schema.with_context(d: 2, e: 3).({a: 1, b: {c: {d: 10}}, e: [3]}).to_dry_result).to eq Failure({b: {c: {d: ["is invalid"]}}})
end
end

describe "using #has_key?" do
it "works with deep nesting" do
schema = described_class.schema do
check { context.has_key?(:b) && context.has_key?(:c) && context.has_key?(:d) && !context.has_key?(:e) }.
with_context(b: 1).
with_context(c: 2).
with_context(d: 3)
end

expect(schema.with_context(a: 1).(1).to_dry_result).to eq Success(1)
end

it "works with complex schemas" do
schema = described_class.schema do
hash_schema(
a: check { context.has_key?(:a) && context.has_key?(:d) && context.has_key?(:e) && !context.has_key?(:f) },
b: {c: {d: check { context.has_key?(:a) && context.has_key?(:d) && context.has_key?(:e) && !context.has_key?(:t) }}},
e: [check { |v| context.has_key?(:a) && context.has_key?(:d) && context.has_key?(:e) && !context.has_key?(:p) }]
).with_context(a: 1)
end

expect(schema.with_context(d: 2, e: 3).({a: 1, b: {c: {d: 10}}, e: [3]}).to_dry_result).to eq Success({:a=>1, :b=>{:c=>{:d=>10}}, :e=>[3]})
end
end
end
Loading