Sha256: d4e3d0d16027a073f0c96af11aa3e51e15a2cb26f99a72af12d077eae60ba377

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

class ReeMapper::MapperFactoryProxy
  attr_reader :mapper_factory, :register_as, :strategies, :after_build

  contract(
    Class, # SubclassOf[ReeMapper::MapperFactory],
    Kwargs[
      register_as: Nilor[Symbol]
    ],
    Optblock => Any
  )
  def initialize(mapper_factory, register_as:, &blk)
    @mapper_factory = mapper_factory
    @register_as    = register_as
    @strategies     = []
    @after_build    = blk
  end

  contract(Or[Symbol, ReeMapper::MapperStrategy], Kwargs[dto: Nilor[Class]], Optblock => Or[ReeMapper::MapperFactoryProxy, ReeMapper::Mapper]).throws(ArgumentError)
  def use(strategy_or_method, dto: nil, &blk)
    if strategy_or_method.is_a?(ReeMapper::MapperStrategy)
      strategy = strategy_or_method
    else
      strategy = mapper_factory.strategies.detect { _1.method == strategy_or_method }
      raise ArgumentError, "MapperFactory strategy :#{strategy_or_method} not found" unless strategy
      strategy = strategy.dup
      strategy.dto = dto if dto
    end

    self.strategies << strategy

    return self unless blk

    mapper = ReeMapper::Mapper.build(strategies)
    mapper_factory.new(mapper).instance_exec(&blk)
    mapper.prepare_dto

    mapper_factory.register(register_as, mapper) if register_as

    after_build&.call(mapper)

    mapper
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ree_lib-1.0.35 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb
ree_lib-1.0.34 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb
ree_lib-1.0.33 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb
ree_lib-1.0.32 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb
ree_lib-1.0.31 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb