Module: Dry::Initializer::Dispatchers

Extended by:
Dispatchers
Included in:
Dispatchers
Defined in:
lib/dry/initializer/dispatchers.rb

Overview

The module is responsible for __normalizing__ arguments of ‘.param` and `.option`.

What the module does is convert the source list of arguments into the standard set of options:

  • ‘:option` – whether an argument is an option (or param)

  • ‘:source` – the name of source option

  • ‘:target` – the target name of the reader

  • ‘:reader` – if the reader’s privacy (:public, :protected, :private, nil)

  • ‘:ivar` – the target nane of the variable

  • ‘:type` – the callable coercer of the source value

  • ‘:optional` – if the argument is optional

  • ‘:default` – the proc returning the default value of the source value

  • ‘:null` – the value to be set to unassigned optional argument

It is this set is used to build [Dry::Initializer::Definition].

# Settings

The module uses global setting ‘null` to define what value should be set to variables that kept unassigned. By default it uses `Dry::Initializer::UNDEFINED`

# Syntax Extensions

The module supports syntax extensions. You can add any number of custom dispatchers __on top__ of the stack of default dispatchers. Every dispatcher should be a callable object that takes the source set of options and converts it to another set of options.

Examples:

# from `option :foo, [], as: :bar, optional: :true
input = { name: :foo, as: :bar, type: [], optional: true }

Dry::Initializer::Dispatcher.call(input)
# => {
#      source:   "foo",
#      target:   "bar",
#      reader:   :public,
#      ivar:     "@bar",
#      type:  ->(v) { Array(v) } }, # simplified for brevity
#      optional: true,
#      default:  -> { Dry::Initializer::UNDEFINED },
#    }

Add special dispatcher


# Define a dispatcher for key :integer
dispatcher = proc do |integer: false, **opts|
  opts.merge(type: proc(&:to_i)) if integer
end

# Register a dispatcher
Dry::Initializer::Dispatchers << dispatcher

# Now you can use option `integer: true` instead of `type: proc(&:to_i)`
class Foo
  extend Dry::Initializer
  param :id, integer: true
end

Defined Under Namespace

Modules: BuildNestedType, CheckType, PrepareDefault, PrepareIvar, PrepareOptional, PrepareReader, PrepareSource, PrepareTarget, UnwrapType, WrapType

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nullObject

Returns:

  • (Object)


68
69
70
# File 'lib/dry/initializer/dispatchers.rb', line 68

def null
  @null
end

#null Defines a value to be set to unassigned attributes(Definesavaluetobesettounassignedattributes) ⇒ Object

Returns:

  • (Object)


68
# File 'lib/dry/initializer/dispatchers.rb', line 68

attr_accessor :null

Instance Method Details

#<<(dispatcher) ⇒ self

Registers a new dispatcher

Parameters:

  • dispatcher (#call)

Returns:

  • (self)

    itself



76
77
78
79
# File 'lib/dry/initializer/dispatchers.rb', line 76

def <<(dispatcher)
  @pipeline = [dispatcher] + pipeline
  self
end

#call(**options) ⇒ Hash<Symbol, Objct>

Normalizes the source set of options

Parameters:

  • options (Hash<Symbol, Object>)

Returns:

  • (Hash<Symbol, Objct>)

    normalized set of options



87
88
89
90
# File 'lib/dry/initializer/dispatchers.rb', line 87

def call(**options)
  options = { null: null, **options }
  pipeline.reduce(options) { |opts, dispatcher| dispatcher.call(**opts) }
end