class ViewModel::References
A bucket for configuration, used for serializing and deserializing.
Public Class Methods
new()
click to toggle source
# File lib/view_model/references.rb, line 8 def initialize @last_ref = 0 @ref_by_value = {} @value_by_ref = {} end
Public Instance Methods
add_reference(value)
click to toggle source
Takes a reference to a thing that is to be shared, and returns the id under which the data is stored. If the data is not present, will compute it by calling the given block.
# File lib/view_model/references.rb, line 21 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
clear!()
click to toggle source
# File lib/view_model/references.rb, line 33 def clear! @ref_by_value.clear @value_by_ref.clear end
has_references?()
click to toggle source
# File lib/view_model/references.rb, line 14 def has_references? @ref_by_value.present? end
Private Instance Methods
new_ref!(viewmodel)
click to toggle source
Ensure stable reference ids for the same (persisted) viewmodels.
# File lib/view_model/references.rb, line 41 def new_ref!(viewmodel) vm_ref = viewmodel.to_reference if vm_ref.model_id hash = Digest::SHA256.base64digest("#{vm_ref.viewmodel_class.name}.#{vm_ref.model_id}") "ref:h:#{hash}" else format('ref:i:%06<count>d', count: (@last_ref += 1)) end end