Class: ViewModel::TraversalContext

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

Overview

Abstract base for Serialize and DeserializeContexts.

Direct Known Subclasses

DeserializeContext, SerializeContext

Defined Under Namespace

Classes: SharedContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shared_context: nil, **shared_context_params) ⇒ TraversalContext

Returns a new instance of TraversalContext.



31
32
33
34
35
36
37
38
# File 'lib/view_model/traversal_context.rb', line 31

def initialize(shared_context: nil, **shared_context_params)
  super()
  @shared_context     = shared_context || self.class.shared_context_class.new(**shared_context_params)
  @parent_context     = nil
  @parent_viewmodel   = nil
  @parent_association = nil
  @root               = true
end

Instance Attribute Details

#shared_contextObject (readonly)

Returns the value of attribute shared_context.



23
24
25
# File 'lib/view_model/traversal_context.rb', line 23

def shared_context
  @shared_context
end

Class Method Details

.new_childObject



27
28
29
# File 'lib/view_model/traversal_context.rb', line 27

def self.new_child(...)
  self.allocate.tap { |c| c.initialize_as_child(...) }
end

.shared_context_classObject



19
20
21
# File 'lib/view_model/traversal_context.rb', line 19

def self.shared_context_class
  SharedContext
end

Instance Method Details

#for_child(parent_viewmodel, association_name:, **rest) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/view_model/traversal_context.rb', line 51

def for_child(parent_viewmodel, association_name:, **rest)
  self.class.new_child(
    shared_context:     shared_context,
    parent_context:     self,
    parent_viewmodel:   parent_viewmodel,
    parent_association: association_name,
    **rest)
end

#for_referencesObject

Obtain a semi-independent context for descending through a shared reference: keep the same shared context, but drop any tree location specific local context (since a shared reference could equally have been reached via any parent)



64
65
66
# File 'lib/view_model/traversal_context.rb', line 64

def for_references
  self.class.new(shared_context: shared_context)
end

#initialize_as_child(shared_context:, parent_context:, parent_viewmodel:, parent_association:) ⇒ Object

Overloaded constructor for initialization of descendent node contexts. Shared context is the same, ancestry is established, and subclasses can override to maintain other node-specific state.



43
44
45
46
47
48
49
# File 'lib/view_model/traversal_context.rb', line 43

def initialize_as_child(shared_context:, parent_context:, parent_viewmodel:, parent_association:)
  @shared_context     = shared_context
  @parent_context     = parent_context
  @parent_viewmodel   = parent_viewmodel
  @parent_association = parent_association
  @root               = false
end

#nearest_rootObject



112
113
114
115
116
117
118
# File 'lib/view_model/traversal_context.rb', line 112

def nearest_root
  if root?
    self
  else
    parent_context&.nearest_root
  end
end

#nearest_root_viewmodelObject



120
121
122
123
124
125
126
127
128
# File 'lib/view_model/traversal_context.rb', line 120

def nearest_root_viewmodel
  if root?
    raise RuntimeError.new('Attempted to find nearest root from a root context. This is probably not what you wanted.')
  elsif parent_context.root?
    parent_viewmodel
  else
    parent_context.nearest_root_viewmodel
  end
end

#parent_association(idx = 0) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/view_model/traversal_context.rb', line 84

def parent_association(idx = 0)
  if idx == 0
    @parent_association
  else
    parent_context(idx - 1)&.parent_association
  end
end

#parent_context(idx = 0) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/view_model/traversal_context.rb', line 68

def parent_context(idx = 0)
  if idx == 0
    @parent_context
  else
    @parent_context&.parent_context(idx - 1)
  end
end

#parent_ref(idx = 0) ⇒ Object



92
93
94
# File 'lib/view_model/traversal_context.rb', line 92

def parent_ref(idx = 0)
  parent_viewmodel(idx)&.to_reference
end

#parent_viewmodel(idx = 0) ⇒ Object



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

def parent_viewmodel(idx = 0)
  if idx == 0
    @parent_viewmodel
  else
    parent_context(idx - 1)&.parent_viewmodel
  end
end

#root?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/view_model/traversal_context.rb', line 108

def root?
  @root
end

#run_callback(hook, view, **args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/view_model/traversal_context.rb', line 96

def run_callback(hook, view, **args)
  # Run in-viewmodel callback hooks before context hooks, as they are
  # permitted to alter the model.
  if view.respond_to?(hook.dsl_viewmodel_callback_method)
    view.public_send(hook.dsl_viewmodel_callback_method, hook.context_name => self, **args)
  end

  callbacks.each do |callback|
    callback.run_callback(hook, view, self, **args)
  end
end