Sha256: b8e58878370f00cc65848ca37db8ff2e8829a2d0322bb1247f484b5f147569a4

Contents?: true

Size: 999 Bytes

Versions: 2

Compression:

Stored size: 999 Bytes

Contents

module Dry::Initializer
  #
  # @private
  #
  # Dispatchers allow adding syntax sugar to `.param` and `.option` methods.
  #
  # Every dispatcher should convert the source hash of options into
  # the resulting hash so that you can send additional keys to the helpers.
  #
  # @example Add special dispatcher
  #
  #   # Define a dispatcher for key :integer
  #   dispatcher = proc do |opts|
  #     opts.merge(type: proc(&:to_i)) if opts[: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
  #
  module Dispatchers
    class << self
      def <<(item)
        list << item
        self
      end

      def [](options)
        list.inject(options) { |opts, item| item.call(opts) }
      end

      private

      def list
        @list ||= []
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-2.5.0 lib/dry/initializer/dispatchers.rb
dry-initializer-2.4.0 lib/dry/initializer/dispatchers.rb