Class: ViewModel::References
- Inherits:
-
Object
- Object
- ViewModel::References
- Defined in:
- lib/view_model/references.rb
Overview
A bucket for configuration, used for serializing and deserializing.
Instance Method Summary collapse
-
#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.
- #clear! ⇒ Object
- #has_references? ⇒ Boolean
-
#initialize ⇒ References
constructor
A new instance of References.
Constructor Details
#initialize ⇒ References
Returns a new instance of References.
6 7 8 9 10 |
# File 'lib/view_model/references.rb', line 6 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.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/view_model/references.rb', line 19 def add_reference(value) if (ref = @ref_by_value[value]).present? ref else ref = new_ref!(value) @ref_by_value[value] = ref @value_by_ref[ref] = value ref end end |
#clear! ⇒ Object
30 31 32 33 |
# File 'lib/view_model/references.rb', line 30 def clear! @ref_by_value.clear @value_by_ref.clear end |
#has_references? ⇒ Boolean
12 13 14 |
# File 'lib/view_model/references.rb', line 12 def has_references? @ref_by_value.present? end |