Class: DumpedRailers::RecordBuilder::FixtureRow

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

Defined Under Namespace

Classes: RecordStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, attrs) ⇒ FixtureRow

Returns a new instance of FixtureRow.



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

def initialize(label, attrs)
  @label = label
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



6
7
8
# File 'lib/dumped_railers/record_builder/fixture_row.rb', line 6

def attrs
  @attrs
end

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'lib/dumped_railers/record_builder/fixture_row.rb', line 6

def label
  @label
end

Instance Method Details

#analyze_dependencies!(dependency_tracker) ⇒ Object

Raises:

  • (RuntimeError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dumped_railers/record_builder/fixture_row.rb', line 13

def analyze_dependencies!(dependency_tracker)
  raise RuntimeError, 'Can\'t execute the dependency analysis twice. This has been done already' if @dependency

  @dependency =  dependency_tracker

  attrs.each do |attr, value|
    ref, model_name = parse_reference_from(value)
    next unless ref
  
    attrs[attr] = ref.to_sym
    @dependency.with(attr).record_label = ref.to_sym
    @dependency.with(attr).model_name   = model_name&.to_sym
  end
end

#instantiate_as!(model) ⇒ Object

Raises:

  • (RuntimeError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/dumped_railers/record_builder/fixture_row.rb', line 28

def instantiate_as!(model)
  raise RuntimeError, 'Could not find the dependency tracker. Run #analyze_dependencies to instaitiate the records' unless @dependency

  @model = model
  resolve_reference!
  object = model.new(attrs)
  RecordStore.register(label, object: object)
  
  object
end