Sha256: d3463cda912282c2da1849e90a81ec2e05c9666b3da4ea5dc30a8bd261ab2fac

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

---
title: Skip Undefined
layout: gem-single
name: dry-initializer
---

The initializer uses special constant `Dry::Initializer::UNDEFINED` to distinguish variables that are set to `nil` from those that are not set at all.

When no value was provided, the constant is assigned to a variable, but hidden in a reader.

```ruby
require 'dry-initializer'

class User
  extend Dry::Initializer
  option :email, optional: true
end

user = User.new

user.email
# => nil

user.instance_variable_get :@email
# => Dry::Initializer::UNDEFINED
```

This gives you full control of the real state of the attributes. However, all that checks cost about >30% of instantiation time, and make attribute readers 2 times slower.

To avoid the overhead in cases you don't care about the differences between `nil` and undefined, you can use a light version of the module. Add `[undefined: false]` config to either `extend` or `include` line of code:

```ruby
extend Dry::Initializer[undefined: false]
```

```ruby
include Dry::Initializer[undefined: false] -> do
  # ...
end
```

This time you should expect `nil` every time no value was given to an optional attribute:

```ruby
require 'dry-initializer'

class User
  extend Dry::Initializer[undefined: false]
  option :email, optional: true
end

user = User.new

user.email
# => nil

user.instance_variable_get :@email
# => nil
```

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-3.0.3 docsite/source/skip-undefined.html.md
dry-initializer-3.0.2 docsite/source/skip-undefined.html.md