Class: BulkLoader::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_loader/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(mapping, default: nil, &block) ⇒ Loader

mapping is a Symbol or Proc. block’s 1st argument mapped using mapping. and your block’s return value’s key should be mapped value.



7
8
9
10
11
12
13
# File 'lib/bulk_loader/loader.rb', line 7

def initialize(mapping, default: nil, &block)
  @mapping = mapping
  @is_mapping_proc = @mapping.is_a?(Proc)
  @default = default
  @is_default_proc = @default.is_a?(Proc)
  @block = block
end

Instance Method Details

#load(lazy_objs, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bulk_loader/loader.rb', line 15

def load(lazy_objs, *args)
  return if lazy_objs.empty?

  mapping_of = get_mapping(lazy_objs)

  result_of = call_block(mapping_of, *args)

  lazy_objs.each(&:clear)

  set_result_to_lazy_objs(result_of, mapping_of)

  fill_default_to_unloaded_obj(lazy_objs)
end