Class: DirtySeed::Sorter

Inherits:
Object
  • Object
show all
Defined in:
lib/dirty_seed/sorter.rb

Overview

Sorts ActiveRecord models depending on their associations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models = []) ⇒ DirtySeed::Sorter

Initializes an instance

Parameters:

  • models (Array<Class>) (defaults to: [])

    models inheriting from ActiveRecord::Base



12
13
14
15
16
17
# File 'lib/dirty_seed/sorter.rb', line 12

def initialize(models = [])
  @models = models
  @sorted = []
  @checked = []
  @current = unsorted.first
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



6
7
8
# File 'lib/dirty_seed/sorter.rb', line 6

def checked
  @checked
end

#currentObject (readonly)

Returns the value of attribute current.



6
7
8
# File 'lib/dirty_seed/sorter.rb', line 6

def current
  @current
end

#modelsObject (readonly) Also known as: unsorted

Returns the value of attribute models.



6
7
8
# File 'lib/dirty_seed/sorter.rb', line 6

def models
  @models
end

#skip_optionalObject (readonly)

Returns the value of attribute skip_optional.



6
7
8
# File 'lib/dirty_seed/sorter.rb', line 6

def skip_optional
  @skip_optional
end

#sortedObject (readonly)

Returns the value of attribute sorted.



6
7
8
# File 'lib/dirty_seed/sorter.rb', line 6

def sorted
  @sorted
end

Instance Method Details

#sort!Array<Class>

Sorts models depending on their associations

Returns:

  • (Array<Class>)

    classes inheriting from ActiveRecord::Base



21
22
23
24
25
26
27
28
29
# File 'lib/dirty_seed/sorter.rb', line 21

def sort!
  return sorted if unsorted.empty?

  set_current
  # active skip_optional option to prevent infinite loop
  skip_optional! if break_loop?
  insert_or_rotate
  sort!
end