Class: DumpedRailers::RecordBuilder::FixtureTable

Inherits:
Object
  • Object
show all
Defined in:
lib/dumped_railers/record_builder/fixture_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_records) ⇒ FixtureTable



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 11

def initialize(raw_records)
  config = raw_records.delete('_fixture')
  
  @model = identify_model!(config)
  @model_name = model.name.to_sym
  @dependency_tracker = DependencyTracker.for(model)
  
  @rows = raw_records.map { |label, attrs|
    build_fixture_row(label, attrs)
  }
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



9
10
11
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 9

def dependencies
  @dependencies
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 9

def model
  @model
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



9
10
11
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 9

def model_name
  @model_name
end

#objectsObject (readonly)

Returns the value of attribute objects.



9
10
11
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 9

def objects
  @objects
end

#rowsObject (readonly)

Returns the value of attribute rows.



9
10
11
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 9

def rows
  @rows
end

Instance Method Details

#analyze_metadata_dependencies!Object

Raises:

  • (RuntimeError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 23

def 
  raise RuntimeError, "Dependency Analysis has already been done with the fixture for #{model_name}" if @dependencies

  rows.map { |row| row.analyze_dependencies!(@dependency_tracker.on(row)) }

  @dependencies = model.reflect_on_all_associations.select(&:belongs_to?).flat_map { |rel|
    if rel.polymorphic?
      @dependency_tracker.list_all_model_names_with(rel.name)
    else
      rel.class_name.to_sym
    end
  }.uniq
end

#build_records!Object

Raises:

  • (RuntimeError)


37
38
39
40
41
42
43
44
# File 'lib/dumped_railers/record_builder/fixture_table.rb', line 37

def build_records!
  raise RuntimeError, "The records in this fixture for #{model_name} have been built already" if @instantiated
  
  @objects = rows.map { |row| row.instantiate_as!(model) }
  @instantiated = true
  
  objects
end