Class: EvilSeed::RelationDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/evil_seed/relation_dumper.rb

Overview

This class performs actual dump generation for single relation and all its not yet loaded dependencies

- Fetches all tuples for root (it does not instantiate AR records but it casts values to Ruby types)
- Extracts foreign key values for all belongs_to associations
- Dumps belongs_to associations(recursion!)
- Dumps all tuples for root, writes them in file
- Dumps all other associations (recursion!)
- Returns all results to caller in correct order

TODO: This class obviously breaks SRP principle and thus should be split!

Constant Summary collapse

MAX_IDENTIFIERS_IN_IN_STMT =
1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, root_dumper, association_path, **options) ⇒ RelationDumper

Returns a new instance of RelationDumper.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/evil_seed/relation_dumper.rb', line 33

def initialize(relation, root_dumper, association_path, **options)
  puts("- #{association_path}") if root_dumper.configuration.verbose

  @relation               = relation
  @root_dumper            = root_dumper
  @verbose                = configuration.verbose
  @identifiers            = options[:identifiers]
  @local_load_map         = Hash.new { |h, k| h[k] = [] }
  @foreign_keys           = Hash.new { |h, k| h[k] = [] }
  @loaded_ids             = []
  @model_class            = relation.klass
  @search_key             = options[:search_key] || model_class.primary_key
  @association_path       = association_path
  @inverse_reflection     = options[:inverse_of]
  @records                = []
  @record_dumper          = configuration.record_dumper_class.new(model_class, configuration, self)
  @nullify_columns        = []
  @table_names            = {}
  @belongs_to_reflections = setup_belongs_to_reflections
  @has_many_reflections   = setup_has_many_reflections
  @options                = options
  @current_deep           = association_path.split('.').size
  @dont_nullify           = dont_nullify
  @custom_scope           = options[:custom_scope]
end

Instance Attribute Details

#association_pathObject (readonly)

Returns the value of attribute association_path.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def association_path
  @association_path
end

#belongs_to_reflectionsObject (readonly)

Returns the value of attribute belongs_to_reflections.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def belongs_to_reflections
  @belongs_to_reflections
end

#current_deepObject (readonly)

Returns the value of attribute current_deep.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def current_deep
  @current_deep
end

#custom_scopeObject (readonly)

Returns the value of attribute custom_scope.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def custom_scope
  @custom_scope
end

#foreign_keysObject (readonly)

Returns the value of attribute foreign_keys.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def foreign_keys
  @foreign_keys
end

#has_many_reflectionsObject (readonly)

Returns the value of attribute has_many_reflections.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def has_many_reflections
  @has_many_reflections
end

#identifiersObject (readonly)

Returns the value of attribute identifiers.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def identifiers
  @identifiers
end

#inverse_reflectionObject (readonly)

Returns the value of attribute inverse_reflection.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def inverse_reflection
  @inverse_reflection
end

#loaded_idsObject (readonly)

Returns the value of attribute loaded_ids.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def loaded_ids
  @loaded_ids
end

#local_load_mapObject (readonly)

Returns the value of attribute local_load_map.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def local_load_map
  @local_load_map
end

#model_classObject (readonly)

Returns the value of attribute model_class.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def model_class
  @model_class
end

#nullify_columnsObject (readonly)

Returns the value of attribute nullify_columns.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def nullify_columns
  @nullify_columns
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def options
  @options
end

#record_dumperObject (readonly)

Returns the value of attribute record_dumper.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def record_dumper
  @record_dumper
end

#recordsObject (readonly)

Returns the value of attribute records.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def records
  @records
end

#relationObject (readonly)

Returns the value of attribute relation.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def relation
  @relation
end

#root_dumperObject (readonly)

Returns the value of attribute root_dumper.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def root_dumper
  @root_dumper
end

#search_keyObject (readonly)

Returns the value of attribute search_key.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def search_key
  @search_key
end

#table_namesObject (readonly)

Returns the value of attribute table_names.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def table_names
  @table_names
end

#verboseObject (readonly)

Returns the value of attribute verbose.



26
27
28
# File 'lib/evil_seed/relation_dumper.rb', line 26

def verbose
  @verbose
end

Instance Method Details

#callArray<IO>

Generate dump and write it into io

Returns:

  • (Array<IO>)

    List of dump IOs for separate tables in order of dependencies (belongs_to are first)



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/evil_seed/relation_dumper.rb', line 61

def call
  dump!
  if deep_limit and current_deep > deep_limit
    [dump_records!].flatten.compact
  else
    [
      dump_belongs_to_associations!,
      dump_records!,
      dump_has_many_associations!,
    ].flatten.compact
  end
end