Class: AbstractImporter::Strategies::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_importer/strategies/base.rb

Direct Known Subclasses

DefaultStrategy, InsertStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Base

Returns a new instance of Base.



19
20
21
22
# File 'lib/abstract_importer/strategies/base.rb', line 19

def initialize(collection, options={})
  @collection = collection
  @remap_ids = options.fetch(:id_map, use_id_map_for?(collection))
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/abstract_importer/strategies/base.rb', line 4

def collection
  @collection
end

Instance Method Details

#already_imported?(hash) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/abstract_importer/strategies/base.rb', line 32

def already_imported?(hash)
  id_map.contains? collection.table_name, hash[:id]
end

#flushObject



36
37
# File 'lib/abstract_importer/strategies/base.rb', line 36

def flush
end

#prepare_attributes(hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/abstract_importer/strategies/base.rb', line 39

def prepare_attributes(hash)
  hash = invoke_callback(:before_build, hash) || hash

  if remap_ids?
    hash = hash.merge(legacy_id: hash.delete(:id))
    if generate_id
      hash[:id] = generate_id.arity.zero? ? generate_id.call : generate_id.call(hash)
    end
  end

  hash.merge(association_attrs)
end

#process_record(hash) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/abstract_importer/strategies/base.rb', line 28

def process_record(hash)
  raise NotImplementedError
end

#remap_ids?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/abstract_importer/strategies/base.rb', line 24

def remap_ids?
  @remap_ids
end