Class: Graphlyte::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlyte/editor.rb

Overview

Walk the document tree and edit or collect data.

This is the general purpose recursive transformer for syntax trees, used to write various validation and transformation passes. See ‘lib/graphlyte/editors`

Usage

A fragment inliner:

inliner = Editor.new.on_fragment_spread do |spread, action|
  action.replace action.document.fragments[spread.name].inline
end

inliner.edit(document)

A variable renamer:

renamer = Editor.new.on_variable do |var|
  var.variable = 'x' if var.variable == 'y'
end

renamer.edit(document)

A string collector:

strings = []
collector = Editor.new.on_value do |value|
  strings << value.value if value.type == :STRING
end

collector.edit(document)

Defined Under Namespace

Classes: Action, Context

Constant Summary collapse

Deleted =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEditor

Returns a new instance of Editor.



44
45
46
47
# File 'lib/graphlyte/editor.rb', line 44

def initialize
  @hooks = {}
  @direction = :bottom_up
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



42
43
44
# File 'lib/graphlyte/editor.rb', line 42

def direction
  @direction
end

Class Method Details

.bottom_upObject



204
205
206
# File 'lib/graphlyte/editor.rb', line 204

def self.bottom_up
  new
end

.top_downObject



197
198
199
200
201
202
# File 'lib/graphlyte/editor.rb', line 197

def self.top_down
  e = new
  e.direction = :top_down

  e
end

Instance Method Details

#context(document = nil) ⇒ Object

To edit specific nodes in a document (or isolated from a document) you will need a Context.



274
275
276
# File 'lib/graphlyte/editor.rb', line 274

def context(document = nil)
  Context.new(document, direction, @hooks.dup.freeze, [])
end

#edit(document) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/graphlyte/editor.rb', line 278

def edit(document)
  c = context(document)

  document.definitions = document.definitions.flat_map do |object|
    c.edit_definition(object)
  end

  document
end

#on_argument(&block) ⇒ Object



213
214
215
216
# File 'lib/graphlyte/editor.rb', line 213

def on_argument(&block)
  @hooks[Syntax::Argument] = block
  self
end

#on_directive(&block) ⇒ Object



218
219
220
221
# File 'lib/graphlyte/editor.rb', line 218

def on_directive(&block)
  @hooks[Syntax::Directive] = block
  self
end

#on_field(&block) ⇒ Object

Selected nodes:



246
247
248
249
# File 'lib/graphlyte/editor.rb', line 246

def on_field(&block)
  @hooks[Syntax::Field] = block
  self
end

#on_fragment(&block) ⇒ Object



251
252
253
254
255
# File 'lib/graphlyte/editor.rb', line 251

def on_fragment(&block)
  on_inline_fragment(&block)
  on_fragment_definition(&block)
  self
end

#on_fragment_definition(&block) ⇒ Object



267
268
269
270
# File 'lib/graphlyte/editor.rb', line 267

def on_fragment_definition(&block)
  @hooks[Syntax::Fragment] = block
  self
end

#on_fragment_spread(&block) ⇒ Object



257
258
259
260
# File 'lib/graphlyte/editor.rb', line 257

def on_fragment_spread(&block)
  @hooks[Syntax::FragmentSpread] = block
  self
end

#on_inline_fragment(&block) ⇒ Object



262
263
264
265
# File 'lib/graphlyte/editor.rb', line 262

def on_inline_fragment(&block)
  @hooks[Syntax::InlineFragment] = block
  self
end

#on_operation(&block) ⇒ Object



223
224
225
226
# File 'lib/graphlyte/editor.rb', line 223

def on_operation(&block)
  @hooks[Syntax::Operation] = block
  self
end

#on_value(&block) ⇒ Object



208
209
210
211
# File 'lib/graphlyte/editor.rb', line 208

def on_value(&block)
  @hooks[Syntax::Value] = block
  self
end

#on_variable(&block) ⇒ Object



228
229
230
231
232
# File 'lib/graphlyte/editor.rb', line 228

def on_variable(&block)
  on_variable_definition(&block)
  on_variable_reference(&block)
  self
end

#on_variable_definition(&block) ⇒ Object



234
235
236
237
# File 'lib/graphlyte/editor.rb', line 234

def on_variable_definition(&block)
  @hooks[Syntax::VariableDefinition] = block
  self
end

#on_variable_reference(&block) ⇒ Object



239
240
241
242
# File 'lib/graphlyte/editor.rb', line 239

def on_variable_reference(&block)
  @hooks[Syntax::VariableReference] = block
  self
end