Class: ViewModel::TraversalContext
- Inherits:
-
Object
- Object
- ViewModel::TraversalContext
show all
- Defined in:
- lib/view_model/traversal_context.rb
Overview
Abstract base for Serialize and DeserializeContexts.
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.
30
31
32
33
34
35
36
37
|
# File 'lib/view_model/traversal_context.rb', line 30
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_context ⇒ Object
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_child(*args) ⇒ Object
26
27
28
|
# File 'lib/view_model/traversal_context.rb', line 26
def self.new_child(*args)
self.allocate.tap { |c| c.initialize_as_child(*args) }
end
|
.shared_context_class ⇒ Object
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
50
51
52
53
54
55
56
57
|
# File 'lib/view_model/traversal_context.rb', line 50
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_references ⇒ Object
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)
63
64
65
|
# File 'lib/view_model/traversal_context.rb', line 63
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.
42
43
44
45
46
47
48
|
# File 'lib/view_model/traversal_context.rb', line 42
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_root ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/view_model/traversal_context.rb', line 109
def nearest_root
if root?
self
else
parent_context&.nearest_root
end
end
|
#nearest_root_viewmodel ⇒ Object
117
118
119
120
121
122
123
124
125
|
# File 'lib/view_model/traversal_context.rb', line 117
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
83
84
85
86
87
88
89
|
# File 'lib/view_model/traversal_context.rb', line 83
def parent_association(idx = 0)
if idx == 0
@parent_association
else
parent_context(idx - 1)&.parent_association
end
end
|
#parent_context(idx = 0) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/view_model/traversal_context.rb', line 67
def parent_context(idx = 0)
if idx == 0
@parent_context
else
@parent_context&.parent_context(idx - 1)
end
end
|
#parent_ref(idx = 0) ⇒ Object
91
92
93
|
# File 'lib/view_model/traversal_context.rb', line 91
def parent_ref(idx = 0)
parent_viewmodel(idx)&.to_reference
end
|
#parent_viewmodel(idx = 0) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/view_model/traversal_context.rb', line 75
def parent_viewmodel(idx = 0)
if idx == 0
@parent_viewmodel
else
parent_context(idx - 1)&.parent_viewmodel
end
end
|
#root? ⇒ Boolean
105
106
107
|
# File 'lib/view_model/traversal_context.rb', line 105
def root?
@root
end
|
#run_callback(hook, view, **args) ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/view_model/traversal_context.rb', line 95
def run_callback(hook, view, **args)
callbacks.each do |callback|
callback.run_callback(hook, view, self, **args)
end
if view.respond_to?(hook.dsl_viewmodel_callback_method)
view.public_send(hook.dsl_viewmodel_callback_method, hook.context_name => self, **args)
end
end
|