Class: CounterCulture::Reconciler

Inherits:
Object
  • Object
show all
Defined in:
lib/counter_culture/reconciler.rb

Defined Under Namespace

Classes: Reconciliation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counter, options = {}) ⇒ Reconciler

Returns a new instance of Reconciler.



11
12
13
14
15
16
# File 'lib/counter_culture/reconciler.rb', line 11

def initialize(counter, options={})
  @counter, @options = counter, options

  @changes = []
  @reconciled = false
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



6
7
8
# File 'lib/counter_culture/reconciler.rb', line 6

def changes
  @changes
end

#counterObject (readonly)

Returns the value of attribute counter.



6
7
8
# File 'lib/counter_culture/reconciler.rb', line 6

def counter
  @counter
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/counter_culture/reconciler.rb', line 6

def options
  @options
end

Instance Method Details

#reconcile!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/counter_culture/reconciler.rb', line 18

def reconcile!
  return false if @reconciled

  if options[:skip_unsupported]
    return false if (foreign_key_values || (counter_cache_name.is_a?(Proc) && !column_names) || delta_magnitude.is_a?(Proc))
  else
    raise "Fixing counter caches is not supported when using :foreign_key_values; you may skip this relation with :skip_unsupported => true" if foreign_key_values
    raise "Must provide :column_names option for relation #{relation.inspect} when :column_name is a Proc; you may skip this relation with :skip_unsupported => true" if counter_cache_name.is_a?(Proc) && !column_names
    raise "Fixing counter caches is not supported when :delta_magnitude is a Proc; you may skip this relation with :skip_unsupported => true" if delta_magnitude.is_a?(Proc)
  end

  associated_model_classes.each do |associated_model_class|
    Reconciliation.new(counter, changes, options, associated_model_class).perform
  end

  @reconciled = true
end