Class: ViewModel::References

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

Overview

A bucket for configuration, used for serializing and deserializing.

Instance Method Summary collapse

Constructor Details

#initializeReferences

Returns a new instance of References.



8
9
10
11
12
# File 'lib/view_model/references.rb', line 8

def initialize
  @last_ref = 0
  @ref_by_value = {}
  @value_by_ref = {}
end

Instance Method Details

#add_reference(value) ⇒ Object

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.



21
22
23
24
25
26
27
28
29
30
31
# 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!Object



33
34
35
36
# File 'lib/view_model/references.rb', line 33

def clear!
  @ref_by_value.clear
  @value_by_ref.clear
end

#has_references?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/view_model/references.rb', line 14

def has_references?
  @ref_by_value.present?
end