Class: ActiveSchema::Feeder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_schema/feeder.rb

Direct Known Subclasses

InAdvanceFeeder, OnTheFlyFeeder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = ActiveSchema.configuration) ⇒ Feeder

Returns a new instance of Feeder.



4
5
6
7
8
9
# File 'lib/active_schema/feeder.rb', line 4

def initialize(configuration = ActiveSchema.configuration)
  @table_hub = TableHub.new
  @associations_generator = Associations::Generator
  @validations_generator  = Validations::Generator
  @configuration = configuration
end

Instance Attribute Details

#associations_generatorObject (readonly)

Returns the value of attribute associations_generator.



3
4
5
# File 'lib/active_schema/feeder.rb', line 3

def associations_generator
  @associations_generator
end

#table_hubObject (readonly)

Returns the value of attribute table_hub.



3
4
5
# File 'lib/active_schema/feeder.rb', line 3

def table_hub
  @table_hub
end

#validations_generatorObject (readonly)

Returns the value of attribute validations_generator.



3
4
5
# File 'lib/active_schema/feeder.rb', line 3

def validations_generator
  @validations_generator
end

Instance Method Details

#add_model(model) ⇒ Object



11
12
13
# File 'lib/active_schema/feeder.rb', line 11

def add_model(model)
  table_hub.add_model(model)
end

#dispatch_attachments(model) ⇒ Object



15
16
17
18
19
# File 'lib/active_schema/feeder.rb', line 15

def dispatch_attachments(model)
  table = table_hub.tables[model.table_name]
  generate_validations(table)
  generate_assocations(table)
end

#generate_assocations(table) ⇒ Object



21
22
23
24
25
# File 'lib/active_schema/feeder.rb', line 21

def generate_assocations(table)
  table_hub.relations[table.model.table_name].select(&:model).each do |linked_table|
    generate_associations_between(table, linked_table)
  end
end

#generate_associations_between(table1, table2) ⇒ Object



27
28
29
30
# File 'lib/active_schema/feeder.rb', line 27

def generate_associations_between(table1, table2)
  generate_directed_associations_between(table1, table2)
  generate_directed_associations_between(table2, table1)
end

#generate_directed_associations_between(table1, table2) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/active_schema/feeder.rb', line 36

def generate_directed_associations_between(table1, table2)
  table1.foreign_keys.each do |column, ref_table|
    if ref_table == table2
      associations_generator.new(table1, ref_table, column).generate
    end
  end
end

#generate_validations(table) ⇒ Object



32
33
34
# File 'lib/active_schema/feeder.rb', line 32

def generate_validations(table)
  validations_generator.new(table, @configuration.skip_validation_for_column).generate
end