dry-initializer

v3.2
  1. Introduction & Usage
  2. Container Version
  3. Params and Options
  4. Tolerance to Unknown Arguments
  5. Optional Attributes and Default Values
  6. Type Constraints
  7. Readers
  8. Inheritance
  9. Skip Undefined
  10. Attributes
  11. Rails Support

TOC


Skip Undefined

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.

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:

extend Dry::Initializer[undefined: false]
include Dry::Initializer[undefined: false].define -> do
  # ...
end

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

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