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.



694
695
696
697
698
699
700
701
702
703
# File 'lib/view_model/active_record/update_operation.rb', line 694

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.



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

def blame_reference
  @blame_reference
end

#membersObject (readonly)

Returns the value of attribute members.



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

def members
  @members
end

#orphaned_membersObject (readonly)

Returns the value of attribute orphaned_members.



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

def orphaned_members
  @orphaned_members
end

Instance Method Details

#concat(references) ⇒ Object



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

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



718
719
720
# File 'lib/view_model/active_record/update_operation.rb', line 718

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

#insert_before(relative_to, references) ⇒ Object



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

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

#remove(vm_references) ⇒ Object



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

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



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

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



736
737
738
# File 'lib/view_model/active_record/update_operation.rb', line 736

def update(references)
  claim_existing_references(references)
end