Class: Graphlyte::Editor::Context
- Inherits:
-
Struct
- Object
- Struct
- Graphlyte::Editor::Context
- Defined in:
- lib/graphlyte/editor.rb
Overview
The class responsible for orchestration of the hooks. This class defines the recursion through the document.
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#document ⇒ Object
Returns the value of attribute document.
-
#hooks ⇒ Object
Returns the value of attribute hooks.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #edit(object, &block) ⇒ Object
- #edit_arguments(object) ⇒ Object
- #edit_bottom_up(object, processor, action) ⇒ Object
- #edit_definition(object) ⇒ Object
- #edit_directives(object) ⇒ Object
- #edit_selection(object) ⇒ Object
- #edit_top_down(object, processor, action) ⇒ Object
- #edit_value(object) ⇒ Object
- #edit_variables(object) ⇒ Object
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction
90 91 92 |
# File 'lib/graphlyte/editor.rb', line 90 def direction @direction end |
#document ⇒ Object
Returns the value of attribute document
90 91 92 |
# File 'lib/graphlyte/editor.rb', line 90 def document @document end |
#hooks ⇒ Object
Returns the value of attribute hooks
90 91 92 |
# File 'lib/graphlyte/editor.rb', line 90 def hooks @hooks end |
#path ⇒ Object
Returns the value of attribute path
90 91 92 |
# File 'lib/graphlyte/editor.rb', line 90 def path @path end |
Instance Method Details
#edit(object, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/graphlyte/editor.rb', line 91 def edit(object, &block) parent = path.last path.push(object) processor = hooks[object.class] action = Action.new(object, path, parent, document) case direction when :bottom_up edit_bottom_up(object, processor, action, &block) when :top_down edit_top_down(object, processor, action, &block) else raise ArgumentError, "Unknown direction: #{direction}" end action.new_nodes ensure path.pop end |
#edit_arguments(object) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/graphlyte/editor.rb', line 148 def edit_arguments(object) return unless object.respond_to?(:arguments) object.arguments = object.arguments&.flat_map do |arg| edit(arg) do |_a| arg.value = edit_value(arg.value).first raise Deleted if arg.value.nil? end end end |
#edit_bottom_up(object, processor, action) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/graphlyte/editor.rb', line 122 def edit_bottom_up(object, processor, action) yield object if block_given? processor&.call(object, action) rescue Deleted action.new_nodes = [] end |
#edit_definition(object) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/graphlyte/editor.rb', line 188 def edit_definition(object) edit(object) do |o| edit_variables(o) edit_directives(o) edit_selection(o) end end |
#edit_directives(object) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/graphlyte/editor.rb', line 140 def edit_directives(object) return unless object.respond_to?(:directives) object.directives = object.directives&.flat_map do |dir| edit(dir) { |d| edit_arguments(d) } end end |
#edit_selection(object) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/graphlyte/editor.rb', line 176 def edit_selection(object) return unless object.respond_to?(:selection) object.selection = object.selection&.flat_map do |selected| edit(selected) do |s| edit_arguments(s) edit_directives(s) edit_selection(s) end end end |
#edit_top_down(object, processor, action) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/graphlyte/editor.rb', line 112 def edit_top_down(object, processor, action) processor&.call(object, action) action.new_nodes = action.new_nodes.filter_map do |node| yield node if block_given? node rescue Deleted nil end end |
#edit_value(object) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/graphlyte/editor.rb', line 159 def edit_value(object) case object when Array [object.flat_map { edit_value(_1) }] when Hash [ object.to_a.flat_map do |(k, old_value)| edit_value(old_value).take(1).map do |new_value| [k, new_value] end end.to_h ] else edit(object) end end |
#edit_variables(object) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/graphlyte/editor.rb', line 129 def edit_variables(object) return unless object.respond_to?(:variables) object.variables = object.variables&.flat_map do |var| edit(var) do |v| edit_directives(v) v.default_value = edit_value(v.default_value).first end end end |