Class: ViewModel::ActiveRecord::UpdateOperation::MutableReferencedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/active_record/update_operation.rb

Overview

Helper class to wrap the previous members of a referenced collection and provide update operations. No one member may be affected by more than one update operation. Elements removed from the collection are collected as ‘orphaned_members`.“

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_data, update_context, members, blame_reference) ⇒ MutableReferencedCollection

Returns a new instance of MutableReferencedCollection.



703
704
705
706
707
708
709
710
711
712
# File 'lib/view_model/active_record/update_operation.rb', line 703

def initialize(association_data, update_context, members, blame_reference)
  @association_data = association_data
  @update_context   = update_context
  @members          = members.dup
  @blame_reference  = blame_reference

  @orphaned_members = []

  @free_members_by_indirect_ref = @members.index_by(&:indirect_viewmodel_reference)
end

Instance Attribute Details

#blame_referenceObject (readonly)

Returns the value of attribute blame_reference.



701
702
703
# File 'lib/view_model/active_record/update_operation.rb', line 701

def blame_reference
  @blame_reference
end

#membersObject (readonly)

Returns the value of attribute members.



701
702
703
# File 'lib/view_model/active_record/update_operation.rb', line 701

def members
  @members
end

#orphaned_membersObject (readonly)

Returns the value of attribute orphaned_members.



701
702
703
# File 'lib/view_model/active_record/update_operation.rb', line 701

def orphaned_members
  @orphaned_members
end

Instance Method Details

#concat(references) ⇒ Object



731
732
733
734
735
# File 'lib/view_model/active_record/update_operation.rb', line 731

def concat(references)
  new_members = claim_or_create_references(references)
  remove_from_members(new_members)
  members.concat(new_members)
end

#insert_after(relative_to, references) ⇒ Object



727
728
729
# File 'lib/view_model/active_record/update_operation.rb', line 727

def insert_after(relative_to, references)
  insert_relative(relative_to, 1, references)
end

#insert_before(relative_to, references) ⇒ Object



723
724
725
# File 'lib/view_model/active_record/update_operation.rb', line 723

def insert_before(relative_to, references)
  insert_relative(relative_to, 0, references)
end

#remove(vm_references) ⇒ Object



737
738
739
740
741
742
743
# File 'lib/view_model/active_record/update_operation.rb', line 737

def remove(vm_references)
  removed_members = vm_references.map do |vm_ref|
    claim_existing_member(vm_ref)
  end
  remove_from_members(removed_members)
  orphaned_members.concat(removed_members)
end

#replace(references) ⇒ Object



714
715
716
717
718
719
720
721
# File 'lib/view_model/active_record/update_operation.rb', line 714

def replace(references)
  members.replace(claim_or_create_references(references))

  # Any unclaimed free members after building the update target are now
  # orphaned and their direct viewmodels can be released.
  orphaned_members.concat(free_members_by_indirect_ref.values)
  free_members_by_indirect_ref.clear
end

#update(references) ⇒ Object



745
746
747
# File 'lib/view_model/active_record/update_operation.rb', line 745

def update(references)
  claim_existing_references(references)
end