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.



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

def initialize(actions)
  @actions = actions
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



71
72
73
# File 'lib/view_model/active_record/update_data.rb', line 71

def actions
  @actions
end

Instance Method Details

#check_for_duplicates!(update_context, blame) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/view_model/active_record/update_data.rb', line 101

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



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

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

#removed_vm_refsObject



94
95
96
97
98
99
# File 'lib/view_model/active_record/update_data.rb', line 94

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)


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

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

#vm_references(update_context) ⇒ Object



84
85
86
# File 'lib/view_model/active_record/update_data.rb', line 84

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