Class: Planter::RecordAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/planter/record_attributes.rb

Overview

Prepares seed records for adapter persistence.

Instance Method Summary collapse

Constructor Details

#initialize(context:, adapter:, transformations_provider:) ⇒ RecordAttributes

Create a record attribute preparer.

Parameters:

  • context (Planter::SeedContext)

    seeder configuration

  • adapter (Object)

    configured persistence adapter

  • transformations_provider (#call)

    returns value transformations by field



16
17
18
19
20
21
# File 'lib/planter/record_attributes.rb', line 16

def initialize(context:, adapter:, transformations_provider:)
  @context = context
  @adapter = adapter
  @transformations_provider = transformations_provider
  @warned_non_column_lookup_attributes = []
end

Instance Method Details

#prepare(record, parent_id: nil) ⇒ Array<Hash, Hash>

Prepare lookup and create attributes for adapter persistence.

Parameters:

  • record (Hash)

    seed record attributes

  • parent_id (Object, nil) (defaults to: nil)

    parent record id

Returns:

  • (Array<Hash, Hash>)


31
32
33
34
35
36
# File 'lib/planter/record_attributes.rb', line 31

def prepare(record, parent_id: nil)
  lookup_attributes, create_attributes = split_record(apply_transformations(record))
  lookup_attributes = lookup_attributes.merge(foreign_key => parent_id) if parent_id

  filter_lookup_attributes(lookup_attributes, create_attributes)
end