class ViewModel::References
A bucket for configuration, used for serializing and deserializing.
def add_reference(value)
under which the data is stored. If the data is not present, will compute
Takes a reference to a thing that is to be shared, and returns the id
def add_reference(value) ref = @ref_by_value[value] unless ref.present? ref = new_ref!(value) @ref_by_value[value] = ref @value_by_ref[ref] = value end ref end
def clear!
def clear! @ref_by_value.clear @value_by_ref.clear end
def has_references?
def has_references? @ref_by_value.present? end
def initialize
def initialize @last_ref = 0 @ref_by_value = {} @value_by_ref = {} end
def new_ref!(viewmodel)
unpersisted viewmodels, use a counter to generate a reference key unique
Ensure stable reference keys for the same (persisted) viewmodels. For
def new_ref!(viewmodel) vm_ref = viewmodel.to_reference if vm_ref.model_id vm_ref.stable_reference else format('ref:i:%06<count>d', count: (@last_ref += 1)) end end