Class: ViewModel::ActiveRecord::AbstractCollectionUpdate::Functional

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

Overview

Wraps an ordered list of FunctionalUpdates, each of whose ‘contents` are either UpdateData for nested associations or references for referenced associations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ Functional

Returns a new instance of Functional.



69
70
71
# File 'lib/view_model/active_record/update_data.rb', line 69

def initialize(actions)
  @actions = actions
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



68
69
70
# File 'lib/view_model/active_record/update_data.rb', line 68

def actions
  @actions
end

Instance Method Details

#check_for_duplicates!(update_context, blame) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/view_model/active_record/update_data.rb', line 97

def check_for_duplicates!(update_context, blame)
  duplicate_vm_refs = vm_references(update_context).duplicates
  if duplicate_vm_refs.present?
    formatted_invalid_ids = duplicate_vm_refs.keys.map(&:to_s).join(', ')
    raise ViewModel::DeserializationError::InvalidStructure.new("Duplicate functional update targets: [#{formatted_invalid_ids}]", blame)
  end
end

#contentsObject



73
74
75
76
77
78
# File 'lib/view_model/active_record/update_data.rb', line 73

def contents
  actions.lazy
    .reject { |action| action.is_a?(FunctionalUpdate::Remove) }
    .flat_map(&:contents)
    .to_a
end

#removed_vm_refsObject



90
91
92
93
94
95
# File 'lib/view_model/active_record/update_data.rb', line 90

def removed_vm_refs
  actions.lazy
    .select { |action| action.is_a?(FunctionalUpdate::Remove) }
    .flat_map(&:removed_vm_refs)
    .to_a
end

#used_vm_refs(_update_context) ⇒ Object

Resolve ViewModel::References used in the update’s contents, whether by reference or value.

Raises:

  • (RuntimeError)


86
87
88
# File 'lib/view_model/active_record/update_data.rb', line 86

def used_vm_refs(_update_context)
  raise RuntimeError.new('abstract method')
end

#vm_references(update_context) ⇒ Object



80
81
82
# File 'lib/view_model/active_record/update_data.rb', line 80

def vm_references(update_context)
  used_vm_refs(update_context) + removed_vm_refs
end