Class: Ripple::Conflict::Resolver

Inherits:
Object
  • Object
show all
Includes:
Translation
Defined in:
lib/ripple/conflict/resolver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Translation

#i18n_scope

Constructor Details

#initialize(robject, model_class) ⇒ Resolver

Returns a new instance of Resolver.



25
26
27
28
# File 'lib/ripple/conflict/resolver.rb', line 25

def initialize(robject, model_class)
  @robject = robject
  @model_class = model_class
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/ripple/conflict/resolver.rb', line 8

def document
  @document
end

#model_classObject (readonly)

Returns the value of attribute model_class.



8
9
10
# File 'lib/ripple/conflict/resolver.rb', line 8

def model_class
  @model_class
end

Class Method Details

.to_procObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/ripple/conflict/resolver.rb', line 12

def self.to_proc
  @to_proc ||= lambda do |robject|
    possible_model_classes = robject.siblings.map { |s| s.data && s.data['_type'] }.compact.uniq
    return nil unless possible_model_classes.size == 1

    resolver = new(robject, possible_model_classes.first.constantize)
    resolver.resolve
    resolver.document.robject
  end
end

Instance Method Details

#resolveObject



30
31
32
33
34
35
36
# File 'lib/ripple/conflict/resolver.rb', line 30

def resolve
  assert_conflict_block
  basic_resolver.perform
  assert_no_unexpected_conflicts
  document.instance_exec(siblings, basic_resolver.remaining_conflicts, &on_conflict_block)
  document.update_robject
end

#siblingsObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ripple/conflict/resolver.rb', line 38

def siblings
  @siblings ||= @robject.siblings.map do |s|
    @model_class.send(:instantiate, s).tap do |record|
      # TODO: make the deleted conditional explicit by putting logic in
      #       RObject to know it has loaded a deleted sibling.
      #       Here we assume it is deleted if the data is nil because
      #       that's the only way we know of that the data can be nil.
      record.instance_variable_set(:@deleted, true) if s.data.nil?
    end
  end
end