Class: ReeMapper::MapperFactoryProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper_factory, register_as:, &blk) ⇒ MapperFactoryProxy

Returns a new instance of MapperFactoryProxy.



13
14
15
16
17
18
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 13

def initialize(mapper_factory, register_as:, &blk)
  @mapper_factory = mapper_factory
  @register_as    = register_as
  @strategies     = []
  @after_build    = blk
end

Instance Attribute Details

#after_buildObject (readonly)

Returns the value of attribute after_build.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 4

def after_build
  @after_build
end

#mapper_factoryObject (readonly)

Returns the value of attribute mapper_factory.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 4

def mapper_factory
  @mapper_factory
end

#register_asObject (readonly)

Returns the value of attribute register_as.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 4

def register_as
  @register_as
end

#strategiesObject (readonly)

Returns the value of attribute strategies.



4
5
6
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 4

def strategies
  @strategies
end

Instance Method Details

#use(strategy_or_method, dto: nil, &blk) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb', line 21

def use(strategy_or_method, dto: nil, &blk)
  if strategy_or_method.is_a?(ReeMapper::MapperStrategy)
    strategy = strategy_or_method
  else
    strategy = mapper_factory.find_strategy(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)

  if register_as
    mapper_factory.register_mapper(register_as, mapper)
    mapper = mapper_factory.types.fetch(register_as).last
  end

  mapper_factory.new(mapper).instance_exec(&blk)
  mapper.prepare_dto

  after_build&.call(mapper)

  mapper
end