Class: Glib::Json::Traversal::Visitor

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/concerns/glib/json/traversal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crawler_test: false) ⇒ Visitor

Returns a new instance of Visitor.



38
39
40
41
42
43
# File 'app/controllers/concerns/glib/json/traversal.rb', line 38

def initialize(crawler_test: false)
  @crawler_test = crawler_test
  @component_registries = []
  @forms = []
  @deferred_actions = []
end

Instance Attribute Details

#component_registriesObject (readonly)

Used in test crawler



36
37
38
# File 'app/controllers/concerns/glib/json/traversal.rb', line 36

def component_registries
  @component_registries
end

#deferred_actionsObject (readonly)

Used in test crawler



36
37
38
# File 'app/controllers/concerns/glib/json/traversal.rb', line 36

def deferred_actions
  @deferred_actions
end

#formsObject (readonly)

Used in test crawler



36
37
38
# File 'app/controllers/concerns/glib/json/traversal.rb', line 36

def forms
  @forms
end

Instance Method Details

#begin_page(spec) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/concerns/glib/json/traversal.rb', line 49

def begin_page(spec)
  return unless @crawler_test

  if (form = fullPageForm(spec))
    @forms << form
  end

  @component_registries << {}
  @deferred_actions << []
end

#current_component_registryObject



105
106
107
# File 'app/controllers/concerns/glib/json/traversal.rb', line 105

def current_component_registry
  @component_registries.last
end

#current_deferred_actionsObject



101
102
103
# File 'app/controllers/concerns/glib/json/traversal.rb', line 101

def current_deferred_actions
  @deferred_actions.last
end

#defer_action(action_crawler, target_id) ⇒ Object



109
110
111
112
113
114
# File 'app/controllers/concerns/glib/json/traversal.rb', line 109

def defer_action(action_crawler, target_id)
  current_deferred_actions << {
    action: action_crawler,
    target_id: target_id
  }
end

#end_page(spec) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/concerns/glib/json/traversal.rb', line 60

def end_page(spec)
  return unless @crawler_test

  if fullPageForm(spec)
    @forms.pop
  end

  current_actions = @deferred_actions.pop

  # Execute actions with target IDs to ensure the target components have been loade.
  current_actions.each do |action|
    action, target_id = action.values
    if (view_spec = current_component_registry[target_id])
      action.execute(view_spec) if action
    else
      raise "View not found for ID: #{target_id}"
    end
  end

  @component_registries.pop
end

#fullPageForm(spec) ⇒ Object



45
46
47
# File 'app/controllers/concerns/glib/json/traversal.rb', line 45

def fullPageForm(spec)
  spec['fullPageForm']
end

#traverse_multiple(views, block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/concerns/glib/json/traversal.rb', line 82

def traverse_multiple(views, block)
  if views.is_a? Array
    views.each do |view|
      case view['view']
      when 'panels/form-v1'
        @forms << view
        traverse_single view, block
        @forms.pop
      else
        traverse_single view, block
      end
    end
  end
end

#traverse_single(view, block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/concerns/glib/json/traversal.rb', line 116

def traverse_single(view, block)
  if @crawler_test && (view_id = view['id'])
    current_component_registry[view_id] = view
  end

  block.call view

  # Generic view children
  traverse_multiple view['childViews'], block

  # Generic menu buttons
  traverse_multiple view['buttons'], block
  traverse_multiple view['childButtons'], block

  # Split panel
  traverse_vertical_content view['left'], block
  traverse_vertical_content view['center'], block
  traverse_vertical_content view['right'], block

  # List template
  traverse_multiple view['editButtons'], block
  traverse_multiple view['leftButtons'], block
  traverse_multiple view['rightButtons'], block

  # Select field
  traverse_vertical_content view['header'], block
  traverse_vertical_content view['footer'], block

  # Table row
  traverse_multiple view['cellViews'], block

  # Table/List
  if (sections = view['sections']).is_a? Array
    sections.each do |section|
      traverse_vertical_content section['header'], block
      traverse_multiple section['rows'], block
      traverse_vertical_content section['footer'], block
    end
  end
end

#traverse_vertical_content(view, block) ⇒ Object



97
98
99
# File 'app/controllers/concerns/glib/json/traversal.rb', line 97

def traverse_vertical_content(view, block)
  traverse_multiple view['childViews'], block if view
end