Class: ColumnSync::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/column_sync/service.rb

Constant Summary collapse

FUNCTION_PREFIX =
"column_sync"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ Service

Returns a new instance of Service.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/column_sync/service.rb', line 13

def initialize(columns)
  raise ArgumentError, "columns must be a hash" unless columns.is_a?(Hash)
  raise ArgumentError, "two tables and columns must be provided" unless columns.size == 2

  referenced_model, referencer_model = columns.keys.permutation(2).find do |(referenced, referencer)|
    referenced.reflect_on_all_associations(:has_one)
              .find { |association| association.klass == referencer }
              .tap { |association| @referencer_fk_column_name = association.foreign_key if association }
  end

  raise ArgumentError, "no valid column relation found" unless referenced_model && referencer_model

  @referenced_column_name = columns[referenced_model].to_s
  @referencer_column_name = columns[referencer_model].to_s

  unless referenced_model.column_names.include?(referenced_column_name)
    raise ArgumentError, "referenced column #{referenced_column_name} does not exist"
  end

  unless referencer_model.column_names.include?(referencer_column_name)
    raise ArgumentError, "referencer column #{referencer_column_name} does not exist"
  end

  @referenced_table_name = referenced_model.table_name
  @referenced_primary_key = referenced_model.primary_key
  @referencer_table_name = referencer_model.table_name
end

Instance Attribute Details

#referenced_column_nameObject (readonly)

Returns the value of attribute referenced_column_name.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referenced_column_name
  @referenced_column_name
end

#referenced_primary_keyObject (readonly)

Returns the value of attribute referenced_primary_key.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referenced_primary_key
  @referenced_primary_key
end

#referenced_table_nameObject (readonly)

Returns the value of attribute referenced_table_name.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referenced_table_name
  @referenced_table_name
end

#referencer_column_nameObject (readonly)

Returns the value of attribute referencer_column_name.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referencer_column_name
  @referencer_column_name
end

#referencer_fk_column_nameObject (readonly)

Returns the value of attribute referencer_fk_column_name.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referencer_fk_column_name
  @referencer_fk_column_name
end

#referencer_table_nameObject (readonly)

Returns the value of attribute referencer_table_name.



6
7
8
# File 'lib/column_sync/service.rb', line 6

def referencer_table_name
  @referencer_table_name
end

Instance Method Details

#functionsObject



41
42
43
44
45
46
# File 'lib/column_sync/service.rb', line 41

def functions
  {
    referencer_sync_name => referencer_update_function,
    referenced_sync_name => referenced_update_function
  }
end

#triggersObject



48
49
50
51
52
53
# File 'lib/column_sync/service.rb', line 48

def triggers
  {
    referencer_sync_name => { definition: referencer_trigger, table_name: referenced_table_name },
    referenced_sync_name => { definition: referenced_trigger, table_name: referencer_table_name }
  }
end