Skip to content

Commit 0638f92

Browse files
authored
Merge pull request #194 from dry-rb/do-not-raise-on-optional-keys
Update `Struct#[]` to return nil for unset optional attributes
2 parents eb069ca + c33210c commit 0638f92

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

changelog.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
- '3.1 is now the minimum Ruby version (@flash-gordon)'
1212
- '`Dry::Struct::Error` is now a subclass of `Dry::Types::CoercionError` (in #193)
1313
(@flash-gordon)'
14+
- '`Dry::Struct#[]` now returns `nil` if an optional attribute is not set.
15+
This is consistent with calling accessor methods for optional attributes.
16+
(issue #171 via #194) (@ivleonov + @flash-gordon)'
1417
- version: 1.6.0
1518
date: 2022-11-04
1619
changed:

lib/dry/struct.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def initialize(attributes)
146146
# rom_n_roda[:subtitle] #=> nil
147147
def [](name)
148148
@attributes.fetch(name) do
149-
raise MissingAttributeError.new(attribute: name, klass: self.class)
149+
if self.class.attribute_names.include?(name)
150+
nil
151+
else
152+
raise MissingAttributeError.new(attribute: name, klass: self.class)
153+
end
150154
end
151155
end
152156

spec/integration/struct_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,19 @@ class Task < Dry::Struct
356356
.with_message("Missing attribute: :name on Test::Task")
357357
end
358358

359+
describe "optional attributes" do
360+
before do
361+
class Test::Task
362+
attribute :name?, "string"
363+
end
364+
end
365+
366+
it "returns nil if the attribute is not set" do
367+
value = Test::Task[user: "Jane"]
368+
expect(value[:name]).to be_nil
369+
end
370+
end
371+
359372
describe "protected methods" do
360373
before do
361374
class Test::Task

0 commit comments

Comments
 (0)