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.



672
673
674
675
676
677
678
679
680
681
# File 'lib/view_model/active_record/update_operation.rb', line 672

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.



670
671
672
# File 'lib/view_model/active_record/update_operation.rb', line 670

def blame_reference
  @blame_reference
end

#membersObject (readonly)

Returns the value of attribute members.



670
671
672
# File 'lib/view_model/active_record/update_operation.rb', line 670

def members
  @members
end

#orphaned_membersObject (readonly)

Returns the value of attribute orphaned_members.



670
671
672
# File 'lib/view_model/active_record/update_operation.rb', line 670

def orphaned_members
  @orphaned_members
end

Instance Method Details

#concat(references) ⇒ Object



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

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



696
697
698
# File 'lib/view_model/active_record/update_operation.rb', line 696

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

#insert_before(relative_to, references) ⇒ Object



692
693
694
# File 'lib/view_model/active_record/update_operation.rb', line 692

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

#remove(vm_references) ⇒ Object



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

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



683
684
685
686
687
688
689
690
# File 'lib/view_model/active_record/update_operation.rb', line 683

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



714
715
716
# File 'lib/view_model/active_record/update_operation.rb', line 714

def update(references)
  claim_existing_references(references)
end